Skip to content

Instantly share code, notes, and snippets.

@MilesQLi
Forked from techpavan/forkAllGitHubRepos.js
Created April 4, 2018 17:06
Show Gist options
  • Save MilesQLi/6cef4d4d048a4d3fdd3d4acf9e63df05 to your computer and use it in GitHub Desktop.
Save MilesQLi/6cef4d4d048a4d3fdd3d4acf9e63df05 to your computer and use it in GitHub Desktop.
Fork all repositories from a user on GitHub
var totalForkCount;
var curForkCount=0;
var errorForkArray = new Array();
function initProcess(){
var user = window.location.pathname.split('/')[1];
var pageNo = 1;
var hrefArray = new Array();
while(hrefArray.length % 30 == 0){
var resp = sendSyncAjax("https://github.com/"+user+"?tab=repositories&page="+pageNo);
hrefArray = hrefArray.concat(getReposFromHtml(resp));
pageNo++;
}
totalForkCount = hrefArray.length;
forkAll(hrefArray);
}
function getReposFromHtml(response){
var text = response.responseText;
var parser = new DOMParser();
var html = parser.parseFromString(text, "text/html");
var repoCount = html.getElementsByClassName('d-inline-block mb-1').length;
var hrefArray = new Array();
for(var i=0; i<repoCount; i++){
hrefArray.push(html.getElementsByClassName('d-inline-block mb-1')[i].getElementsByTagName('a')[0].href);
}
return hrefArray;
}
function sendSyncAjax(url){
var xhr = new XMLHttpRequest();
xhr.open('GET', url, false);
xhr.send(null);
return xhr;
}
function forkAll(hrefArray){
var div = document.createElement('div');
div.style.display='none';
div.id='hdiv';
document.body.appendChild(div);
for (var i=0; i<hrefArray.length; i++){
loadPageAndFork(hrefArray[i]);
}
}
function loadPageAndFork(url){
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var parser = new DOMParser();
var html = parser.parseFromString(xhr.responseText, "text/html");
if(html.getElementsByClassName('btn-with-count').length == 5){
var actionUrl = html.getElementsByClassName('btn-with-count')[3].action;
var formData = html.getElementsByClassName('btn-with-count')[3].getElementsByTagName('input');
var postData = 'utf8=%E2%9C%93&authenticity_token='+encodeURIComponent(formData[1].value);
sendForkRequest(actionUrl, postData);
}else{
errorForkArray.push(url);
if(totalForkCount == curForkCount+errorForkArray.length){
window.alert("Done forking "+totalForkCount+" repositories. Error with these: "+errorForkArray);
}
}
}
};
xhr.send(null);
}
function sendForkRequest(actionUrl, postData){
var xhr = new XMLHttpRequest();
xhr.open('POST', actionUrl, false);
xhr.onreadystatechange = function() {
if(this.readyState == 4){
if (this.status == 200) {
curForkCount++;
}else{
errorForkArray.push(actionUrl);
}
if(totalForkCount == curForkCount+errorForkArray.length){
window.alert("Done forking "+totalForkCount+" repositories. Error with these: "+errorForkArray);
}
}
}
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send(postData);
}
initProcess();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment