Created
March 30, 2022 11:54
-
-
Save divinity76/ab680db65337ff1ce9b9270e0007289d to your computer and use it in GitHub Desktop.
get all github repos of user
This file contains 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 get_all_github_repos_of_user(user) { | |
let extractedRepos = []; | |
let wget = function(url) { | |
let xhr = new XMLHttpRequest(); | |
xhr.open("GET", url, false); | |
(xhr.send()); | |
return xhr.responseText; | |
}; | |
let domparser = function(html) { | |
return (new DOMParser()).parseFromString(html, "text/html"); | |
}; | |
let $ = document.querySelectorAll.bind(document); | |
let nextPage = "https://github.com/" + encodeURIComponent(user) + "?tab=repositories"; | |
while (nextPage) { | |
let page = nextPage; | |
let pageDom = domparser(wget(page)); | |
window.debugDom = pageDom; | |
if (pageDom.querySelectorAll("[data-test-selector='pagination'] a").length === 2) { | |
nextPage = pageDom.querySelectorAll("[data-test-selector='pagination'] a")[1].href; | |
} else if (pageDom.querySelectorAll("[data-test-selector='pagination'] a")[0].textContent === "Next") { | |
nextPage = pageDom.querySelectorAll("[data-test-selector='pagination'] a")[0].href; | |
} else { | |
nextPage = null; | |
} | |
let repos = pageDom.querySelectorAll("#user-repositories-list li"); | |
repos.forEach(function(repo) { | |
let toAdd = { | |
name: repo.querySelectorAll("[itemprop*='name codeRepository']")[0].textContent.trim(), | |
url: repo.querySelectorAll("[itemprop*='name codeRepository']")[0].href, | |
// not all repos have a description, in which case the querys selector will return an empty array.. | |
description: repo.querySelectorAll("[itemprop*='description']").length ? repo.querySelectorAll("[itemprop*='description']")[0].textContent.trim() : "", | |
}; | |
extractedRepos.push(toAdd); | |
console.log(toAdd); // using shitty blocking wget, this console.log gives an indication of progress and that we're not hanging.. | |
}); | |
} | |
return extractedRepos; | |
} | |
get_all_github_repos_of_user("divinity76"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment