-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcontent_script.js
239 lines (194 loc) · 5.33 KB
/
content_script.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
229
230
231
232
233
234
235
236
237
238
239
//Global variables
var destinations = {}
var targets = {}
var links = []
var timer
// List of domains this extension should be disabled on
var ignoredDomains = [
/docs\.google\.com/
]
// Resets the timer and causes populateDestinations() to be called after a delay
function resetDestinations(){
destinations = {}
if(timer){
clearTimeout(timer)
}
timer = setTimeout(populateDestinations, 50)
}
// Populates the list of destinations
function populateDestinations(){
// Reset the timer
if(timer){
clearTimeout(timer)
}
timer = null
// Reset the list of destinations
destinations = {
prev: null,
next: null,
up: null,
top: null
}
targets = {
prev: null,
next: null,
up: null,
top: null
}
// Search through elements in document order
links = [].concat(
Array.prototype.slice.call(document.getElementsByTagName("LINK")),
Array.prototype.slice.call(document.getElementsByTagName("A"))
)
// Determine the destinations of the rels we care about
for(var i in links){
if(!links[i].rel || !links[i].href) continue
for(var dest in destinations){
if(links[i].rel.toLowerCase() == dest){
destinations[dest] = links[i].href
if(links[i].tagName == "A") targets[dest] = links[i]
}
}
}
// Flip the links to now search bottom-up
links = links.reverse()
// Search for destinations based on link contents
for(var dest in destinations){
// Skip destinations that have already been matched to an A-link
if(destinations[dest] && targets[dest]) continue
// Track an expression matching array
var regexes = []
if(dest == "next"){
regexes.push(/\bnext\b/i)
// de
regexes.push(/\bnächste seite\b/i)
regexes.push(/\bnächste\b/i)
regexes.push(/\bnächstes\b/i)
regexes.push(/\bweiter\b/i)
// ru (\b does not support unicode though regexp does)
regexes.push(/вперёд/i)
regexes.push(/следующий/i)
regexes.push(/следующая/i)
regexes.push(/след/i)
regexes.push(/позже/i)
// ua
regexes.push(/вперед/i)
regexes.push(/наступна/i)
regexes.push(/пізніше/i)
regexes.push(/→/i)
regexes.push(/»/i)
// i.mdi-navigation-chevron-right
}
if(dest == "prev"){
regexes.push(/\bprev(ious)?\b/i)
// de
regexes.push(/\bvorherige seite\b/i)
regexes.push(/\bvorherige\b/i)
regexes.push(/\bzurück\b/i)
// ru
regexes.push(/назад/i)
regexes.push(/предыдущий/i)
regexes.push(/предыдущая/i)
regexes.push(/пред/i)
regexes.push(/раньше/i)
// ua
regexes.push(/назад/i)
regexes.push(/попередня/i)
regexes.push(/раніше/i)
regexes.push(/←/i)
regexes.push(/«/i)
}
if(dest == "up"){
regexes.push(/\bup\b/i)
}
if(dest == "top"){
regexes.push(/\bhome\b/i)
}
// Attempt to match links based on text or class name (first match wins)
(function(){
for(var r in regexes){
for(var i in links){
var text = links[i].innerText.trim()
var className = links[i].className.trim()
if(
links[i].href
&& (text.match(regexes[r]) || className.match(regexes[r]))
){
destinations[dest] = links[i].href
targets[dest] = links[i]
return // Breaks 2 loops
}
}
}
})()
}
}
// Keyboard event handler
function keyListener(e){
// Assume windows settings by default
var cmdKey = e.ctrlKey
var ignoreKey = e.altKey
// Detect the shortcut sequence based on OS
if(navigator.platform.match(/^Mac/)){
cmdKey = e.altKey
ignoreKey = e.ctrlKey
}
// Ignore anything with ignored key in it
if(e.ignoreKey) return
// Repopulate if we don't yet exist
if(!destinations) populateDestinations()
var action = null
// Key + Left
if(cmdKey && !e.shiftKey && e.keyCode == 37 && destinations.prev){
action = "prev"
}
// Key + Right
if(cmdKey && !e.shiftKey && e.keyCode == 39 && destinations.next){
action = "next"
}
// Key + Up
if(cmdKey && !e.shiftKey && e.keyCode == 38 && destinations.up){
action = "up"
}
// Key + Shift + Up
if(cmdKey && e.shiftKey && e.keyCode == 38 && destinations.top){
action = "top"
}
// Space (when scrolled to the bottom of the window)
if(!cmdKey && !e.shiftKey && e.keyCode == 32 && e.srcElement == document.body && destinations.next){
if(document.body.scrollHeight - document.body.scrollTop - document.documentElement.clientHeight <= 0){
action = "next"
}
}
// Shift + Space (when scrolled to the top of the window)
if(!cmdKey && e.shiftKey && e.keyCode == 32 && e.srcElement == document.body && destinations.prev){
if(!document.body.scrollTop) action = "prev"
}
if(!action || e.srcElement.tagName == "INPUT" || e.srcElement.tagName == "TEXTAREA") return
// Cancel the event's default action
e.preventDefault()
// Navigate or click the link
if(targets[action]){
targets[action].click()
}else{
window.location = destinations[action]
}
}
// The key listening event should only be bound to the top window
if (window == top) {
// Check the ignored domain list
var bindEvents = true
for (var i = 0; i < ignoredDomains.length; i++){
if(window.location.hostname.match(ignoredDomains[i])){
bindEvents = false
break
}
}
// Bind the key handlers
if(bindEvents){
document.addEventListener('DOMSubtreeModified', resetDestinations)
window.addEventListener('keydown', keyListener, false)
}
}
// Initial population
resetDestinations()