-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
118 lines (99 loc) · 2.56 KB
/
background.js
File metadata and controls
118 lines (99 loc) · 2.56 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
const STORAGE_KEY = "layer-toggle-enabled-0301314611"; // must be unique to extension and match content.js
/**
* See https://developer.chrome.com/docs/extensions/reference/tabs/#type-Tab
* for full type definition
* @typedef {Object} Tab
* @property {number=} id
* @property {string=} url
*/
/**
* @returns {Promise<Tab>}
*/
const getActiveTab = async () => {
return new Promise(resolve => {
chrome.tabs.query({active: true, lastFocusedWindow: true}, tabs => {
resolve(tabs[0]);
});
});
};
/**
* @param {Tab} tab
*/
const runContentScript = (tab) => {
chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ["content.js"]
});
};
/**
* @param {Tab} tab
*/
const shouldRunContentScript = (tab) => {
if (!tab) return false;
const { url } = tab;
return url && url.length && !url.startsWith("chrome://");
};
const attemptContentScript = async () => {
const activeTab = await getActiveTab();
if (shouldRunContentScript(activeTab)) {
runContentScript(activeTab);
}
};
/**
* @returns {Promise<boolean>}
*/
const getStorageValue = async () => {
return new Promise(resolve => {
chrome.storage.local.get(STORAGE_KEY, (result) => {
resolve(result[STORAGE_KEY]);
});
});
};
/**
* @param {boolean} value
*/
const setStorageValue = (value) => {
chrome.storage.local.set({ [STORAGE_KEY]: value });
};
const toggleStorageValue = async () => {
const oldValue = await getStorageValue();
const newValue = !oldValue;
setStorageValue(newValue);
};
/**
* @param {boolean} enabled
*/
const setBadge = (enabled) => {
const text = enabled ? "ON" : "";
chrome.action.setBadgeText({ text });
};
const handleInstalled = async () => {
const initialValue = await getStorageValue();
if (typeof initialValue === "boolean") {
setBadge(initialValue);
attemptContentScript();
} else {
setStorageValue(false);
}
};
const handleActivated = () => {
attemptContentScript();
};
const handleDOMContentLoaded = () => {
attemptContentScript();
};
const handleClicked = () => {
toggleStorageValue();
};
const handleStorageChanged = (changes, area) => {
if (area === "local" && STORAGE_KEY in changes) {
const { newValue } = changes[STORAGE_KEY];
setBadge(newValue);
attemptContentScript();
}
};
chrome.runtime.onInstalled.addListener(handleInstalled);
chrome.tabs.onActivated.addListener(handleActivated);
chrome.webNavigation.onDOMContentLoaded.addListener(handleDOMContentLoaded);
chrome.action.onClicked.addListener(handleClicked);
chrome.storage.onChanged.addListener(handleStorageChanged);