I’ve long wished for Google Reader style hotkeys (hitting “j” to move to the next item, “k” to go to the previous) to be available in more places. In particular, pages with many images are screaming for it – it’s just silly to keep messing with the scroll bar to get each image to line up properly in the viewable portion of the browser. If you don’t know what I mean go to any of these pages with lots of images and try to view each image. If you’re not frustrated with the scroll bar by the 3rd image you’re far more patient than I.
I finally found a few minutes to hack at it, and sure enough, it was quite easy to do with GreaseMonkey and jQuery.
The script does a very simple thing – it brings the next image to the top of the browser window so you can see it. You hit “j” to see the next image and “k” to go back to the previous.
Install GreaseMonkey and then install ScrollMonkey by clicking on it.
Once installed, go to any of the above pages with images, click on the page (to ensure the page is in focus), and hit the “j” key a couple of times until the first picture is at the top of the browser window. Now hit “j” again to go to the next image and “k” to go back.
The script is very simple and brain dead – I’ve made no attempt to make it intelligent. In particular, it’s active on every page and it downloads jQuery, which is probably more overhead than you want to have. In my case I typically have GreaseMonkey turned off and only turn it on when I need it, so it’s not much of a problem. It also looks at all images instead of attempting to find the large images of interest.
It would also be smart to generalize it to deal with anything – paragraphs, headings, etc – instead of just images.
And it’d be probably be nice to have it turn on via a hotkey instead of always being on, and it probably will interfere with any forms you’ll try to fill out.
But practically speaking none of these are problems I run into, so I’ll leave them for now and fix them if there’s any demand for it.
Give it a try and let me know what you think.
Here’s the code in its entirety, for those interested:
// Add jQuery
var GM_JQ = document.createElement('script');
GM_JQ.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js';
GM_JQ.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(GM_JQ);
// Check if jQuery's loaded
function GM_wait() {
if(typeof unsafeWindow.jQuery == 'undefined') { window.setTimeout(GM_wait,100); }
else { $ = unsafeWindow.jQuery; run(); }
}
GM_wait();
function run() {
var images = $('img'),
idx = -1,
nimages = images.length;
$(document).keypress(function (e) {
// j == 106 ; k == 107
if (e.which != 106 && e.which != 107) { return; }
if (e.which == 106 && idx < nimages-1) { idx++; }
if (e.which == 107 && idx > 0) { idx--; }
var top = $(images[idx]).offset().top;
unsafeWindow.scrollTo(0, top);
} );
}