From dcf395f01d3ee4e325ae37f1ac1afa970f866ffc Mon Sep 17 00:00:00 2001 From: Bauke Date: Sun, 20 Mar 2022 19:39:46 +0100 Subject: [PATCH] Make the background script/service worker Chromium compatible. --- source/background-scripts/browser-action.ts | 34 +++++++++++++-------- source/background-scripts/initialize.ts | 17 +++++++---- 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/source/background-scripts/browser-action.ts b/source/background-scripts/browser-action.ts index f970e8f..cabbf13 100644 --- a/source/background-scripts/browser-action.ts +++ b/source/background-scripts/browser-action.ts @@ -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 { + await nextItem(); +} + let timeoutId: number | undefined; +// Firefox browser action handler in background script. export async function browserActionClicked(): Promise { - 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 { timeoutId = undefined; await browser.runtime.openOptionsPage(); } + +async function nextItem(): Promise { + 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); +} diff --git a/source/background-scripts/initialize.ts b/source/background-scripts/initialize.ts index 638bb80..5bd22f6 100644 --- a/source/background-scripts/initialize.ts +++ b/source/background-scripts/initialize.ts @@ -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(); +}