/
lobster-keys.js
228 lines (225 loc) · 6.6 KB
/
lobster-keys.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// ==UserScript==
// @name Lobster Keys
// @description Keybindings for lobste.rs
// @namespace https://rodaine.com
// @source https://github.com/rodaine/lobster-keys.git
// @version 0.1.0
// @author Chris Roche <github@rodaine.com>
// @match https://lobste.rs/*
// @grant none
// @noframes
// ==/UserScript==
/******/ (() => { // webpackBootstrap
/******/ "use strict";
var __webpack_exports__ = {};
/**
* Story element on the document.
*/
class Story {
constructor(el) {
this._el = el;
}
/**
* Extracts all stories in-order from `d`.
*
* @param d Document
*/
static FromDoc(d = window.document) {
const els = d.getElementsByClassName('story');
const out = [];
for (const el of els) {
out.push(new Story(el));
}
return out;
}
/**
* Selects this story, making the keybindings apply to this Story. If a
* different story was previously seelcted, that story should be
* unfocused first.
*/
focus() {
this._el.classList.add(Story.focusCls);
const url = this.getAnchor(".u-url" /* URL */);
if (url) {
url.focus();
url.blur();
}
this.scrollIntoView();
}
/**
* Deselects this story.
*/
unfocus() {
this._el.classList.remove(Story.focusCls);
}
/**
* Opens the Story's URL
*/
open() { this.click(".u-url" /* URL */); }
/**
* Opens the domain search page for the Story's hostname
*/
domain() { this.click(".domain" /* Domain */); }
/**
* Opens the Story's author's profile page.
*/
author() { this.click(".u-author" /* Author */); }
/**
* Opens the flag dropdown menu, and moves focus to the first option.
*/
flag() {
this.click(".flagger" /* Flag */);
const opts = window.document.querySelector('#downvote_why a');
opts && opts.focus();
}
/**
* Toggles whether or not the Story is hidden.
*/
hide() { this.click(".hider" /* Hide */); }
/**
* Toggles whether or not the Story is saved.
*/
save() { this.click(".saver" /* Save */); }
/**
* Opens the Story's comments page.
*/
comments() { this.click(".comments_label a" /* Comments */); }
/**
* Toggles the Story's upvote arrow.
*/
upvote() { this.click(".upvoter" /* Upvote */); }
getAnchor(a) {
return this._el.querySelector(a);
}
click(a) {
const anchor = this.getAnchor(a);
anchor && anchor.click();
}
scrollIntoView() {
const bound = this._el.getBoundingClientRect();
if (bound.top < 0
|| bound.left < 0
|| bound.bottom > (window.innerHeight || document.documentElement.clientHeight)
|| bound.right > (window.innerWidth || document.documentElement.clientWidth)) {
return this._el.scrollIntoView(Story.scrollOpts);
}
}
}
/**
* The class name applied to the selected [[Story]]
*/
Story.focusCls = 'lobster-keys-focus';
Story.scrollOpts = { block: "nearest" };
/**
* Interaction controller for the keybindings
*/
class LobstersKeyController {
/**
* Attaches a controller to `d`, listening for events and injecting styles
* if a [[Story]] list is detected.
*
* @param d Document
*/
constructor(d = window.document) {
this.stories = Story.FromDoc(d);
if (this.stories.length > 0) {
document.addEventListener('keyup', (e) => { this.handleKeyUp(e); });
this.attachStyles(d);
}
}
get index() { return this._idx; }
set index(i) {
if (i === undefined) {
this._idx = undefined;
return;
}
if (i < 0) {
i = 0;
}
else if (i >= this.stories.length) {
i = this.stories.length - 1;
}
this._idx = i;
}
get story() {
const i = this.index;
return i === undefined ? undefined : this.stories[i];
}
changeStory(d) {
if (this.index === undefined) {
switch (d) {
case 1 /* Next */:
this.index = 0;
break;
case 0 /* Previous */:
this.index = this.stories.length - 1;
break;
}
}
else {
this.story && this.story.unfocus();
switch (d) {
case 1 /* Next */:
this.index++;
break;
case 0 /* Previous */:
this.index--;
break;
}
}
this.story && this.story.focus();
}
changePage(d) {
switch (d) {
case 0 /* Previous */:
const prev = window.document.querySelector('.morelink a:first-child');
prev && prev.innerText.indexOf('<<') > -1 && prev.click();
break;
case 1 /* Next */:
const next = window.document.querySelector('.morelink a:last-child');
next && next.innerText.indexOf('>>') > -1 && next.click();
break;
}
}
handleKeyUp(e) {
switch (e.code) {
case "KeyJ" /* J */:
return this.changeStory(1 /* Next */);
case "KeyK" /* K */:
return this.changeStory(0 /* Previous */);
case "BracketLeft" /* OpenBracket */:
return this.changePage(0 /* Previous */);
case "BracketRight" /* CloseBracket */:
return this.changePage(1 /* Next */);
}
const story = this.story;
if (story) {
switch (e.code) {
case "Enter" /* Enter */:
return story.open();
case "KeyA" /* A */:
return story.author();
case "KeyC" /* C */:
return story.comments();
case "KeyD" /* D */:
return story.domain();
case "KeyF" /* F */:
return story.flag();
case "KeyH" /* H */:
return story.hide();
case "KeyS" /* S */:
return story.save();
case "KeyU" /* U */:
return story.upvote();
}
}
}
attachStyles(d) {
const styles = d.createElement('style');
styles.innerHTML = `.${Story.focusCls} { background-color: #fffcd799; }`;
d.body.appendChild(styles);
}
}
new LobstersKeyController(window.document);
/******/ })()
;