-
Notifications
You must be signed in to change notification settings - Fork 20
/
commands.js
470 lines (422 loc) · 17.2 KB
/
commands.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
'use strict';
/**
* Executes commands that need to happen in the content script on behalf of the background script.
*/
window.fg.module('commands', function (exports, fg) {
// Hash of handler functions for supported commands.
var commandHandlers = {
'goOrigin': commandGoOrigin,
'historyBack': commandHistoryBack,
'historyBackOrCloseTab': commandHistoryBackOrCloseTab,
'historyForward': commandHistoryForward,
'pageUp': commandPageUp,
'pageDown': commandPageDown,
'parentDirectory': commandParentDirectory,
'reloadFrame': commandReloadFrame,
'scrollBottom': commandScrollBottom,
'scrollDown': commandScrollDown,
'scrollTop': commandScrollTop,
'scrollUp': commandScrollUp,
'stop': commandStop,
'userScript': commandUserScript
};
// Settings for this module.
var settings = fg.helpers.initModuleSettings({
insertRelatedTab: true,
scrollDuration: 1000,
scrollAmount: 100,
useRelPrevNext: true
}, 'local');
// Promise to ensure that repeated scroll commands are executed in order.
// Note: this only works within a frame as the promise does not exist if the command bubbles.
var scrollYPromise = Promise.resolve();
// Event listeners ---------------------------------------------------------------------------------------------------
browser.runtime.onMessage.addListener((message, sender) => {
switch (message.topic) {
case 'mg-contentCommand':
// Execute a command on behalf of the background script.
// Check if the command should be handled by this frame.
if (fg.mouseEvents.scriptFrameId === message.data.context.targetFrameId) {
// Execute the delegated command in this frame.
return commandHandlers[message.data.command](message.data);
}
}
return false;
});
window.addEventListener('message', event => {
// These commands should never be dispatched from a frame above.
if ((event.source.window !== window.parent) && (event.source.window !== window.top)) {
switch (event.data.topic) {
case 'mg-commandScrollBottom':
commandScrollBottom(event.data.data);
break;
case 'mg-commandScrollDown':
commandScrollDown(event.data.data);
break;
case 'mg-commandScrollTop':
commandScrollTop(event.data.data);
break;
case 'mg-commandScrollUp':
commandScrollUp(event.data.data);
break;
}
}
});
// -------------------------------------------------------------------------------------------------------------------
// Post a message to the given window with the given topic.
// Typically used to send messages up the frame/window hierarchy.
function postTo (targetWindow, topic, data) {
targetWindow.postMessage({
topic: 'mg-' + topic,
data: data || {}
}, '*');
}
// Attempt to format the given value similarly to the original value.
function padSame (original, newValue) {
// Look for leading zeros to determine padding size.
// This will fail in some case, for example: 100 -> 99 or 099?
return (original[0] === '0') ? newValue.padStart(original.length, '0') : newValue;
}
// Modify the page number parameter in a URL.
// Tries each replacer stategy in turn until one is successful.
// JSFiddle to debug this algorithm: https://jsfiddle.net/Lrdgcxcs/1/
function alterPageNumber (callback) {
var replacers = [
// Match common pagination query parameters.
url => url.replace(
/\b(page|p)=(\d+)\b/i,
(match, p1, p2, offset) => p1 + '=' + padSame(p2, String(callback(Number(p2))))
),
// Match pageXX or page/XX in the URL.
url => url.replace(
/\b(page\/?)(\d+)\b/i,
(match, p1, p2, offset) => p1 + padSame(p2, String(callback(Number(p2))))
),
// Generic find and replace numbers in the URL.
// - Try to scan for numbers in the path from end to start.
// - Try to scan for number in the query or fragment from start to end.
url => {
// Split the URL each time a number is enountered.
let segments = url.split(/([\d]+)/);
// Find the last segment of the path component.
let lastPathSegment = segments.reduce((n, segment, i) => {
return !!~segment.indexOf('?') || !!~segment.indexOf('#') ? Math.min(n, i) : n;
}, segments.length - 1);
// Look for a number in the path first.
// Scan from end to start and increment the last number in the path.
let done = false;
for (let i = lastPathSegment; i >= 0; i--) {
let value = segments[i].length ? Number(segments[i]) : Number.NaN;
if (value >= 0) {
segments[i] = padSame(segments[i], String(callback(value)));
done = true;
break;
}
}
if (!done) {
// Look for a number in query as fallback.
// Scan from start to end and increment the first number in the query or fragment.
for (let i = lastPathSegment; i < segments.length; i++) {
let value = segments[i].length ? Number(segments[i]) : Number.NaN;
if (value >= 0) {
segments[i] = padSame(segments[i], String(callback(value)));
break;
}
}
}
// Assemble the segments.
return segments.join('');
}
];
// Ignore the origin component of the URL.
var origin = String(window.location.origin);
var noOriginPart = String(window.location.href).substring(origin.length);
for (var i = 0; i < replacers.length; i++) {
var newPart = replacers[i](noOriginPart);
if (newPart !== noOriginPart) {
window.location.href = origin + newPart;
return true;
}
}
return false;
}
// Follow the last rel=next or rel=prev link in the page.
function goRelNextPrev (next) {
let list = document.querySelectorAll(next ? 'a[rel~=next]' : 'a[rel~=prev]');
let href = list.length && list[list.length - 1].href;
if (href) {
window.location.href = href;
return true;
}
return false;
}
// Get a target from which the search for a scrollable element can begin. Returns in order: a nested frame
// identified in data.initialScrollFrameId, the mouseDown target, or the document.scrollingElement.
function getInitialScrollTarget (data) {
let state = fg.mouseEvents.state;
if (data.initialScrollFrameId) {
// Command was referred up the hierachy.
// Look for a scrollable node begining from the parent of the nested iframe.
let nestedFrame = state.nestedFrames.find(tuple => tuple.scriptFrameId === data.initialScrollFrameId);
if (nestedFrame) {
return nestedFrame.element.parentNode;
}
} else
if (state.mouseDown) {
// Can only be present for non-referred commands.
// Use the mouseDown target if available.
return state.mouseDown.target;
}
// Default to the document scrolling element.
return document.scrollingElement;
}
// Bubble the scroll command up the frame hierachy if the frame cannot scroll. The following bubble logic is
// attempting to copy native Firefox behaviour. One notable difference is that Firefox scrolls from the active
// element when you press Page Up/Down. Mouse gestures always activate the element below the mouse on mousedown
// which causes scroll commands to always focus and scroll iframes.
function bubblingScrollTarget (data, command, handler) {
if (!handler(getInitialScrollTarget(data))) {
// Failed to find an acceptable scrolling node in this frame.
if (fg.mouseEvents.state.isNested) {
// Bubble up if scrolling is disabled...
if ((fg.mouseEvents.state.frameScrolling === 'no') ||
// ..or the frame does not have scroll bars.
(document.scrollingElement.scrollHeight <= document.scrollingElement.clientHeight)
) {
// Set the initialScrollFrameId so that getInitialScrollTarget() in the parent window will target the frame.
data.initialScrollFrameId = fg.mouseEvents.scriptFrameId;
postTo(window.parent, command, data);
}
}
}
}
// Find the first scrollable node or ancestor that isn't at the top.
function findScrollingUpNode (node) {
let state = fg.mouseEvents.state;
while (node) {
// Node must not be scrolled to the top.
if ((node.scrollTop > 0) &&
// Node must not have overflow:hidden style.
(window.getComputedStyle(node).overflowY !== 'hidden') &&
// Node must not be HTML and have scrolling=no attribute.
((node.tagName !== 'HTML') || (state.frameScrolling !== 'no'))
) {
return node;
}
node = node.parentNode;
}
return null;
}
// Find the first scrollable node or ancestor that isn't at the bottom.
function findScrollingDownNode (node) {
let state = fg.mouseEvents.state;
while (node) {
// Node must not be scrolled to the bottom.
if ((node.scrollTop < node.scrollTopMax) &&
// Node must not have overflow:hidden style.
(window.getComputedStyle(node).overflowY !== 'hidden') &&
// Node must not be HTML and have scrolling=no attribute.
((node.tagName !== 'HTML') || (state.frameScrolling !== 'no'))
) {
return node;
}
node = node.parentNode;
}
return null;
}
// Function adapted from:
// https://github.com/danro/jquery-easing/blob/master/jquery.easing.js
function easeOutQuad (time, initial, change, duration) {
return -change * (time /= duration) * (time - 2) + initial;
}
// Smoothly scroll the window to the given offset using requestAnimationFrame().
function scrollYEase (node, scrollTo, duration) {
return new Promise((resolve, reject) => {
let start = window.performance.now();
let initial = (typeof node.scrollY !== 'undefined') ? node.scrollY : node.scrollTop;
let change = scrollTo - initial;
// Animation function to scroll based on easing function.
function animate (step) {
let time = (step - start);
let value = easeOutQuad(time, initial, change, duration);
if (time < duration) {
// Schedule the next animation frame.
node.scrollTo(0, value);
window.requestAnimationFrame(animate);
} else {
// Finish by scrolling to the exact amount.
node.scrollTo(0, scrollTo);
resolve();
}
}
if (duration > 0) {
// Schedule the first animation frame.
window.requestAnimationFrame(animate);
} else {
// Animation is disabled.
node.scrollTo(0, scrollTo);
resolve();
}
});
}
// Command implementations -------------------------------------------------------------------------------------------
// Navigate to the origin of the current URL.
function commandGoOrigin (data) {
window.location.href = window.location.origin;
}
// Navigate back in history.
function commandHistoryBack (data) {
window.history.back();
}
// Navigate back in history or close the tab if there are no previous states.
function commandHistoryBackOrCloseTab (data) {
let originalUrl = window.location.href;
window.history.back();
// Using a timeout to kill the current tab if the content script is not unloaded was suggested by @kafene.
// Also check if the URL changed to catch push-state on single page applications.
// If the application is lagging a short timeout could potentially close a tab with more history.
const closeTabTimeoutMs = 300;
window.setTimeout(function () {
if (originalUrl === window.location.href) {
// URL hasn't changed and script was not killed so lose the current tab.
return executeInBackground(data => {
return browser.windows.getAll().then(windows => {
// Do not close the browser; keep the tab if it is the last one.
let keepLastTab = (windows.length === 1);
return commandCloseTab(data, keepLastTab); // jshint ignore:line
});
}, [ data ]);
}
}, closeTabTimeoutMs);
}
// Navigate forward in history.
function commandHistoryForward (data) {
window.history.forward();
}
// Increment the page/number in the URL.
function commandPageUp (data) {
if (!(settings.useRelPrevNext && goRelNextPrev(true))) {
alterPageNumber(p => p + 1);
}
}
// Decrement the page/number in the URL.
function commandPageDown (data) {
if (!(settings.useRelPrevNext && goRelNextPrev(false))) {
// Clamp page down at zero.
alterPageNumber(p => (p > 0) ? (p - 1) : 0);
}
}
// Go to the parent directory or domain.
function commandParentDirectory (data) {
if (window.location.pathname === '/') {
// Remove one subdomain, but try to detect when only the TLD remains.
let domainParts = window.location.hostname.split('.').slice(1);
if ((domainParts.length === 1) || ((domainParts.length === 2) && (domainParts[0] === 'co'))) {
// Suspected TLD so don't go up.
return;
} else {
window.location.hostname = domainParts.join('.');
}
} else {
// Go up one directory.
window.location.href = (window.location.href.endsWith('/') ? '..' : './');
}
}
// Reload the frame in the active tab.
function commandReloadFrame (data) {
window.location.reload();
}
// Scroll to the bottom of the frame or page.
function commandScrollBottom (data) {
bubblingScrollTarget(data, 'commandScrollBottom', node => {
node = findScrollingDownNode(node);
return node ? scrollYEase(node, node.scrollHeight - node.clientHeight, settings.scrollDuration) : false;
});
}
// Scroll the viewport down.
function commandScrollDown (data) {
// This chaining ensures the previous scroll command is done.
scrollYPromise = scrollYPromise.then(() => {
bubblingScrollTarget(data, 'commandScrollDown', node => {
node = findScrollingDownNode(node);
if (node) {
// This chaining ensures the scrolling animation is done.
scrollYPromise = scrollYPromise.then(() => {
let scrollAmount = node.clientHeight * (settings.scrollAmount / 100);
let scrollTo = Math.min(node.scrollTop + scrollAmount, node.scrollTopMax);
return scrollYEase(node, scrollTo, settings.scrollDuration);
});
return true;
}
return false;
});
});
}
// Scroll to the top of the frame or page.
function commandScrollTop (data) {
bubblingScrollTarget(data, 'commandScrollTop', node => {
node = findScrollingUpNode(node);
return node ? scrollYEase(node, 0, settings.scrollDuration) : false;
});
}
// Scroll the viewport up.
function commandScrollUp (data) {
// This chaining ensures the previous scroll command is done.
scrollYPromise = scrollYPromise.then(() => {
bubblingScrollTarget(data, 'commandScrollUp', node => {
node = findScrollingUpNode(node);
if (node) {
// This chaining ensures the scrolling animation is done.
scrollYPromise = scrollYPromise.then(() => {
let scrollAmount = node.clientHeight * (settings.scrollAmount / 100);
let scrollTo = Math.max(node.scrollTop - scrollAmount, 0);
return scrollYEase(node, scrollTo, settings.scrollDuration);
});
return true;
}
return false;
});
});
}
// Stop loading the current document.
function commandStop (data) {
window.stop();
}
// Execute a user script.
function commandUserScript (data) {
// TODO Remove this in next version.
if (data.element.mediaSource) {
data.element.mediaInfo = {
source: data.element.mediaSource,
type: data.element.mediaType
};
}
/* jshint evil:true */
try {
var mouseDown = fg.mouseEvents.state.mouseDown;
return Promise.resolve(eval(data.userScript.script));
} catch (err) {
// Report any error with the user script.
let label = data.userScript.label || 'User Script';
setStatus(label + ' error: ' + err.message);
console.log(label, 'error', err);
}
}
// User script API functions -----------------------------------------------------------------------------------------
// These are functions that primarily exist for use with user scripts.
// Serialize a function and send it to the background script for execution.
// This is a mechanism for user scripts to execute code in the priviledged background context.
function executeInBackground (func, args) {
return browser.runtime.sendMessage({
topic: 'mg-executeInBackground',
data: {
args: args || [],
func: func.toString()
}
});
}
// Set the status text.
function setStatus (status) {
postTo(window.top, 'status', status);
}
});