-
Notifications
You must be signed in to change notification settings - Fork 5
/
options.js
64 lines (58 loc) · 1.7 KB
/
options.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
const defaults = {
color: "#fff",
textColor: "#808080",
time: 7,
text: "default text",
fontSize: "2vw",
runOn: String.raw`hckrnews\.com
reddit\.com
facebook\.com
news\.ycombinator\.com
youtube\.com`,
delayLinks: false,
variance: 0,
};
function saveOptions(e) {
e.preventDefault();
browser.storage.sync.set({
settings: {
color: document.querySelector("#color").value,
textColor: document.querySelector("#textColor").value,
time: parseFloat(document.querySelector("#time").value, 10),
text: document.querySelector("#text").value,
fontSize: document.querySelector("#fontSize").value,
runOn: document.querySelector("#runOn").value,
delayLinks: document.querySelector("#delayLinks").checked,
variance: document.querySelector("#variance").value,
},
});
document.getElementById("savedSettings").style = "color:green;";
}
function restoreOptions() {
function setCurrentChoice(result) {
const settings = (result && result.settings) || {};
Object.keys(defaults).forEach((key) => {
const el = document.querySelector(`#${key}`);
if (el.type === "checkbox") {
el.checked = vOrDefault(settings[key], defaults[key]);
} else {
el.value = vOrDefault(settings[key], defaults[key]);
}
});
}
function onError(error) {
console.log(`Error: ${error}`);
}
browser.storage.sync.get("settings").then(setCurrentChoice, onError);
}
document.addEventListener("DOMContentLoaded", restoreOptions);
document.querySelector("form").addEventListener("submit", saveOptions);
function vOrDefault(v, def) {
if (v) {
return v;
} else if (v === "" || v === {} || v === 0) {
return v;
} else {
return def;
}
}