forked from kenaniah/chrome-navigation-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent_script.js
177 lines (145 loc) · 4.02 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
//Global variables
var destiations = {};
var targets = {};
var links = [];
var timer;
var prevScrollTop;
//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;
prevScrollTop = -1;
//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(/^next/i);
}
if(dest == "prev"){
regexes.push(/^prev/i);
}
if(dest == "up"){
regexes.push(/^up\b/i);
}
if(dest == "top"){
regexes.push(/^home/i);
}
//Attempt to match links based on text (first match wins)
(function(){
for(var r in regexes){
for(var i in links){
if(links[i].href && links[i].innerText.replace(/[^a-z]/i, "").trim().match(regexes[r])){
destinations[dest] = links[i].href;
targets[dest] = links[i];
return;
}
}
}
})();
}
}
//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)
//End of scroll area is detected by not changing scrollTop on consecutive space bar presses
if(!cmdKey && !e.shiftKey && e.keyCode == 32 && e.srcElement == document.body
&& destinations.next && document.body.scrollTop == prevScrollTop){
action = "next";
prevScrollTop = -1;
}
else {
prevScrollTop = document.body.scrollTop;
return;
}
//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;
//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) {
document.addEventListener('DOMSubtreeModified', resetDestinations);
window.addEventListener('keydown', keyListener, false);
}
//Initial population
resetDestinations();