49 lines
1.3 KiB
TypeScript
49 lines
1.3 KiB
TypeScript
import browser from 'webextension-polyfill';
|
|
|
|
import {Settings} from '../settings/settings.js';
|
|
|
|
let timeoutId: number | undefined;
|
|
|
|
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 the icon is clicked again in those 500ms, open the options page instead.
|
|
if (timeoutId === undefined) {
|
|
timeoutId = window.setTimeout(async () => {
|
|
timeoutId = undefined;
|
|
|
|
const nextItem = settings.nextQueueItem();
|
|
|
|
if (nextItem === undefined) {
|
|
await browser.runtime.openOptionsPage();
|
|
return;
|
|
}
|
|
|
|
const tabs = await browser.tabs.query({
|
|
active: true,
|
|
currentWindow: true,
|
|
});
|
|
|
|
const message: Queue.Message<Queue.Item> = {
|
|
action: 'queue open url',
|
|
data: nextItem,
|
|
};
|
|
|
|
try {
|
|
await browser.tabs.sendMessage(tabs[0].id!, message);
|
|
} catch {
|
|
await browser.tabs.create({active: true, url: nextItem.url});
|
|
}
|
|
|
|
await settings.removeQueueItem(nextItem.id);
|
|
}, 500);
|
|
return;
|
|
}
|
|
|
|
window.clearTimeout(timeoutId);
|
|
timeoutId = undefined;
|
|
await browser.runtime.openOptionsPage();
|
|
}
|