Skip to content

Commit

Permalink
Add badge with link count
Browse files Browse the repository at this point in the history
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
  • Loading branch information
aledeg committed Dec 3, 2019
1 parent 4eba9c9 commit 8088ad4
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 27 deletions.
4 changes: 0 additions & 4 deletions _locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
4 changes: 0 additions & 4 deletions _locales/fr/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
52 changes: 36 additions & 16 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || [];
Expand All @@ -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 = []) {
Expand Down Expand Up @@ -142,31 +159,31 @@ 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) {
if (delta.id !== downloadId) {
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'));
}
Expand All @@ -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;
Expand Down Expand Up @@ -239,3 +257,5 @@ browser.pageAction.onClicked.addListener(tab => {
browser.downloads.onChanged.addListener(handleChanged);

browser.runtime.onMessage.addListener(handleMessage);

updateBadge();
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

"manifest_version": 2,
"name": "LinkDump",
"version": "1.14",
"version": "1.15",
"description": "__MSG_extensionDescription__",
"icons": {
"48": "icons/linkdump-48.png"
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "linkdump",
"version": "1.14",
"version": "1.15",
"description": "Store links and dump them",
"main": "background.js",
"scripts": {
Expand Down

0 comments on commit 8088ad4

Please sign in to comment.