-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontent-script.js
103 lines (93 loc) · 2.57 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
"use strict";
// Keep in sync with settings.js!
const defaultSettings = {
cancelTimers: true,
stopVideos: true,
stopCssAnimations: true,
shortcut: 'Shift+Esc'
};
let settings = null;
let settingsPromise = browser.storage.local.get(defaultSettings)
.then(data => { settings = data; })
.catch(err => { console.error(err); });
function keyDownEventToString(event) {
function keyToString(event) {
let code = event.which, key = event.key;
if (code === 27)
return "Esc";
if (code === 32)
return "Space";
if (code < 32)
return null;
return (key.length === 1 ? key.toUpperCase() : key);
}
function accelToString(event) {
var accel = "";
if (event.ctrlKey) accel += "Ctrl+";
if (event.metaKey) accel += "Meta+";
if (event.shiftKey) accel += "Shift+";
if (event.altKey) accel += "Alt+";
return accel;
}
var key = keyToString(event), accel = accelToString(event);
return key ? accel + key : "";
}
// end keep in sync
settingsPromise.then(() => {
browser.storage.onChanged.addListener((changes, area) => {
if (area != "local")
return;
for (var prop in changes) {
if (prop in defaultSettings) {
settings[prop] = changes[prop].newValue;
console.log("updated setting", prop, changes[prop].newValue);
}
}
});
});
function superStop() {
// (A bit anticlimactic, no?)
window.stop();
}
let timer = 0;
function cancelTimers() {
let lastTimer = window.setTimeout(() => {});
window.clearTimeout(lastTimer);
for (let i = timer; i < lastTimer; i++)
window.clearTimeout(i);
timer = lastTimer + 1;
}
function stopVideos() {
for (let video of document.getElementsByTagName("video")) {
if (video.pause) video.pause();
}
}
function stopCssAnimations() {
let el = document.createElement("style");
el.textContent = "*, *::before, *::after { animation: none !important; }";
document.documentElement.appendChild(el);
}
function handleKeyDown(event) {
if (!event.isTrusted)
return;
if (!settings || !settings.shortcut || keyDownEventToString(event) != settings.shortcut)
return;
superStop(); // if other things somehow fail
browser.runtime.sendMessage({type: "do-super-stop"})
.catch(err => { console.error(err); });
}
window.addEventListener('keydown', handleKeyDown, true);
browser.runtime.onMessage.addListener((msg, sender, sendResponse) => {
if (msg.type == "do-super-stop") {
if (window == top)
superStop();
if (settings && settings.cancelTimers)
cancelTimers();
if (settings && settings.stopVideos)
stopVideos();
if (settings && settings.stopCssAnimations)
stopCssAnimations();
} else {
throw new Error("unknown message type");
}
});