queue/source/background/setup.ts

46 lines
1.2 KiB
TypeScript

// The main entry point for the background script. Note that in Manifest V3 this
// is run in a service worker.
// https://developer.chrome.com/docs/extensions/migrating/to-service-workers/
import browser from "webextension-polyfill";
import {
clearHistory,
openNextItemOrOptionsPage,
setBadgeText,
} from "../item/item.js";
import {firefoxActionClick} from "./action.js";
import {
contextClicked,
getContextMenus,
initializeContextMenus,
} from "./context-menu.js";
if ($browser === "firefox") {
browser.browserAction.onClicked.addListener(firefoxActionClick);
} else {
browser.action.onClicked.addListener(async () => {
await openNextItemOrOptionsPage();
});
}
browser.runtime.onStartup.addListener(async () => {
await clearHistory();
await setBadgeText();
});
browser.runtime.onInstalled.addListener(async () => {
await initializeContextMenus();
await setBadgeText();
if ($dev) {
await browser.runtime.openOptionsPage();
}
});
browser.contextMenus.onClicked.addListener(async (info, tab) => {
const contextMenus = getContextMenus();
const contextMenuIds = new Set<string>(contextMenus.map(({id}) => id!));
await contextClicked(contextMenuIds, info, tab);
});