Created
September 26, 2010 18:22
-
-
Save hmarr/598175 to your computer and use it in GitHub Desktop.
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
// ==UserScript== | |
// @name GitHub Contributor Compare | |
// @description Adds a compare link to forked repo commit pages | |
// @match http://github.com/*/commit/* | |
// @match https://github.com/*/commit/* | |
// ==/UserScript== | |
var onLoad = function(callback) { | |
var script = document.createElement("script"); | |
var src = "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"; | |
script.setAttribute("src", src); | |
script.addEventListener('load', function() { | |
var script = document.createElement("script"); | |
script.textContent = "(" + callback.toString() + ")();"; | |
document.body.appendChild(script); | |
}, false); | |
document.body.appendChild(script); | |
} | |
onLoad(function() { | |
var source = $('.fork-flag a')[0]; | |
if (source) { | |
source = source.innerHTML; | |
var branchUrl = '/api/v2/json/repos/show/' + source + '/branches'; | |
$.getJSON(branchUrl, function(response) { | |
var branches = []; | |
for (branch in response.branches) { | |
branches.push(branch); | |
} | |
sourceUser = source.split('/')[0]; | |
var makeUrl = function(branch) { | |
var urlParts = window.location.href.split('/'); | |
var commit = urlParts.pop(); | |
urlParts.push(sourceUser + ':' + branch + '...' + commit); | |
return urlParts.join('/').replace('commit', 'compare'); | |
}; | |
var navBar = $($('.subnav-bar ul')[0]); | |
var link = $('<a></a>').attr('href', '#'); | |
link.html('Compare to Source Branch (' + branches.length + ')'); | |
link.addClass('dropdown'); | |
var navItem = $('<li></li>').append(link); | |
var dropDown = $('<ul></ul>'); | |
for (var i = 0; i < branches.length; i++) { | |
var branch = branches[i]; | |
var branchItem = $('<li></li>') | |
var branchLink = $('<a></a>').html(branch); | |
branchLink.attr('href', makeUrl(branch)); | |
branchItem.append(branchLink); | |
dropDown.append(branchItem); | |
} | |
navItem.append(dropDown); | |
navBar.append(navItem); | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment