-
Notifications
You must be signed in to change notification settings - Fork 1
/
xkcd - tweaks for text and nav.user.js
151 lines (128 loc) · 5.07 KB
/
xkcd - tweaks for text and nav.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// ==UserScript==
// @name xkcd - tweaks for text and navigation
// @namespace https://github.com/adamhotep/userscripts
// @description Hidden text below images, archive dates, keyboard navigation
// @author Adam Katz
// @include http://xkcd.com/*
// @include http://www.xkcd.com/*
// @include https://xkcd.com/*
// @include https://www.xkcd.com/*
// @grant GM_addStyle
// @version 1.4.2.20190121
// @license AGPL
// ==/UserScript==
/*
* Features:
* - Keyboard navigation:
* - left arrow: previous comic
* - right arrow: next comic
* - enter: view rollover text (like hovring the mouse over the comic)
* - Archive listing shows publication dates and what you've viewed so far
*
* Copyright (C) 2009+ Adam Katz
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at your
* option) any later version. This program is distributed in the hope that
* it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License at <http://www.gnu.org/licenses>.
*/
// This was inspired by riddle's "xkcd titles" script from way back in the day.
// https://userscripts-mirror.org/scripts/show/6080
// My version is a little safer, it'll cover multiple images (which may or may
// not create inappropriate spoilers...), and if your internet connection is
// congested (your tubes are clogged), it ensures the title isn't printed
// before the image is drawn (which is the main reason I rewrote it).
/// define GM_addStyle if necessary {{{
if (typeof GM_addStyle == 'undefined') {
function GM_addStyle(css) {
'use strict';
let head = document.head;
if (head) {
let style = document.createElement("style");
style.type = "text/css";
style.textContent = css;
head.appendChild(style);
return style;
}
return null;
}
} // end GM_addStyle }}}
var css = "";
var comic_imgs = document.querySelectorAll(`#comic img[title]`);
if (comic_imgs) {
document.body.classList.add("notxt");
// add rollover text
for (let i = 0, il = comic_imgs.length; i < il; i++) {
let comicText = document.createElement("div");
comicText.appendChild(document.createTextNode(comic_imgs[i].title))
comicText.classList.add("comicText");
comic_imgs[i].removeAttribute("title");
comic_imgs[i].parentNode.insertBefore(comicText, comic_imgs[i].nextSibling);
// roll over the image to get the text inserted below the image
// no cheating: can't be triggered until image loads (dude, nice modem!)
comic_imgs[i].addEventListener("load", function() {
this.onmouseover = function() {
document.body.classList.remove("notxt");
this.onmouseover = null;
};
}, true);
}
let prev = document.querySelector(`a[rel="prev"][href]`);
let next = document.querySelector(`a[rel="next"][href]`);
if (prev && next) {
function horizontal_scroll() {
let wide;
try { // using try-catch because this might become locked down for privacy
wide = getComputedStyle(document.querySelector(`#comic`));
if (wide) { wide = wide.width.match(/^[0-9]+/); }
if (wide) { wide = (wide > window.innerWidth); }
} catch(error) {
// iirc, browsers merely lie about this now (hopefully it's good enough)
console.log("xkcd tweaks: privacy protections may bar computing width. "
+ "Exact error:\n" + error);
wide = false;
}
return wide;
}
// keyboard navigation
let onKeyDown = function(event) {
// https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key
switch (event.key) {
case "ArrowLeft":
if (! horizontal_scroll() ) location.href = prev.href;
break;
case "ArrowRight":
if (! horizontal_scroll() ) location.href = next.href;
break;
case "c":
case "?":
case "Enter":
case "Escape":
document.body.classList.toggle("notxt");
break;
}
};
window.addEventListener("keydown", onKeyDown, false);
};
css += /* syn=css */ `
.comicText { color:gray; padding:1ex; letter-spacing:-0.01em;
font:small-caps 1.4rem
"Comic Sans MS", "Comic Neue", sans-serif; }
.notxt .comicText { display:none; }
`;
}
// show release dates and color visited links in archive
if (location.pathname.indexOf("/archive/") == 0) {
css += /* syn=css */ `
a[title^="20"]::before { content:attr(title); float:left; margin-left:5em; }
a[title^="20"]:visited { color:#b9c!important }
a[title^="20"]:hover { color:#36c!important }
a[title^="20"]:visited:hover { color:#63c!important }
`;
}
if (css) {
GM_addStyle(css);
}