From 0a2891d919512e45307f2cc4cbc8a10b62d7e6dc Mon Sep 17 00:00:00 2001 From: Bauke Date: Tue, 25 Apr 2023 12:33:07 +0200 Subject: [PATCH] Add clear history and next item functions. --- source/item/item.ts | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/source/item/item.ts b/source/item/item.ts index b4da283..77c107f 100644 --- a/source/item/item.ts +++ b/source/item/item.ts @@ -199,3 +199,42 @@ export async function setBadgeText(): Promise { action.setBadgeTextColor({color: "#f2efff"}); } } + +/** + * Remove all historical items from local WebExtension storage. + */ +export async function clearHistory(): Promise { + const historyPrefix: ItemKeyPrefix = "history-"; + const historyItemKeys = await getItemKeys(historyPrefix); + const storage = storageForPrefix(historyPrefix); + await storage.remove(historyItemKeys); +} + +/** + * Opens the next queued item if one is available, otherwise opens the + * WebExtension options page. + * + * @param newTab Open the next item in a new tab (default `false`). + */ +export async function openNextItemOrOptionsPage(newTab = false): Promise { + const item = await nextItem(); + if (item === undefined) { + await browser.runtime.openOptionsPage(); + return; + } + + const url = item.value.url; + await (newTab + ? browser.tabs.create({active: true, url}) + : browser.tabs.update({url})); + + await item.remove(); + await setBadgeText(); + + const historyItem = await createItem( + item.value.text, + item.value.url, + "history-", + ); + await historyItem.save(); +}