mirror of
https://github.com/mdn/webextensions-examples.git
synced 2026-04-16 14:28:33 +02:00
show a tab count on the badge (#302)
This commit is contained in:
28
tabs-tabs-tabs/background.js
Normal file
28
tabs-tabs-tabs/background.js
Normal 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();
|
||||
@@ -1,4 +1,7 @@
|
||||
{
|
||||
"background": {
|
||||
"scripts": ["background.js"]
|
||||
},
|
||||
"browser_action": {
|
||||
"browser_style": true,
|
||||
"default_title": "Tabs, tabs, tabs",
|
||||
|
||||
Reference in New Issue
Block a user