-
Notifications
You must be signed in to change notification settings - Fork 0
/
stutter_scroll.js
388 lines (298 loc) · 13.7 KB
/
stutter_scroll.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
const SCRIPT_NAME = "StutterScroll";
const PAGE_LOAD_TIMEOUT_SECONDS = 15;
// const DEBUG_LOG_TO_CONSOLE = true;
const DEBUG_LOG_TO_CONSOLE = false;
// global parameters with working defaults...
let active = false;
let initial_pause = 5;
let stutter_pause = 6;
let stutter_class = 'stutter_scroll_stop_here';
// let url_list = 'https://ontheday.net/\nhttps://google.com/';
// let url_list = 'http://127.0.0.1:8000/2018/bay_climb/tournament/open_men/\nhttp://127.0.0.1:8000/2018/bay_climb/otd_ftw/\nhttp://127.0.0.1:8000/2018/bay_climb/tournament/men_fixed/\nhttp://127.0.0.1:8000/2018/bay_climb/otd_ftw/\nhttp://127.0.0.1:8000/2018/bay_climb/tournament/open_women/\nhttp://127.0.0.1:8000/2018/bay_climb/otd_ftw/\nhttp://127.0.0.1:8000/2018/bay_climb/tournament/women_fixed/\nhttp://127.0.0.1:8000/2018/bay_climb/otd_ftw/';
let url_list = 'https://ontheday.net/2018/bay_climb/\nhttps://ontheday.net/2018/bay_climb/otd_ftw/\nhttps://ontheday.net/2018/bay_climb/tournament/open_men/\nhttps://ontheday.net/2018/bay_climb/otd_ftw/\nhttps://ontheday.net/2018/bay_climb/tournament/open_women/\nhttps://ontheday.net/2018/bay_climb/otd_ftw/\nhttps://ontheday.net/2018/bay_climb/tournament/men_fixed/\nhttps://ontheday.net/2018/bay_climb/otd_ftw/\nhttps://ontheday.net/2018/bay_climb/tournament/women_fixed/\nhttps://ontheday.net/2018/bay_climb/otd_ftw/'
let split_urls = 'tbd';
let url_index = 0;
let stutter_index = 0;
let stutter_id_list = [];
let initial_tab_id = null;
let activeTimeout = null;
const WAIT_TIME_INCREMENT = 250;
let requiredWaitTime = null;
let elapsedWaitTime = null;
function debug_log(message = '') {
if (DEBUG_LOG_TO_CONSOLE) {
// from here: https://stackoverflow.com/questions/10211145/getting-current-date-and-time-in-javascript
// let timestamp = new Date().toLocaleString();
let timestamp = new Date().toLocaleTimeString();
let output_list = [timestamp, SCRIPT_NAME, message];
console.log(output_list.join(': '));
}
}
function testForValidTab(url) {
// allows starting from a new tab...
const NEW_TAB_STRING = "about:newtab";
const APPLICABLE_PROTOCOLS = ["http:", "https:"];
if (url === NEW_TAB_STRING ) {
return true;
}
else {
let anchor = document.createElement('a');
anchor.href = url;
return APPLICABLE_PROTOCOLS.includes(anchor.protocol);
}
}
function setDormantState() {
debug_log('stopping');
active = false;
if (activeTimeout) {
clearTimeout(activeTimeout);
}
browser.browserAction.setIcon({path: "icons/stutter_scroll.svg"});
browser.browserAction.setTitle({title: "Start Stutter Scroll"});
}
function toggleStutter() {
debug_log('++++++++++ toggleStutter ++++++++++');
if (active) {
setDormantState();
}
else {
// only proceed if we are on a regular tab...
let gettingTab = browser.tabs.query({active: true});
gettingTab.then((tab_list) => {
let starting_tab = tab_list[0];
function gettingStarted(this_tab) {
initial_tab = this_tab;
initial_tab_id = initial_tab.id;
active = true;
debug_log('starting');
startStutter();
browser.browserAction.setIcon({path: "icons/stutter_scroll_disable.svg"});
browser.browserAction.setTitle({title: "Stop Stutter Scroll"});
}
// open a new tab if required
if (testForValidTab(starting_tab.url)) {
gettingStarted(starting_tab);
}
else {
let creatingTab = browser.tabs.create({active: true});
creatingTab.then((created_tab) => {
gettingStarted(created_tab);
})
}
});
}
}
function startStutter() {
debug_log('startStutter: ' + active);
if (active) {
// get parameter
let gettingItem = browser.storage.sync.get('stutter_class');
gettingItem.then((res) => {
stutter_class = res.stutter_class || stutter_class;
debug_log('stutter_class: ' + stutter_class);
determinePages();
});
}
}
function determinePages() {
// this routine recovers the page list and triggers the loading of the first page...
debug_log('determinePages: ' + active);
if (active) {
// get parameter
let gettingItem = browser.storage.sync.get('url_list');
gettingItem.then((res) => {
url_list = res.url_list || url_list;
split_urls = url_list.split('\n');
debug_log('url_list: ' + url_list);
debug_log(split_urls);
// explicitly sets up loading the first page in the url_list...
url_index = 0;
loadPage();
});
}
}
function loadPage() {
// todo: this routine is over-indented... fix at some point.
debug_log('loadPage: ' + active);
let gettingTab = browser.tabs.query({active: true});
gettingTab.then((tab) => {
let current_tab = tab[0];
if (current_tab.id !== initial_tab_id) {
setDormantState();
return;
}
if (active) {
// if we have reached the end of the list, go back and restart...
if (url_index >= split_urls.length) {
determinePages();
return;
}
// select the page to load...
let page_url = split_urls[url_index];
debug_log('page_url: ' + page_url);
// increment the url_index for next time...
url_index++;
// cases where we move on are:
// - zero length url
// - we are already on the requested page
//
// todo: need to validate the urls somewhere...
// if (page_url.length === 0 || page_url === current_tab.url) {
// loadPage();
// return;
// }
if (page_url.length === 0 ) {
// setDormantState();
loadPage();
return;
}
let updatedTab = browser.tabs.update({url: page_url});
updatedTab
.then((tab) => {
// cribbed from here [Erik Rose]: https://bugzilla.mozilla.org/show_bug.cgi?id=1397667
//
// This appears necessary as the script blocks page loading until later.
// Therefore, any page inspection will be on the already loaded page.
//
// Solution: add a Promise that contains a listener for onUpdated event.
//
// todo: thank Erik Rose for the code!
if (DEBUG_LOG_TO_CONSOLE) {
console.log(tab);
}
function isComplete(tab) {
return tab.status === 'complete' && tab.url === page_url;
}
// if (!isComplete(tab)) {
// return new Promise((resolve, reject) => {
// const waitForPageLoadTimeout = setTimeout(
// function giveUp() {
// browser.tabs.onUpdated.removeListener(onUpdated);
// reject(new Error('Tab never reached the "complete" state, just ' + tab.status + ' on ' + tab.url));
// setDormantState();
// },
// PAGE_LOAD_TIMEOUT_SECONDS * 1000
// );
//
// function onUpdated(tabId, changeInfo, updatedTab) {
// // Must use updatedTab below; using just `tab` seems to remain
// // stuck to about:blank.
// if (tabId === updatedTab.id && isComplete(updatedTab)) {
// clearTimeout(waitForPageLoadTimeout);
// browser.tabs.onUpdated.removeListener(onUpdated);
// resolve(updatedTab);
// }
// }
// browser.tabs.onUpdated.addListener(onUpdated);
// });
// }
// sets listener for the page load...
return new Promise((resolve, reject) => {
const waitForPageLoadTimeout = setTimeout(
function giveUp() {
browser.tabs.onUpdated.removeListener(onUpdated);
reject(new Error('Tab never reached the "complete" state, just ' + tab.status + ' on ' + tab.url));
setDormantState();
},
PAGE_LOAD_TIMEOUT_SECONDS * 1000
);
function onUpdated(tabId, changeInfo, updatedTab) {
// Must use updatedTab below; using just `tab` seems to remain
// stuck to about:blank.
if (tabId === updatedTab.id && isComplete(updatedTab)) {
clearTimeout(waitForPageLoadTimeout);
browser.tabs.onUpdated.removeListener(onUpdated);
resolve(updatedTab);
}
}
browser.tabs.onUpdated.addListener(onUpdated);
});
})
.then((tab) => {
if (DEBUG_LOG_TO_CONSOLE) {
console.log(tab);
}
// get parameter
let gettingItem = browser.storage.sync.get('initial_pause');
gettingItem.then((res) => {
initial_pause = res.initial_pause || initial_pause;
chrome.tabs.sendMessage(current_tab.id, {stutter_class: stutter_class}, function (response) {
stutter_id_list = response.id_list;
if (DEBUG_LOG_TO_CONSOLE) {
console.log(stutter_id_list);
}
// explicitly indicate that we are at the start of the page...
stutter_index = 0;
// debug_log('Calling stutterPage - timeout: ' + initial_pause);
// activeTimeout = setTimeout(stutterPage, initial_pause * 1000);
//
debug_log('Calling waitForSpecifiedTime - timeout (s): ' + initial_pause);
elapsedWaitTime = 0;
requiredWaitTime = initial_pause * 1000;
activeTimeout = setTimeout(waitForSpecifiedTime, WAIT_TIME_INCREMENT);
});
});
});
}
});
}
function waitForSpecifiedTime() {
debug_log('waitForSpecifiedTime: ' + active + ', wait time (ms): ' + requiredWaitTime);
let gettingTab = browser.tabs.query({active: true});
gettingTab.then((tab) => {
let current_tab = tab[0];
if (current_tab.id !== initial_tab_id) {
setDormantState();
return;
}
if (active) {
elapsedWaitTime += WAIT_TIME_INCREMENT;
if (elapsedWaitTime >= requiredWaitTime) {
debug_log('Wait complete - scroll the page... (ms) ' + elapsedWaitTime);
stutterPage();
}
else {
// continue waiting...
debug_log('Continue waiting... (ms) ' + elapsedWaitTime);
activeTimeout = setTimeout(waitForSpecifiedTime, WAIT_TIME_INCREMENT);
}
}
});
}
function stutterPage() {
debug_log('stutterPage: ' + active);
let gettingTab = browser.tabs.query({active: true});
gettingTab.then((tab) => {
let current_tab = tab[0];
if (current_tab.id !== initial_tab_id) {
setDormantState();
return;
}
if (active) {
// if we are at the end of the stuttering on this page, move to the next page...
if (stutter_index >= stutter_id_list.length) {
loadPage();
return;
}
debug_log('stutter_index: ' + stutter_index + ' in ' + stutter_id_list.length);
debug_log(stutter_id_list[stutter_index]);
// send message to the scroll to the current stutter id...
chrome.tabs.sendMessage(current_tab.id, {move_to_stutter_id: stutter_id_list[stutter_index]}, function (response) {});
stutter_index++;
// get parameter
let gettingItem = browser.storage.sync.get('stutter_pause');
gettingItem.then((res) => {
stutter_pause = res.stutter_pause || stutter_pause;
// debug_log('Calling stutterPage - timeout: ' + stutter_pause);
// activeTimeout = setTimeout(stutterPage, stutter_pause * 1000);
//
debug_log('Calling waitForSpecifiedTime - timeout (s): ' + stutter_pause);
elapsedWaitTime = 0;
requiredWaitTime = stutter_pause * 1000;
activeTimeout = setTimeout(waitForSpecifiedTime, WAIT_TIME_INCREMENT);
});
}
});
}
debug_log('stutter_scroll.js: adding listener...');
browser.browserAction.onClicked.addListener(toggleStutter);