diff --git a/background.js b/background.js index f96d376..a0c42ff 100644 --- a/background.js +++ b/background.js @@ -1,6 +1,8 @@ const browserTypeFirefox = 1; const browserTypeChrome = 2; +const storageTabCountKey = 'tab-count'; + const getBrowser = () => { try { return [browser, browserTypeFirefox]; @@ -13,14 +15,34 @@ const maxNumTabs = 10; const [browser, browserType] = getBrowser(); browser.tabs.onCreated.addListener(newTab => { - getTabs().then(tabs => { - const numTabsOpen = tabs.filter(tab => !tab.pinned).length; - if (numTabsOpen > maxNumTabs) { + getUnpinnedTabs().then(tabs => { + browser.storage.local.set({[storageTabCountKey]: tabs.length}); + if (tabs.length > maxNumTabs) { browser.tabs.remove(newTab.id); } }); }); +browser.tabs.onRemoved.addListener(tabId => { + // This listener can get fired before the tab is removed, so we'll need + // to explicitly filter out the removed tab. + getUnpinnedTabs() + .then(tabs => tabs.filter(tab => tab.id !== tabId)) + .then(tabs => { + browser.storage.local.set({[storageTabCountKey]: tabs.length}); + }); +}) + +browser.storage.onChanged.addListener(changes => { + if (changes.hasOwnProperty(storageTabCountKey)) { + browser.storage.local.get(storageTabCountKey, items => { + browser.browserAction.setBadgeText({ + text: items[storageTabCountKey].toString() + }); + }); + } +}); + const getTabs = () => { if (browserType === browserTypeChrome) { return new Promise((resolve, reject) => { diff --git a/manifest.json b/manifest.json index 9a786cc..000d37b 100644 --- a/manifest.json +++ b/manifest.json @@ -8,7 +8,8 @@ "96": "icons/icon96.png" }, "permissions": [ - "tabs" + "tabs", + "storage" ], "background": { "scripts": ["background.js"]