-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.js
57 lines (51 loc) · 1.75 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
// Saves options to chrome.storage
const saveOptions = () => {
const toggleEl = document.getElementById("toggle-buttons");
const toggleButtons = toggleEl.checked;
const wordWrapEl = document.getElementById("word-wrap");
let wordWrap = wordWrapEl.checked;
if (!toggleButtons) {
wordWrap = false;
document.getElementById("word-wrap").checked = false;
document.getElementById("word-wrap").disabled = true;
} else {
document.getElementById("word-wrap").disabled = false;
}
chrome.storage.sync.set(
{
toggleButtons,
wordWrap,
},
() => {
// Update status to let user know options were saved.
const status = document.getElementById("status");
status.textContent = "Settings updated!";
status.classList.add("success");
setTimeout(() => {
status.textContent = "";
status.classList.remove("success");
}, 2000);
}
);
};
// Restores select box and checkbox state using the preferences
// stored in chrome.storage.
const restoreOptions = () => {
chrome.storage.sync.get(
{ toggleButtons: true, wordWrap: true },
(items) => {
if (items.toggleButtons === undefined) {
items.toggleButtons = true;
chrome.storage.sync.set({ toggleButtons: items.toggleButtons });
}
if (items.wordWrap === undefined) {
items.wordWrap = true;
chrome.storage.sync.set({ wordWrap: items.wordWrap });
}
document.getElementById("toggle-buttons").checked = items.toggleButtons === true;
document.getElementById("word-wrap").checked = items.wordWrap === true;
}
);
};
document.addEventListener("DOMContentLoaded", restoreOptions);
document.querySelectorAll("input").forEach((el) => el.addEventListener("change", saveOptions));