-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptions.js
36 lines (33 loc) · 1.04 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
function getInputValue(input) {
if (input.type === "checkbox") {
return input.checked;
} else if (input.type === "number") {
return input.valueAsNumber;
} else {
return input.value;
}
}
function setInputValue(input, value) {
if (input.type === "checkbox") {
input.checked = value;
} else if (input.type === "number") {
input.valueAsNumber = value;
} else {
input.value = value;
}
}
document.addEventListener("DOMContentLoaded", () => {
chrome.storage.sync.get((options) => {
function onChange(event) {
const input = event.target;
// NOTE: getInputValue() can return NaN. That’s fine.
options[input.name] = getInputValue(input);
chrome.storage.sync.set(options);
}
for (const input of document.getElementsByTagName("input")) {
const value = getOption(options, input.name);
setInputValue(input, value);
input.addEventListener("input", onChange);
}
});
});