-
-
Save larsxschneider/3988475 to your computer and use it in GitHub Desktop.
View/edit a file in a pull request by clicking on the line number.
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
/** | |
* 1) Use http://defunkt.io/dotjs/ | |
* 2) Install this into ~/.js/github.com.js | |
* 3) Click on a line number in the editor to open the file at this position (e.g. to see more context) | |
* 4) Alt+Click on a line number in the editor to edit the file at this position | |
* | |
* This script is based on @agnoster's idea: https://gist.github.com/3906361 | |
*/ | |
function getBranch() { | |
return $($('.pull-description .commit-ref')[1]).text().trim() | |
} | |
function getURLParameter(sParam) { | |
var sPageURL = window.location.search.substring(1); | |
var sURLVariables = sPageURL.split('&'); | |
for (var i = 0; i < sURLVariables.length; i++) { | |
var sParameterName = sURLVariables[i].split('='); | |
if (sParameterName[0] == sParam) { | |
return sParameterName[1]; | |
} | |
} | |
} | |
function updateButtons() { | |
var branch = getBranch() | |
if (!branch) return // couldn't get a branch? NOT A PULL REQUEST | |
$('.linkable-line-number').each(function(i, el) { | |
el.onclick = function(mouseEvent) { | |
var line = $.trim(el.innerText); | |
if(parseInt(line)) { | |
var baseURL = $(el).first().parentsUntil('.diff-view').last().find('.grouped-button')[0].href; | |
var lineParameter = '#L' + line + '-' + line; | |
if(mouseEvent.altKey) { | |
baseURL = baseURL.replace(/blob\/[a-z0-9]+/, 'edit/' + branch); | |
lineParameter = '?gotoLine=' + line; | |
} | |
window.open(baseURL + lineParameter, '_blank'); | |
} | |
} | |
}) | |
} | |
function gotoLine() { | |
var line = parseInt(getURLParameter('gotoLine')); | |
if(line) { | |
$(window).load(function() { | |
location.href = 'javascript:editor.ace.gotoLine(' + line + ', 0, false); editor.ace.scrollToRow(' + (line-1) + ');' | |
}); | |
} | |
} | |
$(function() { | |
updateButtons(); | |
gotoLine(); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome, works great for me!