Skip to content

Instantly share code, notes, and snippets.

@markhepburn
Created July 24, 2010 08:35
Show Gist options
  • Save markhepburn/488542 to your computer and use it in GitHub Desktop.
Save markhepburn/488542 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name PhD Comics Keyboard Navigation
// @namespace http://www.everythingtastesbetterwithchilli.com
// @description Use arrow keys to navigate through the phdcomics archive.
// @include http://*phdcomics.com/comics/*
// ==/UserScript==
function findHrefForParentOf(xpth) {
var imgIterator = document.evaluate(
xpth,
document,
null,
XPathResult.UNORDERED_NODE_ITERATOR_TYPE,
null);
var img = imgIterator.iterateNext();
return img ? img.parentNode.href : null;
}
var NEXTURL = findHrefForParentOf('//img[@src="images/next_button.gif"]');
var PREVURL = findHrefForParentOf('//img[@src="images/prev_button.gif"]');
function handleKeyEvent(e) {
var evt = e || window.event;
var code = evt.keyCode;
// next: 'l' (76), 'j' (74), or right arrow key (39):
if (code === 76 || code === 74 || code === 39) {
if (NEXTURL) {
window.location = NEXTURL;
}
}
// previous: 'h' (72), 'k' (75), or left arrow key (37):
else if (code === 72 || code === 75 || code === 37) {
if (PREVURL) {
window.location = PREVURL;
}
}
}
window.addEventListener('keydown', handleKeyEvent, true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment