From 97ba7df024b6e35491ac5a9eb8c1c4a171825e9f Mon Sep 17 00:00:00 2001 From: Adrian Iain Lam Date: Sat, 7 Mar 2015 04:55:50 -0800 Subject: [PATCH] --- xkcd_keyboard_nav.user.js | 76 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 xkcd_keyboard_nav.user.js diff --git a/xkcd_keyboard_nav.user.js b/xkcd_keyboard_nav.user.js new file mode 100644 index 0000000..f574bbf --- /dev/null +++ b/xkcd_keyboard_nav.user.js @@ -0,0 +1,76 @@ +/* xkcd keyboard nav - Navigate xkcd with your keyboard + * + * Copyright (c) 2015 Adrian Iain Lam + * + * 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 | + * +---------------+----------------+ + * + * This script uses keyboardEvent.key, which may not be supported by + * your browser. I have no intention to write a compatible script + * with keyboardEvent.keyCode as it is deprecated anyway. + */ + +// ==UserScript== +// @name xkcd keyboard nav +// @namespace https://github.com/adrianiainlam +// @description Navigate xkcd with your keyboard +// @version 1.0.-1 +// @downloadURL +// @updateURL +// @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.innerHTML = child.title; + } else if(child instanceof HTMLAnchorElement && child.children[0] instanceof HTMLImageElement) { + newnode.innerHTML = 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("keypress", function(e) { + var navList = document.getElementsByClassName("comicNav")[0].children; + if(!e.altKey && !e.ctrlKey && !e.metaKey && !e.shiftKey) { + switch(e.key) { + case "ArrowLeft": // prev + case "Left": // deprecated + document.location.href = navList[1].firstChild.getAttribute("href"); + break; + case "ArrowRight": // next + case "Right": // deprecated + 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(); -- 2.7.4