Skip to content

Instantly share code, notes, and snippets.

@dhensby
Last active August 29, 2015 14:00
Show Gist options
  • Save dhensby/11214414 to your computer and use it in GitHub Desktop.
Save dhensby/11214414 to your computer and use it in GitHub Desktop.
Scroll to the next diff in a PR summary window!
(function($, w, undefined) {
var $diffs = $('.diff-view .js-details-container');
var $comments = $('.diff-view .js-comment');
var $w = $(w);
var $a = $('<a/>', {
'class': 'js-next-diff',
'href': '#'
}).css({
'position': 'fixed',
'font-size': 'large',
'display': 'block',
'padding': '1rem',
'text-transform': 'uppercase',
'top': 0,
'right': 0
}).text('Next').on('click', function(ev) {
ev.preventDefault();
goToNext($diffs);
});
$(document.body).append($a).on('keydown', function(ev) {
//if the event came from an input box, we don't want to scroll
if ($(ev.target).is(':input')) {
return true;
}
if (ev.which == 74) {
goToNext($diffs);
}
else if (ev.which == 75) {
goToPrev($diffs);
}
else if (ev.which == 78) {
goToNext($comments);
}
else if (ev.which == 77) {
goToPrev($comments);
}
});
function goToNext($els) {
var scrollTop = $w.scrollTop() + 5;
$els.each(function() {
var offsetTop = $(this).offset().top;
if (offsetTop > scrollTop) {
$('html,body').animate({scrollTop: offsetTop});
return false;
}
});
}
function goToPrev($els) {
var scrollTop = $w.scrollTop() - 5;
var prev;
$els.each(function() {
var offsetTop = $(this).offset().top;
if (offsetTop >= scrollTop) {
//don't animate here, because if we loop through all of them
// we want the last item to be animated to
return false;
}
prev = offsetTop;
});
//animate to the last "prev"
$('html,body').animate({scrollTop: prev});
}
})(jQuery, window);
@dhensby
Copy link
Author

dhensby commented Apr 23, 2014

It's pretty annoying in GitHub when you've got lots of files to review and some are large diffs generated automatically that you don't need to review. Speed scrolling means it's easy to miss the start of the next diff.

This script adds a link that takes you to the next diff to review.

Just copy and paste this script into your console when viewing the diffs tab of a PR.

Bookmarklet that doesn't work because of GitHub's security policy:

javascript:(function(){(function($,w,undefined){var $diffs=$('.diff-view .js-details-container');var $comments=$('.diff-view .js-comment');var $w=$(w);var $a=$('<a/>',{'class':'js-next-diff','href':'#'}).css({'position':'fixed','font-size':'large','display':'block','padding':'1rem','text-transform':'uppercase','top':0,'right':0}).text('Next').on('click',function(ev){ev.preventDefault();goToNext($diffs);});$(document.body).append($a).on('keydown',function(ev){if($(ev.target).is(':input')){return true;}if(ev.which==74){goToNext($diffs);}else if(ev.which==75){goToPrev($diffs);}else if(ev.which==78){goToNext($comments);}else if(ev.which==77){goToPrev($comments);}});function goToNext($els){var scrollTop=$w.scrollTop()+5;$els.each(function(){var offsetTop=$(this).offset().top;if(offsetTop>scrollTop){$('html,body').animate({scrollTop:offsetTop});return false;}});}function goToPrev($els){var scrollTop=$w.scrollTop()-5;var prev;$els.each(function(){var offsetTop=$(this).offset().top;if(offsetTop>=scrollTop){return false;}prev=offsetTop;});$('html,body').animate({scrollTop:prev});}})(jQuery,window);})();

@willmorgan
Copy link

This bookmarklet appears to work:

javascript:(function(e,t,n){var r=e(".js-diff-view .js-details-container");var i=e(t);var s=e("<a/>",{"class":"js-next-diff",href:"#"}).css({position:"fixed","font-size":"large",display:"block",padding:"1rem","text-transform":"uppercase",top:0,right:0}).text("Next").on("click",function(t){t.preventDefault();var n=i.scrollTop();r.each(function(){var t=e(this).offset().top;if(t>n){e("html,body").animate({scrollTop:t});return false}})});e(document.body).append(s).on("keydown",function(t){if(e(t.target).is(":input")){return true}if(t.which==74){s.click()}else if(t.which==75){var n=i.scrollTop();var o;r.each(function(){var t=e(this).offset().top;if(t>=n){return false}o=t});e("html,body").animate({scrollTop:o})}})})(jQuery,window)

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