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

View File

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