-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
38 lines (34 loc) · 1.04 KB
/
background.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
import { injectScript, updateIcon } from "./utils.js";
// Handle tab updates
browser.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
if (changeInfo.status === "complete") {
const url = tab.url;
if (!url.startsWith("http") && !url.startsWith("https")) {
return; // Ignore special pages like about:newtab
}
const result = await browser.storage.local.get(url);
const script = result[url] || "";
if (script) {
injectScript(tabId, url, script);
} else {
updateIcon("no-script");
}
}
});
// Handle browser startup
browser.runtime.onStartup.addListener(async () => {
const tabs = await browser.tabs.query({});
for (const tab of tabs) {
const url = tab.url;
if (!url.startsWith("http") && !url.startsWith("https")) {
continue; // Ignore special pages like about:newtab
}
const result = await browser.storage.local.get(url);
const script = result[url] || "";
if (script) {
injectScript(tab.id, url, script);
} else {
updateIcon("no-script");
}
}
});