Make the background script/service worker Chromium compatible.

This commit is contained in:
Bauke 2022-03-20 19:39:46 +01:00
parent ded38c7bf7
commit dcf395f01d
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
2 changed files with 32 additions and 19 deletions

View File

@ -3,27 +3,21 @@ import browser from 'webextension-polyfill';
import {Settings} from '../settings/settings.js';
import {updateBadge} from '../utilities/badge.js';
// Chromium action handler in service worker.
export async function actionClicked(): Promise<void> {
await nextItem();
}
let timeoutId: number | undefined;
// Firefox browser action handler in background script.
export async function browserActionClicked(): Promise<void> {
const settings = await Settings.fromSyncStorage();
// When the extension icon is initially clicked, create a timeout for 500ms
// that will open the next queue item when it expires.
if (timeoutId === undefined) {
timeoutId = window.setTimeout(async () => {
timeoutId = undefined;
const nextItem = settings.nextQueueItem();
if (nextItem === undefined) {
await browser.runtime.openOptionsPage();
return;
}
await browser.tabs.update({url: nextItem.url});
await settings.removeQueueItem(nextItem.id);
await updateBadge(settings);
await nextItem();
}, 500);
return;
}
@ -33,3 +27,17 @@ export async function browserActionClicked(): Promise<void> {
timeoutId = undefined;
await browser.runtime.openOptionsPage();
}
async function nextItem(): Promise<void> {
const settings = await Settings.fromSyncStorage();
const nextItem = settings.nextQueueItem();
if (nextItem === undefined) {
await browser.runtime.openOptionsPage();
return;
}
await browser.tabs.update({url: nextItem.url});
await settings.removeQueueItem(nextItem.id);
await updateBadge(settings);
}

View File

@ -3,7 +3,7 @@ import browser from 'webextension-polyfill';
import {Settings} from '../settings/settings.js';
import {updateBadge} from '../utilities/badge.js';
import {History} from '../utilities/history.js';
import {browserActionClicked} from './browser-action.js';
import {actionClicked, browserActionClicked} from './browser-action.js';
import {initializeContextMenus} from './context-menus.js';
browser.runtime.onStartup.addListener(async () => {
@ -12,12 +12,17 @@ browser.runtime.onStartup.addListener(async () => {
await updateBadge(await Settings.fromSyncStorage());
});
browser.browserAction.onClicked.addListener(browserActionClicked);
if (import.meta.env.VITE_BROWSER === 'chromium') {
browser.action.onClicked.addListener(actionClicked);
} else {
browser.browserAction.onClicked.addListener(browserActionClicked);
}
browser.runtime.onInstalled.addListener(async () => {
if (import.meta.env.DEV) {
await browser.runtime.openOptionsPage();
}
await initializeContextMenus();
await updateBadge(await Settings.fromSyncStorage());
});
initializeContextMenus();
if (import.meta.env.DEV) {
void browser.runtime.openOptionsPage();
}