Something went wrong. Try again.
Cross-browser extension for limiting tabs
Something went wrong. Try again.
2.7 kB · 95 lines
JavaScript
at master
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596//// shared code - keep in-sync in options.js//
const localStorageGet = (keys) => { return browser.storage.local.get(keys);};
const defaultTabLimit = 10;const tabLimitStorageKey = "tab-limit";const getTabLimit = () => localStorageGet(tabLimitStorageKey).then(results => { return +results[tabLimitStorageKey] || defaultTabLimit;});
//// options code//
const storageTabCountKey = 'tab-count';
const badgeBackgroundColorGrey = '#636c72';const badgeBackgroundColorYellow = '#f0ad4e';const badgeBackgroundColorRed = '#d9534f';
browser.tabs.onCreated.addListener(newTab => { Promise.all([getUnpinnedTabs(), getTabLimit()]).then(([tabs, tabLimit]) => { if (tabs.length > tabLimit) { browser.tabs.remove(newTab.id); } setTabCountIntoStorage(tabs.length); });});
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 to make sure it's not factored // into the new tab count. getUnpinnedTabs() .then(tabs => tabs.filter(tab => tab.id !== tabId)) .then(tabs => setTabCountIntoStorage(tabs.length));});
browser.storage.onChanged.addListener(changes => { if (changes.hasOwnProperty(storageTabCountKey) || changes.hasOwnProperty(tabLimitStorageKey)) { onTabCountChange(); }});
const onTabCountChange = () => { getTabCountFromStorage().then(tabCount => { browser.browserAction.setBadgeText({text: tabCount.toString()}); getBadgeBackgroundColor(tabCount).then(backgroundColor => { browser.browserAction.setBadgeBackgroundColor({ color: backgroundColor, }); }); });};
const getBadgeBackgroundColor = tabCount => { return getTabLimit().then(tabLimit => { if (tabCount < tabLimit) { return badgeBackgroundColorGrey; } else if (tabCount === tabLimit) { return badgeBackgroundColorYellow; } else { return badgeBackgroundColorRed; } });};
const setTabCountIntoStorage = tabCount => { return browser.storage.local.set({[storageTabCountKey]: tabCount});};
const getTabCountFromStorage = () => { return localStorageGet(storageTabCountKey) .then(results => results[storageTabCountKey]);};
const getTabs = () => { return browser.tabs.query({});};
const getUnpinnedTabs = () => { return getTabs().then(tabs => tabs.filter(tab => !tab.pinned));};
browser.browserAction.setPopup({ popup: "src/options.html"})
getUnpinnedTabs() .then(tabs => setTabCountIntoStorage(tabs.length)) .then(() => onTabCountChange());