-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
49 lines (44 loc) · 1.48 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
window.addEventListener("load", loadSettings);
function loadSettings() {
configureToggleUI("toggle", "freedom");
configureToggleUI("toggleDistractions", "distractions");
}
function configureToggleUI(toggle, setting) {
var toggleUI = document.getElementById(toggle);
var toggleValue = localStorage.getItem(setting);
toggleUI.onclick = getSettingFunction(toggle, setting);
refresh(toggle, toggleValue);
}
function getSettingFunction(toggle, setting) {
var toggle = toggle;
var setting = setting;
return function() {
setSetting(toggle, setting);
}
}
function refresh(toggle, setTo) {
//var toggleValue = localStorage.getItem(setting);
var toggleUI = document.getElementById(toggle);
if (setTo === "true") {
toggleUI.checked = true;
} else {
toggleUI.checked = false;
}
}
function setSetting(toggle, setting) {
var settingValue = localStorage.getItem(setting);
var toggleUI = document.getElementById(toggle)
console.log("setSetting: localStorage " + setting + " value is " + settingValue);
if (settingValue === null) {
console.log("setting " + setting + " to toggle value " + toggleUI.checked);
localStorage.setItem(setting, toggleUI.checked);
} else if (settingValue === "false") {
console.log("settingValue is false: " + settingValue + ", setting to true");
localStorage.setItem(setting, true);
refresh(toggle, "true");
} else {
console.log("settingValue is true: " + settingValue + ", setting to false");
localStorage.setItem(setting, false);
refresh(toggle, "false");
}
}