Created
December 29, 2016 08:06
-
-
Save techpavan/a35e3b603249871e0578c55d58c2b7e0 to your computer and use it in GitHub Desktop.
Fork all repositories from a user on GitHub
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
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(); |
Please Help Me,
I need to fork all repos from "https://github.com/lineageos" to "https://github.com/RebaseOS". I tried it with your code but it doesnt worked...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The above javascript is built and tested with Firefox 50.1.0.
Access any page on the GitHub user's profile whose repositories need to be forked. Open firebug console and execute the above javascript.
Tried to create a bookmarklet to make it a simpler process, but Content Security Policy on GitHub blocks the execution.