-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.js
55 lines (45 loc) · 1.29 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
async function handleMessage(message, sender) {
switch (message.type) {
case "reset":
browser.theme.reset();
break;
case "update":
browser.theme.update(sender.tab.windowId, message.theme);
break;
default:
break;
}
}
async function handleActivated(tabInfo) {
updateTabTheme(tabInfo.tabId);
}
async function updateTabTheme(tabId) {
try {
await browser.tabs.executeScript(tabId, {
code: "updateTheme()"
})
} catch (error) {
browser.theme.reset()
}
}
async function handleUpdated(tabId, changeInfo, tab) {
updateTabTheme(tabId)
}
async function handleWindowFocusChange(windowId) {
const window = await browser.windows.get(windowId);
if (window.focused) {
const tabs = await browser.tabs.query({
active: true,
windowId: windowId
});
updateTabTheme(tabs[0].tabId);
}
}
async function handleCreated(tab) {
updateTabTheme(tab.id);
}
browser.tabs.onUpdated.addListener(handleUpdated);
browser.tabs.onActivated.addListener(handleActivated);
browser.tabs.onCreated.addListener(handleCreated);
browser.windows.onFocusChanged.addListener(handleWindowFocusChange);
browser.runtime.onMessage.addListener(handleMessage);