Make the background script/service worker Chromium compatible.
This commit is contained in:
parent
ded38c7bf7
commit
dcf395f01d
|
@ -3,17 +3,33 @@ 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;
|
||||
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();
|
||||
|
||||
if (nextItem === undefined) {
|
||||
|
@ -24,12 +40,4 @@ export async function browserActionClicked(): Promise<void> {
|
|||
await browser.tabs.update({url: nextItem.url});
|
||||
await settings.removeQueueItem(nextItem.id);
|
||||
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();
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue