show a tab count on the badge (#302)

This commit is contained in:
Andy McKay
2017-10-26 14:50:32 -07:00
committed by wbamberg
parent dabfe5c721
commit b52844d11f
2 changed files with 31 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
function updateCount(tabId, isOnRemoved) {
browser.tabs.query({})
.then((tabs) => {
let length = tabs.length;
// onRemoved fires too early and the count is one too many.
// see https://bugzilla.mozilla.org/show_bug.cgi?id=1396758
if (isOnRemoved && tabId && tabs.map((t) => { return t.id; }).includes(tabId)) {
length--;
}
browser.browserAction.setBadgeText({text: length.toString()});
if (length > 2) {
browser.browserAction.setBadgeBackgroundColor({'color': 'green'});
} else {
browser.browserAction.setBadgeBackgroundColor({'color': 'red'});
}
});
}
browser.tabs.onRemoved.addListener(
(tabId) => { updateCount(tabId, true);
});
browser.tabs.onCreated.addListener(
(tabId) => { updateCount(tabId, false);
});
updateCount();

View File

@@ -1,4 +1,7 @@
{
"background": {
"scripts": ["background.js"]
},
"browser_action": {
"browser_style": true,
"default_title": "Tabs, tabs, tabs",