Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
get all github repos of user
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