Commit | Line | Data |
---|---|---|
97ba7df0 AIL |
1 | /* xkcd keyboard nav - Navigate xkcd with your keyboard |
2 | * | |
3 | * Copyright (c) 2015 Adrian Iain Lam <adrianiainlam@gmail.com> | |
4 | * | |
5 | * This program is free software. It comes without any warranty, to | |
6 | * the extent permitted by applicable law. You can redistribute it | |
7 | * and/or modify it under the terms of the Do What The Fuck You Want | |
8 | * To Public License, Version 2, as published by Sam Hocevar. See | |
9 | * http://www.wtfpl.net/txt/copying/ for more details. | |
10 | * | |
11 | * +---------------+----------------+ | |
12 | * | Key | Navigates to | | |
13 | * +---------------+----------------+ | |
14 | * | [right arrow] | next comic | | |
15 | * | [left arrow] | previous comic | | |
16 | * | [f] | first comic | | |
17 | * | [l] | last comic | | |
18 | * | [r] | random comic | | |
19 | * +---------------+----------------+ | |
20 | * | |
21 | * This script uses keyboardEvent.key, which may not be supported by | |
22 | * your browser. I have no intention to write a compatible script | |
23 | * with keyboardEvent.keyCode as it is deprecated anyway. | |
24 | */ | |
25 | ||
26 | // ==UserScript== | |
27 | // @name xkcd keyboard nav | |
28 | // @namespace https://github.com/adrianiainlam | |
29 | // @description Navigate xkcd with your keyboard | |
8573fe02 AIL |
30 | // @version 1.0.0 |
31 | // @downloadURL https://gist.github.com/adrianiainlam/542dd0794a874ca31321/raw/xkcd_keyboard_nav.user.js | |
32 | // @updateURL https://gist.github.com/adrianiainlam/542dd0794a874ca31321/raw/xkcd_keyboard_nav.user.js | |
97ba7df0 AIL |
33 | // @include /^https?://xkcd\.com(/[0-9]+)?/?$/ |
34 | // @grant none | |
35 | // ==/UserScript== | |
36 | ||
37 | function showTitleText() { | |
38 | var elem = document.getElementById("comic"); | |
39 | var child = elem.children[0]; | |
40 | var newnode = document.createElement("p"); | |
41 | if(child instanceof HTMLImageElement) { | |
42 | newnode.innerHTML = child.title; | |
43 | } else if(child instanceof HTMLAnchorElement && child.children[0] instanceof HTMLImageElement) { | |
44 | newnode.innerHTML = child.children[0].title; | |
45 | } else { | |
46 | console.log("Title text not found by this script. Please file a bug report with URL"); | |
47 | } | |
48 | elem.appendChild(newnode); | |
49 | } | |
50 | ||
51 | document.body.addEventListener("keypress", function(e) { | |
52 | var navList = document.getElementsByClassName("comicNav")[0].children; | |
53 | if(!e.altKey && !e.ctrlKey && !e.metaKey && !e.shiftKey) { | |
54 | switch(e.key) { | |
55 | case "ArrowLeft": // prev | |
56 | case "Left": // deprecated | |
57 | document.location.href = navList[1].firstChild.getAttribute("href"); | |
58 | break; | |
59 | case "ArrowRight": // next | |
60 | case "Right": // deprecated | |
61 | document.location.href = navList[3].firstChild.getAttribute("href"); | |
62 | break; | |
63 | case "r": // random | |
64 | document.location.href = navList[2].firstChild.getAttribute("href"); | |
65 | break; | |
66 | case "f": // first | |
67 | document.location.href = navList[0].firstChild.getAttribute("href"); | |
68 | break; | |
69 | case "l": // last | |
70 | document.location.href = navList[4].firstChild.getAttribute("href"); | |
71 | break; | |
72 | } | |
73 | } | |
74 | }); | |
75 | ||
76 | showTitleText(); |