-
Notifications
You must be signed in to change notification settings - Fork 27
/
bg.js
40 lines (34 loc) · 1.09 KB
/
bg.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
function toggleState(currentState) {
return currentState === "on" ? "off" : "on";
}
function reset(currentState) {
if (currentState === "on") {
chrome.action.setIcon({ path: "on.png" });
chrome.action.setTitle({ title: "Status: ON" });
chrome.declarativeNetRequest.updateEnabledRulesets({
"enableRulesetIds": ["ruleset_1"]
});
} else {
chrome.action.setIcon({ path: "off.png" });
chrome.action.setTitle({ title: "Status: OFF" });
chrome.declarativeNetRequest.updateEnabledRulesets({
"disableRulesetIds": ["ruleset_1"]
});
}
chrome.storage.local.set({ currentState: currentState });
}
// init state
chrome.runtime.onStartup.addListener(() => {
chrome.storage.local.get("currentState", result => {
if (result.currentState === undefined) {
result.currentState = "on"; // default to "on"
}
reset(result.currentState); // reset state
});
});
chrome.action.onClicked.addListener(tab => {
chrome.storage.local.get("currentState", result => {
const currentState = toggleState(result.currentState);
reset(currentState);
});
});