-
Notifications
You must be signed in to change notification settings - Fork 2
/
settings.js
94 lines (82 loc) · 2.25 KB
/
settings.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
"use strict";
// Keep in sync with content-script.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
let loadPromise = new Promise(resolve => {
window.addEventListener('load', resolve, false);
});
function handleShortcutKeyDown(event) {
if (event.which === 8) {
// Clear on backspace.
this.value = "";
}
else {
let str = keyDownEventToString(event);
if (!str)
return;
this.value = str;
}
browser.storage.local.set({
"shortcut": this.value
});
event.stopPropagation();
event.preventDefault();
}
Promise.all([loadPromise, settingsPromise]).then(() => {
var shortcutEl = document.getElementById("shortcut");
var cancelTimersEl = document.getElementById("timers");
var stopVideosEl = document.getElementById("videos");
var stopAnimationsEl = document.getElementById("animations");
shortcutEl.value = settings.shortcut;
cancelTimersEl.checked = settings.cancelTimers;
stopVideosEl.checked = settings.stopVideos;
stopAnimationsEl.checked = settings.stopCssAnimations;
cancelTimersEl.onchange = function() {
browser.storage.local.set({
"cancelTimers": this.checked
});
};
stopVideosEl.onchange = function() {
browser.storage.local.set({
"stopVideos": this.checked
});
};
stopAnimationsEl.onchange = function() {
browser.storage.local.set({
"stopCssAnimations": this.checked
});
};
shortcutEl.addEventListener("keydown", handleShortcutKeyDown, false);
document.body.hidden = false;
});