-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_worker.js
53 lines (46 loc) · 1.68 KB
/
service_worker.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
const EXTENSION_DEFAULT_OPACITY = '0.6';
//onInstalled
//extension is installed or updated
//one-off inits
chrome.runtime.onInstalled.addListener(({reason}) => {
//or, we could skip this .set() and simply call .get() with a default in options.js::restoreOptions()
if (reason === 'install') {
chrome.storage.local.set({
opacity: EXTENSION_DEFAULT_OPACITY
});
}
//cement to users whether overlay is ON or OFF - default to OFF
chrome.action.setBadgeText({
text: "OFF",
});
});
//onClicked
//extension's icon button is clicked
chrome.action.onClicked.addListener(async (tab) => {
//ON / OFF state handling
const prevState = await chrome.action.getBadgeText({tabId: tab.id});
const nextState = prevState === 'ON' ? 'OFF' : 'ON';
//show if ON / OFF
await chrome.action.setBadgeText({
tabId: tab.id,
text: nextState
});
//NOTE: combining insert/removeCSS() (for the overlay styles) and executeScript() (to toggle the overlay) does not work.
//Timing / rendering issues?
// if (nextState === "ON") {
// await chrome.scripting.insertCSS({
// files: ['styles/security_screen.css'],
// target: { tabId: tab.id}
// });
// } else if (nextState === "OFF") {
// await chrome.scripting.removeCSS({
// files: ['styles/security_screen.css'],
// target: { tabId: tab.id}
// });
// }
//NOTE: This script handles the toggling of the overlay itself - we don't need the ON / OFF state
await chrome.scripting.executeScript({
target : {tabId : tab.id},
files : [ "scripts/security_screen.js" ],
});
});