Skip to content

Instantly share code, notes, and snippets.

@larsxschneider
Forked from agnoster/github.com.js
Created October 31, 2012 17:25
Show Gist options
  • Save larsxschneider/3988475 to your computer and use it in GitHub Desktop.
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.
/**
* 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();
})
@torsten
Copy link

torsten commented Nov 1, 2012

Awesome, works great for me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment