-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathCopy Github Repositories on button click, change button color, and display count.user.js
More file actions
85 lines (74 loc) · 3.05 KB
/
Copy Github Repositories on button click, change button color, and display count.user.js
File metadata and controls
85 lines (74 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// ==UserScript==
// @name Copy Github Repositories
// @namespace http://tampermonkey.net/
// @version 0.2
// @description Copy Github Repositories on button click, change button color, and display count
// @author rix4uni
// @match https://github.com/search?q=*
// @icon https://www.google.com/s2/favicons?sz=64&domain=github.com
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Create a button element
const button = document.createElement('button');
button.textContent = 'Copy Repositories';
button.style.backgroundColor = 'green';
button.style.color = 'white';
button.style.padding = '8px 20px';
button.style.border = 'none';
button.style.cursor = 'pointer';
button.style.position = 'fixed';
button.style.top = '65px';
button.style.right = '10px';
button.style.zIndex = '9999';
button.style.fontFamily = `'Arial', sans-serif`; // Add a custom font
button.style.fontSize = '16px';
button.style.borderRadius = '8px'; // Add border radius for rounded corners
button.style.boxShadow = '0 4px 8px rgba(0, 0, 0, 0.2)'; // Optional: Add some shadow for better aesthetics
// Append the button to the body
document.body.appendChild(button);
// Function to reset button color
const resetButtonColor = () => {
button.style.backgroundColor = 'green';
};
// Add a click event listener to the button
button.addEventListener('click', () => {
// Select all elements with the specified class
const links = document.querySelectorAll('.Link__StyledLink-sc-14289xe-0.elltiT');
// Collect href attributes
const hrefs = [];
links.forEach(link => {
hrefs.push(link.href);
});
// Copy Repositories to clipboard
const hrefsText = hrefs.join('\n');
navigator.clipboard.writeText(hrefsText).then(() => {
alert(`${hrefs.length} Repositories copied to clipboard!`); // Show the count of hrefs copied
}).catch(err => {
console.error('Failed to copy: ', err);
});
// Change button color from green to blue
button.style.backgroundColor = 'blue';
});
// Monitor URL changes using a MutationObserver
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.type === 'childList' || mutation.type === 'attributes') {
// Check if the URL has changed
if (window.location.href !== observer.currentUrl) {
observer.currentUrl = window.location.href;
resetButtonColor();
}
}
});
});
// Set the initial URL
observer.currentUrl = window.location.href;
// Start observing the document body for changes
observer.observe(document.body, {
childList: true, // Watch for additions/removals of child elements
subtree: true, // Watch across the entire DOM tree
attributes: true // Watch for attribute changes
});
})();