-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontrol.js
275 lines (231 loc) · 5.78 KB
/
control.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
(function () {
/**********************
script modified by owq
***********************/
var interval = 20,
vertical_moment = 250,
horizontal_moment = 100,
next,
flg,
hotkeysEnabled = true,
data = {};
/*** SCROLLING STUFF ***/
function scrollFunc(moment) {
if (flg === 'vertical') {
scrollBy(0, moment);
} else if (flg === 'horizontal') {
scrollBy(moment, 0);
}
}
function scrollToTop() {
scroll(0, -document.documentElement.scrollHeight);
document.removeEventListener('keydown', vHandler, false);
document.addEventListener('keydown', initKeyBind, false);
}
function scrollToBottom() {
scroll(0, document.documentElement.scrollHeight);
}
function scrollToFirst() {
var scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
scroll(-document.documentElement.scrollWidth, scrollTop);
}
function scrollToLast() {
var scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
scroll(document.documentElement.scrollWidth, scrollTop);
}
function smoothScroll(moment) {
if (moment > 0)
moment = Math.floor(moment / 2);
else
moment = Math.ceil(moment / 2);
scrollFunc(moment);
if (Math.abs(moment) < 1) {
setTimeout(function () {
scrollFunc(moment)
});
return;
}
next = setTimeout(function () {
smoothScroll(moment);
}, interval);
}
function smoothScrollBy(moment) {
clearTimeout(next);
smoothScroll(moment);
}
function scrollDown() {
flg = 'vertical';
smoothScrollBy(vertical_moment);
}
function scrollUp() {
flg = 'vertical';
smoothScrollBy(-vertical_moment);
}
function scrollRight() {
flg = 'horizontal';
smoothScrollBy(horizontal_moment);
}
function scrollLeft() {
flg = 'horizontal';
smoothScrollBy(-horizontal_moment);
}
/*** MAIN STUFF ***/
function reload() {
location.reload();
}
function stopLoading() {
stop();
}
function closeTab() {
var port = chrome.extension.connect();
port.postMessage({
action : "closeTab"
});
}
function undoCloseTab() {
var port = chrome.extension.connect();
port.postMessage({
action : "undoCloseTab"
});
}
function previousTab() {
var port = chrome.extension.connect();
port.postMessage({
action : "previousTab"
});
}
function nextTab() {
var port = chrome.extension.connect();
port.postMessage({
action : "nextTab"
});
}
function newTab() {
var port = chrome.extension.connect();
port.postMessage({
action : "newTab"
});
}
function historyBack() {
history.back();
}
function historyForward() {
history.forward();
}
function showPA() {
chrome.extension.sendRequest({
data : "showPA"
}, function (response) {});
hotkeysEnabled = true;
}
function hidePA() {
chrome.extension.sendRequest({
data : "hidePA"
}, function (response) {});
hotkeysEnabled = false;
}
function blockPA() {
chrome.extension.sendRequest({
data : "blockPA"
}, function (response) {});
hotkeysEnabled = false;
}
//keybinding
//@hotkey: properties: value, enabled
function addHotkey(hotkey, func) {
if (hotkey.enabled)
shortcut.add(hotkey.value, func);
}
function removeHotkey(hotkey, func) {
//based on old data, if hotkey wasn't enabled it wouldn't be added in the first place.
//so this should be safe.
if (hotkey.enabled)
shortcut.remove(hotkey.value);
}
function makeDefaultHotkeys() {
//create new structure using defaults
function Hotkey() {
this.value = "";
this.enabled = false;
}
var hotkeys = {};
for (var i in defaultHotkeys) {
hotkeys[i] = new Hotkey();
hotkeys[i].value = defaultHotkeys[i];
hotkeys[i].enabled = true;
}
return hotkeys;
}
function initHotkeys(hotkeys, blocklist) {
if (blocklist) {
for (var i = 0; i < blocklist.length; i++) {
if (blocklist[i].length == 0) continue;
if (location.href.indexOf(blocklist[i]) >= 0) {
console.log("Hotkeys blocked.");
blockPA();
return;
}
}
}
addHotkey(hotkeys["scrollUp"], scrollUp);
addHotkey(hotkeys["scrollDown"], scrollDown);
addHotkey(hotkeys["previousTab"], previousTab);
addHotkey(hotkeys["nextTab"], nextTab);
addHotkey(hotkeys["scrollToTop"], scrollToTop);
addHotkey(hotkeys["scrollToBottom"], scrollToBottom);
addHotkey(hotkeys["historyBack"], historyBack);
addHotkey(hotkeys["historyForward"], historyForward);
addHotkey(hotkeys["reload"], reload);
addHotkey(hotkeys["stopLoading"], stopLoading);
addHotkey(hotkeys["newTab"], newTab);
addHotkey(hotkeys["closeTab"], closeTab);
addHotkey(hotkeys["undoCloseTab"], undoCloseTab);
//console.log("Hotkeys Extension running on this page.");
showPA();
}
function removeHotkeys(hotkeys) {
for (var i in hotkeys) {
removeHotkey(hotkeys[i]);
}
hidePA();
}
//start using local data
//@pre assumes data is correct
function initHotkeysLocal() {
initHotkeys(data['hotkeys'], data['blocklist']);
}
chrome.storage.onChanged.addListener(function (changes, namespace) {
//console.log(changes);
//Remove hotkeys
if(data['hotkeys'])
removeHotkeys(data['hotkeys']);
//Set new values
for (key in changes) {
data[key] = changes[key].newValue;
}
//Let's go!
initHotkeysLocal();
});
//TODO deal with new structure
chrome.extension.onConnect.addListener(function (port) {
//console.assert(port.name == "knockknock");
port.onMessage.addListener(function (msg) {
if (msg.action == "togglePA") {
if(hotkeysEnabled)
removeHotkeys(data['hotkeys']);
else
initHotkeysLocal();
}
});
});
chrome.storage.sync.get(['hotkeys', 'blocklist'], function (result) {
if(!result['hotkeys']) {
console.log("No data found. Create default hotkeys.");
chrome.storage.sync.set({'hotkeys': makeDefaultHotkeys()}, function() {});
return;
}
data = result;
initHotkeys(result['hotkeys'], result['blocklist']);
});
//end
})();