2022-03-05 13:10:45 +00:00
|
|
|
import browser from 'webextension-polyfill';
|
|
|
|
|
|
|
|
import {Settings} from '../settings/settings.js';
|
2022-03-14 22:58:05 +00:00
|
|
|
import {updateBadge} from '../utilities/badge.js';
|
2022-03-05 13:10:45 +00:00
|
|
|
|
2022-03-20 18:39:46 +00:00
|
|
|
// Chromium action handler in service worker.
|
|
|
|
export async function actionClicked(): Promise<void> {
|
|
|
|
await nextItem();
|
|
|
|
}
|
|
|
|
|
2022-03-05 13:10:45 +00:00
|
|
|
let timeoutId: number | undefined;
|
|
|
|
|
2022-03-20 18:39:46 +00:00
|
|
|
// Firefox browser action handler in background script.
|
2022-03-05 13:10:45 +00:00
|
|
|
export async function browserActionClicked(): Promise<void> {
|
|
|
|
// 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;
|
2022-03-20 18:39:46 +00:00
|
|
|
await nextItem();
|
2022-03-05 13:10:45 +00:00
|
|
|
}, 500);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-14 22:58:05 +00:00
|
|
|
// If the icon is clicked again in those 500ms, open the options page instead.
|
2022-03-05 13:10:45 +00:00
|
|
|
window.clearTimeout(timeoutId);
|
|
|
|
timeoutId = undefined;
|
|
|
|
await browser.runtime.openOptionsPage();
|
|
|
|
}
|
2022-03-20 18:39:46 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|