-
Notifications
You must be signed in to change notification settings - Fork 1
/
options.js
80 lines (69 loc) · 1.96 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
let dogsCheckBox = document.querySelector("input[name=dogs]");
let lowResCheckBox = document.querySelector("input[name=lowres]");
let disabledCheckBox = document.querySelector("input[name=disabled]");
let disabledOption = document.getElementById("disabled-option");
let disabledLabel = document.getElementById("disabled-label");
let disabledKey = null;
function updateDogsCheckBox(topic)
{
dogsCheckBox.checked = topic == "dogs";
}
chrome.storage.local.get(["topic", "lowres"], items =>
{
updateDogsCheckBox(items.topic);
lowResCheckBox.checked = !!items.lowres;
});
chrome.storage.onChanged.addListener(changes =>
{
if ("topic" in changes)
updateDogsCheckBox(changes.topic.newValue);
if ("lowres" in changes)
lowResCheckBox.checked = !!changes.lowres.newValue;
if (disabledKey && disabledKey in changes)
disabledCheckBox.checked = !!changes[disabledKey].newValue;
});
dogsCheckBox.addEventListener("change", () =>
{
if (dogsCheckBox.checked)
chrome.storage.local.set({topic: "dogs"});
else
chrome.storage.local.remove("topic");
});
lowResCheckBox.addEventListener("change", () =>
{
chrome.storage.local.set({lowres: lowResCheckBox.checked});
});
disabledCheckBox.addEventListener("change", () =>
{
if (disabledCheckBox.checked)
chrome.storage.local.set({[disabledKey]: true});
else
chrome.storage.local.remove(disabledKey);
});
function queryDisabledState()
{
chrome.runtime.sendMessage({type: "get-last-host"}, lastHost =>
{
if (lastHost)
{
disabledOption.hidden = false;
disabledLabel.textContent = "Disable on " + lastHost;
disabledKey = "disabled:" + lastHost;
chrome.storage.local.get(disabledKey, items =>
{
disabledCheckBox.checked = !!items[disabledKey];
});
}
else
{
disabledOption.hidden = true;
disabledKey = null;
}
});
}
document.addEventListener("visibilitychange", () =>
{
if (!document.hidden)
queryDisabledState();
});
queryDisabledState();