-
Notifications
You must be signed in to change notification settings - Fork 10
/
popup.js
66 lines (59 loc) · 1.8 KB
/
popup.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
let options = {};
/**
* Saves the options to storage
*/
function saveOptions() {
chrome.storage.local.set({
options: options,
});
}
/**
* Loads the users options from storage and sets it to the global (global to this file) options object
* it also updates the respective elements to reflect the current state
*/
function loadOptions() {
chrome.storage.local.get("options", function (o) {
options = o.options || {
announceMovement: false,
featureIndexOverlayOption: false,
renderMap: false
};
for (let name in options) {
let elem = document.getElementById(name);
if (elem) {
switch (typeof options[name]) {
case "boolean":
elem.checked = options[name];
break;
}
}
}
});
}
/**
* Handles checkbox changes, changes are then reflected in the users options in storage
* @param e - Event object
*/
function handleCheckboxChange(e) {
let option = e.target.id;
options[option] = e.target.checked;
console.log(options);
saveOptions();
}
/**
* Used to clear the extensions storage
*/
function resetStorage() {
chrome.storage.local.clear(loadOptions);
}
// You cannot call a function directly from popup.html, you need to attach a listener in the accompanying JS file
/**
* Attaches event handlers to the user inputs (i.e. checkboxes), and initializes the options
*/
document.addEventListener("DOMContentLoaded", () => {
loadOptions();
document.getElementById("announceMovement").addEventListener("change", handleCheckboxChange);
document.getElementById("featureIndexOverlayOption").addEventListener("change", handleCheckboxChange);
document.getElementById("renderMap").addEventListener("change", handleCheckboxChange);
document.getElementById("clear").addEventListener("click", resetStorage);
});