diff --git a/tabs-tabs-tabs/background.js b/tabs-tabs-tabs/background.js new file mode 100644 index 0000000..ec4b051 --- /dev/null +++ b/tabs-tabs-tabs/background.js @@ -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(); diff --git a/tabs-tabs-tabs/manifest.json b/tabs-tabs-tabs/manifest.json index 76663af..6159095 100644 --- a/tabs-tabs-tabs/manifest.json +++ b/tabs-tabs-tabs/manifest.json @@ -1,4 +1,7 @@ { + "background": { + "scripts": ["background.js"] + }, "browser_action": { "browser_style": true, "default_title": "Tabs, tabs, tabs",