From 8088ad465843fcd36beedcd3eb0ab4d43d550f88 Mon Sep 17 00:00:00 2001 From: Alexis Degrugillier Date: Tue, 3 Dec 2019 08:14:56 +0100 Subject: [PATCH] Add badge with link count There is a badge on the linkdump button to show how many links are in the dump. This way, there is no need to open it to check if it's empty or not. See #32 --- _locales/en/messages.json | 4 --- _locales/fr/messages.json | 4 --- background.js | 52 +++++++++++++++++++++++++++------------ manifest.json | 2 +- package-lock.json | 2 +- package.json | 2 +- 6 files changed, 39 insertions(+), 27 deletions(-) diff --git a/_locales/en/messages.json b/_locales/en/messages.json index c2d4f94..caa3e80 100644 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -11,10 +11,6 @@ "message": "Download complete", "description": "Notify the user that the download is complete." }, - "notificationStorageCleared": { - "message": "Storage cleared", - "description": "Notify the user that the storage has been cleared." - }, "notificationStorageCopied": { "message": "Storage copied", "description": "Notify the user that the storage has been copied to the clipboard." diff --git a/_locales/fr/messages.json b/_locales/fr/messages.json index 85419a8..8531b7c 100644 --- a/_locales/fr/messages.json +++ b/_locales/fr/messages.json @@ -11,10 +11,6 @@ "message": "Téléchargement terminé", "description": "Notify the user that the download is complete." }, - "notificationStorageCleared": { - "message": "Entrepôt nettoyé", - "description": "Notify the user that the storage has been cleared." - }, "notificationStorageCopied": { "message": "Entrepôt copié", "description": "Notify the user that the storage has been copied to the clipboard." diff --git a/background.js b/background.js index 35d8785..a9e4e6e 100644 --- a/background.js +++ b/background.js @@ -17,6 +17,22 @@ function notification(message) { }); } +async function updateBadge(items) { + let count = items; + + if (items === undefined) { + const storage = await browser.storage.local.get('urls'); + const urls = storage.urls || []; + count = urls.length || ''; + } else if (items === 0) { + count = ''; + } + + browser.browserAction.setBadgeText({text: count.toString()}); + browser.browserAction.setBadgeTextColor({color: 'white'}); + browser.browserAction.setBadgeBackgroundColor({color: '#007bc5'}); +} + async function addLink(link) { const storage = await browser.storage.local.get('urls'); let urls = storage.urls || []; @@ -36,7 +52,8 @@ async function addLink(link) { } }) - await browser.storage.local.set({ "urls": urls}); + await browser.storage.local.set({ urls }); + await updateBadge(urls.length); } function getLinks(bookmark, initialLinks = []) { @@ -142,21 +159,20 @@ function download(downloadOptions) { }); } -function deleteLink(link) { - browser.storage.local.get('urls').then(obj => { - if (!obj.urls) return; - let { urls } = obj; +async function deleteLink(link) { + const obj = await browser.storage.local.get('urls'); + if (!obj.urls) return; + let { urls } = obj; - urls = urls.filter((item) => item.url !== link.url && item.title !== link.title); + urls = urls.filter((item) => item.url !== link.url && item.title !== link.title); - browser.storage.local.set({ urls }); - }); + await browser.storage.local.set({ urls }); + await updateBadge(urls.length); } -function clear() { - browser.storage.local.remove('urls').then( - notification('notificationStorageCleared') - ); +async function clear() { + await browser.storage.local.remove('urls'); + await updateBadge(0); } function handleChanged(delta) { @@ -164,9 +180,10 @@ function handleChanged(delta) { return; } if (delta.state && delta.state.current === 'complete') { - browser.storage.local.get('options').then(obj => { + browser.storage.local.get('options').then(async obj => { if (obj.options !== undefined && obj.options.clear !== undefined && obj.options.clear.download) { - browser.storage.local.remove('urls'); + await browser.storage.local.remove('urls'); + await updateBadge(0); } }).then(notification('notificationDownloadComplete')); } @@ -185,9 +202,10 @@ function handleMessage(message) { break; } case 'copied': { - browser.storage.local.get('options').then(obj => { + browser.storage.local.get('options').then(async obj => { if (obj.options !== undefined && obj.options.clear !== undefined && obj.options.clear.copy) { - browser.storage.local.remove('urls'); + await browser.storage.local.remove('urls'); + await updateBadge(0); } }).then(notification('notificationStorageCopied')); break; @@ -239,3 +257,5 @@ browser.pageAction.onClicked.addListener(tab => { browser.downloads.onChanged.addListener(handleChanged); browser.runtime.onMessage.addListener(handleMessage); + +updateBadge(); diff --git a/manifest.json b/manifest.json index 235db61..ba0dc91 100644 --- a/manifest.json +++ b/manifest.json @@ -7,7 +7,7 @@ "manifest_version": 2, "name": "LinkDump", - "version": "1.14", + "version": "1.15", "description": "__MSG_extensionDescription__", "icons": { "48": "icons/linkdump-48.png" diff --git a/package-lock.json b/package-lock.json index 8809ad8..77eca8f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "linkdump", - "version": "1.14.0", + "version": "1.15", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index b79ef32..b36c3c1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "linkdump", - "version": "1.14", + "version": "1.15", "description": "Store links and dump them", "main": "background.js", "scripts": {