Add clear history and next item functions.

This commit is contained in:
Bauke 2023-04-25 12:33:07 +02:00
parent 265ca87a90
commit 0a2891d919
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
1 changed files with 39 additions and 0 deletions

View File

@ -199,3 +199,42 @@ export async function setBadgeText(): Promise<void> {
action.setBadgeTextColor({color: "#f2efff"}); action.setBadgeTextColor({color: "#f2efff"});
} }
} }
/**
* Remove all historical items from local WebExtension storage.
*/
export async function clearHistory(): Promise<void> {
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<void> {
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();
}