Instantly share code, notes, and snippets.
xkcd keyboard nav - Navigate xkcd with your keyboard
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
/* xkcd keyboard nav - Navigate xkcd with your keyboard | |
* | |
* Copyright (c) 2015-2019 Adrian Iain Lam | |
* <spam@adrianiainlam.tk> s/spam/me/ | |
* | |
* This program is free software. It comes without any warranty, to | |
* the extent permitted by applicable law. You can redistribute it | |
* and/or modify it under the terms of the Do What The Fuck You Want | |
* To Public License, Version 2, as published by Sam Hocevar. See | |
* http://www.wtfpl.net/txt/copying/ for more details. | |
* | |
* +---------------+----------------+ | |
* | Key | Navigates to | | |
* +---------------+----------------+ | |
* | [right arrow] | next comic | | |
* | [left arrow] | previous comic | | |
* | [f] | first comic | | |
* | [l] | last comic | | |
* | [r] | random comic | | |
* +---------------+----------------+ | |
*/ | |
// ==UserScript== | |
// @name xkcd keyboard nav | |
// @namespace https://github.com/adrianiainlam | |
// @description Navigate xkcd with your keyboard | |
// @version 1.0.5 | |
// @downloadURL https://gist.github.com/adrianiainlam/542dd0794a874ca31321/raw/xkcd_keyboard_nav.user.js | |
// @updateURL https://gist.github.com/adrianiainlam/542dd0794a874ca31321/raw/xkcd_keyboard_nav.user.js | |
// @include /^https?://xkcd\.com(/[0-9]+)?/?$/ | |
// @grant none | |
// ==/UserScript== | |
function showTitleText() { | |
var elem = document.getElementById("comic"); | |
var child = elem.children[0]; | |
var newnode = document.createElement("p"); | |
if(child instanceof HTMLImageElement) { | |
newnode.appendChild(document.createTextNode(child.title)); | |
} else if(child instanceof HTMLAnchorElement && child.children[0] instanceof HTMLImageElement) { | |
newnode.appendChild(document.createTextNode(child.children[0].title)); | |
} else { | |
console.log("Title text not found by this script. Please file a bug report with URL"); | |
} | |
elem.appendChild(newnode); | |
} | |
document.body.addEventListener("keyup", function(e) { | |
if (!(e.target instanceof HTMLBodyElement)) { | |
return; | |
} | |
var navList = document.getElementsByClassName("comicNav")[0].children; | |
if(!e.altKey && !e.ctrlKey && !e.metaKey && !e.shiftKey) { | |
switch(e.key) { | |
case "ArrowLeft": // prev | |
document.location.href = navList[1].firstChild.getAttribute("href"); | |
break; | |
case "ArrowRight": // next | |
document.location.href = navList[3].firstChild.getAttribute("href"); | |
break; | |
case "r": // random | |
document.location.href = navList[2].firstChild.getAttribute("href"); | |
break; | |
case "f": // first | |
document.location.href = navList[0].firstChild.getAttribute("href"); | |
break; | |
case "l": // last | |
document.location.href = navList[4].firstChild.getAttribute("href"); | |
break; | |
} | |
} | |
}); | |
showTitleText(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment