Add a badge to the extension icon that shows the number of items saved.

This commit is contained in:
Bauke 2020-11-12 22:42:21 +01:00
parent 182d7afaf2
commit 6ce6b9ba28
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
7 changed files with 766 additions and 686 deletions

View File

@ -8,7 +8,8 @@ import {
QItem, QItem,
QMessage, QMessage,
removeQItem, removeQItem,
saveSettings saveSettings,
updateBadge
} from '.'; } from '.';
let timeoutID: number | null = null; let timeoutID: number | null = null;
@ -46,6 +47,12 @@ browser.browserAction.onClicked.addListener(async () => {
} }
}); });
browser.runtime.onMessage.addListener(async (request: QMessage<unknown>) => {
if (request.action === 'queue update badge') {
await updateBadge();
}
});
browser.runtime.onInstalled.addListener(async () => { browser.runtime.onInstalled.addListener(async () => {
const manifest = getManifest(); const manifest = getManifest();
const settings = await getSettings(); const settings = await getSettings();
@ -100,5 +107,6 @@ browser.contextMenus.onClicked.addListener(async (info, _tab) => {
}); });
await saveSettings(settings); await saveSettings(settings);
await updateBadge(settings);
} }
}); });

View File

@ -1,3 +1,8 @@
import {browser} from 'webextension-polyfill-ts';
import {initializeBackgroundMessageHandler} from '.'; import {initializeBackgroundMessageHandler} from '.';
initializeBackgroundMessageHandler(); initializeBackgroundMessageHandler();
(async () => {
await browser.runtime.sendMessage({action: 'queue update badge'});
})();

View File

@ -1,6 +1,6 @@
import {html} from 'htm/preact'; import {html} from 'htm/preact';
type QMessageAction = 'queue open url'; type QMessageAction = 'queue open url' | 'queue update badge';
export type QMessage<T> = { export type QMessage<T> = {
action: QMessageAction; action: QMessageAction;

View File

@ -3,7 +3,7 @@
"manifest_version": 2, "manifest_version": 2,
"name": "Queue", "name": "Queue",
"description": "A WebExtension for queueing links.", "description": "A WebExtension for queueing links.",
"version": "0.1.2", "version": "0.1.3",
"permissions": [ "permissions": [
"contextMenus", "contextMenus",
"storage", "storage",

View File

@ -1,5 +1,6 @@
import {html} from 'htm/preact'; import {html} from 'htm/preact';
import {useState} from 'preact/hooks'; import {useState} from 'preact/hooks';
import {browser} from 'webextension-polyfill-ts';
import { import {
ConfirmButton, ConfirmButton,
Link, Link,
@ -19,6 +20,7 @@ export function PageMain(props: MainProps): QComponent {
const _removeQItem = async (id: number) => { const _removeQItem = async (id: number) => {
const updated = await removeQItem(id); const updated = await removeQItem(id);
updateQueue(updated.queue); updateQueue(updated.queue);
await browser.runtime.sendMessage({action: 'queue update badge'});
}; };
const qItems: QComponent[] = queue const qItems: QComponent[] = queue

View File

@ -1,5 +1,5 @@
import {browser, Manifest} from 'webextension-polyfill-ts'; import {browser, Manifest} from 'webextension-polyfill-ts';
import {QItem, QMessage} from '..'; import {getSettings, QItem, QMessage, Settings} from '..';
/** /**
* Initializes the background message handler. * Initializes the background message handler.
@ -45,5 +45,33 @@ export function error(thing: unknown) {
console.error('[Queue]', thing); console.error('[Queue]', thing);
} }
/**
* Updates the extension icon badge with the number of saved items. This can
* only be run from the background script.
* @param settings Optional user settings to use.
*/
export async function updateBadge(settings?: Settings): Promise<void> {
if (settings === undefined) {
settings = await getSettings();
}
let text: string | null = null;
if (settings.queue.length > 0) {
text = settings.queue.length.toString();
}
await Promise.all([
browser.browserAction.setBadgeBackgroundColor({
color: '#2a2041'
}),
browser.browserAction.setBadgeTextColor({
color: '#f2efff'
}),
browser.browserAction.setBadgeText({
text
})
]);
}
export * from './components'; export * from './components';
export * from './settings'; export * from './settings';

1401
yarn.lock

File diff suppressed because it is too large Load Diff