forked from mdn/webextensions-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
34 lines (31 loc) · 1.17 KB
/
background.js
File metadata and controls
34 lines (31 loc) · 1.17 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
async function openOrSelectTab(reloadExistingTab = false) {
const tabUrl = browser.runtime.getURL("/extpage.html");
let [tab] = await browser.tabs.query({ url: tabUrl });
if (!tab) {
await browser.tabs.create({ url: tabUrl, active: true });
} else {
await browser.tabs.update(tab.id, {
active: true,
...(reloadExistingTab ? { url: tabUrl } : {})
});
}
}
async function switchToNextTheme() {
openOrSelectTab();
const themes = await browser.management.getAll().then(extensions => {
return extensions.filter(ext => ext.type === "theme");
});
const activeThemeIndex = themes.findIndex(theme => theme.enabled);
const nextThemeIndex = activeThemeIndex < themes.length - 1
? activeThemeIndex + 1
: 0
const nextTheme = themes[nextThemeIndex];
await browser.management.setEnabled(nextTheme.id, true);
}
// Switch to the next theme available on pageAction or action icons clicks.
browser.pageAction.onClicked.addListener(switchToNextTheme);
browser.action.onClicked.addListener(switchToNextTheme);
// Open the the extension page in a new tab after the add-on is installed.
browser.runtime.onInstalled.addListener(() => {
openOrSelectTab(true);
});