Compare commits

...

2 Commits

Author SHA1 Message Date
Bauke 0a2891d919
Add clear history and next item functions. 2023-04-25 12:33:07 +02:00
Bauke 265ca87a90
Add documentation. 2023-04-25 12:32:38 +02:00
1 changed files with 46 additions and 0 deletions

View File

@ -31,6 +31,13 @@ export type SerializedItem = {
/** The key prefix for {@link Item}s. */ /** The key prefix for {@link Item}s. */
export type ItemKeyPrefix = "item-" | "history-"; export type ItemKeyPrefix = "item-" | "history-";
/**
* Returns the dedicated WebExtension storage area for a given
* {@link ItemKeyPrefix}.
*
* @param prefix The target {@link ItemKeyPrefix}.
* @returns The WebExtension storage area.
*/
export function storageForPrefix( export function storageForPrefix(
prefix: ItemKeyPrefix, prefix: ItemKeyPrefix,
): browser.Storage.StorageArea { ): browser.Storage.StorageArea {
@ -192,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();
}