-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
50 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
50
(async () => {
const trigger = document.querySelector('.trigger');
/**
* update state of trigger button
*/
const updateDisplayState = (el, bool) => {
if(!bool){
el.classList.add('disabled');
} else {
el.classList.remove('disabled');
}
el.textContent = !bool ? 'turn ON' : 'turn OFF';
};
/**
* Make sure default state of app is set (whether it is activated or not)
*/
const activeState = await browser.storage.local.get('isActive').then(results => results.isActive);
if (!activeState) {
await browser.storage.local.set({
isActive: false
});
}
/**
* update the visuals of trigger button
*/
updateDisplayState(trigger, await browser.storage.local.get('isActive').then(results => results.isActive));
// on / off functionality
document.querySelector('.trigger').addEventListener("click", async(e) => {
// get current state
const result = await browser.storage.local.get('isActive').then(results => results.isActive);
// now set the opposite of current state
await browser.storage.local.set({
isActive: !result
}).then(() => {
// update html
const target = e.target;
updateDisplayState(target, !result);
// reload tab
browser.tabs.reload();
// close browser extension window
window.close();
});
});
})();