Last active
June 6, 2025 14:00
-
-
Save nemanjam/29b037188d13ffad0945d22fe30613c4 to your computer and use it in GitHub Desktop.
Get all *.github.io and github.com/* links
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function setupGithubLinkCollectorUI() { | |
// === Get all <a href="..."> values from the page === | |
function getAllAnchors() { | |
return Array.from(document.querySelectorAll('a[href]')).map(a => a.href); | |
} | |
// === Extract only the root of *.github.io links (e.g. https://user.github.io) === | |
function extractGithubIoRoots(hrefs) { | |
return [...new Set( | |
hrefs | |
.filter(href => href.includes('.github.io')) | |
.map(href => { | |
try { | |
const url = new URL(href); | |
return `${url.protocol}//${url.host}`; // Remove path, keep root | |
} catch { | |
return null; | |
} | |
}) | |
.filter(Boolean) | |
)]; | |
} | |
// === Extract only the username root of github.com links (e.g. https://github.com/user) === | |
function extractGithubComUserLinks(hrefs) { | |
return [...new Set( | |
hrefs | |
.filter(href => href.includes('github.com/')) | |
.map(href => { | |
try { | |
const url = new URL(href); | |
const parts = url.pathname.split('/').filter(Boolean); // Removes empty strings | |
return parts.length >= 1 | |
? `${url.protocol}//${url.host}/${parts[0]}` // Keep only first path part (username) | |
: null; | |
} catch { | |
return null; | |
} | |
}) | |
.filter(Boolean) | |
)]; | |
} | |
// === Display list of links as clickable <a> elements === | |
function displayLinks(links, container) { | |
container.innerHTML = ''; // Clear previous results | |
links.forEach(link => { | |
const a = document.createElement('a'); | |
a.href = link; | |
a.textContent = link; | |
a.target = '_blank'; // Open in new tab | |
a.style.display = 'block'; | |
container.appendChild(a); | |
}); | |
} | |
// === Handle click on "Collect *.github.io" button === | |
function handleGithubIoClick(outputContainer) { | |
const hrefs = getAllAnchors(); | |
const links = extractGithubIoRoots(hrefs); | |
console.log('github.io links:', links); | |
displayLinks(links, outputContainer); | |
} | |
// === Handle click on "Collect github.com" button === | |
function handleGithubComClick(outputContainer) { | |
const hrefs = getAllAnchors(); | |
const links = extractGithubComUserLinks(hrefs); | |
console.log('github.com user links:', links); | |
displayLinks(links, outputContainer); | |
} | |
// === Build the UI and inject it into the page === | |
function createCollectorUI() { | |
// Remove old UI if already added | |
const existing = document.getElementById('github-link-collector'); | |
if (existing) existing.remove(); | |
// Create wrapper box | |
const wrapper = document.createElement('div'); | |
wrapper.id = 'github-link-collector'; | |
Object.assign(wrapper.style, { | |
position: 'fixed', | |
top: '10px', | |
right: '10px', | |
backgroundColor: 'white', | |
border: '1px solid #888', | |
padding: '12px', | |
zIndex: 10000, | |
fontSize: '14px', | |
fontFamily: 'sans-serif', | |
maxHeight: '90vh', | |
overflowY: 'auto' | |
}); | |
// Output area to display links | |
const output = document.createElement('div'); | |
output.style.marginTop = '10px'; | |
// Create first button | |
const btnIo = document.createElement('button'); | |
btnIo.textContent = 'Collect *.github.io links'; | |
btnIo.style.display = 'block'; | |
btnIo.style.marginBottom = '8px'; | |
btnIo.onclick = () => handleGithubIoClick(output); | |
// Create second button | |
const btnCom = document.createElement('button'); | |
btnCom.textContent = 'Collect github.com user links'; | |
btnCom.style.display = 'block'; | |
btnCom.style.marginBottom = '8px'; | |
btnCom.onclick = () => handleGithubComClick(output); | |
// Add buttons and output to wrapper, add wrapper to page | |
wrapper.appendChild(btnIo); | |
wrapper.appendChild(btnCom); | |
wrapper.appendChild(output); | |
document.body.appendChild(wrapper); | |
} | |
// === Initialize the UI === | |
createCollectorUI(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment