From 15f1ebc7ef9ea3c4e3f0a8ca887b784be2d3f2b9 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Mon, 19 Feb 2018 16:48:04 -0500 Subject: [PATCH] add ability to change tab limit --- background.js | 41 +++++++++++++++++++++++++++++------------ options.html | 15 +++++++++++++++ options.js | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 12 deletions(-) create mode 100644 options.html create mode 100644 options.js diff --git a/background.js b/background.js index b15a064..edc7d6f 100644 --- a/background.js +++ b/background.js @@ -15,11 +15,18 @@ const getBrowser = () => { } }; -const maxNumTabs = 10; +const defaultTabLimit = 10; + +const tabLimitStorageKey = "tab-limit"; + const [browser, browserType] = getBrowser(); +const getMaxNumTabs = () => localStorageGet(tabLimitStorageKey).then(results => { + return results[tabLimitStorageKey] || defaultTabLimit; +}); + browser.tabs.onCreated.addListener(newTab => { - getUnpinnedTabs().then(tabs => { + Promise.all([getUnpinnedTabs(), getMaxNumTabs()]).then(([tabs, maxNumTabs]) => { if (tabs.length > maxNumTabs) { browser.tabs.remove(newTab.id); } @@ -44,21 +51,27 @@ browser.storage.onChanged.addListener(changes => { const onTabCountChange = newTabCount => { getTabCountFromStorage().then(tabCount => { - browser.browserAction.setBadgeBackgroundColor({ - color: getBadgeBackgroundColor(tabCount), - }); browser.browserAction.setBadgeText({text: tabCount.toString()}); + getBadgeBackgroundColor(tabCount).then(backgroundColor => { + browser.browserAction.setBadgeBackgroundColor({ + color: backgroundColor, + }); + }); }); }; const getBadgeBackgroundColor = tabCount => { - if (tabCount < maxNumTabs) { - return badgeBackgroundColorGrey; - } else if (tabCount === maxNumTabs) { - return badgeBackgroundColorYellow; - } else { - return badgeBackgroundColorRed; - } + return new Promise((resolve, reject) => { + getMaxNumTabs().then(maxNumTabs => { + if (tabCount < maxNumTabs) { + return resolve(badgeBackgroundColorGrey); + } else if (tabCount === maxNumTabs) { + return resolve(badgeBackgroundColorYellow); + } else { + return resolve(badgeBackgroundColorRed); + } + }); + }); } const setTabCountIntoStorage = tabCount => { @@ -97,3 +110,7 @@ const getTabs = () => { const getUnpinnedTabs = () => { return getTabs().then(tabs => tabs.filter(tab => !tab.pinned)); }; + +browser.browserAction.setPopup({ + popup: "options.html" +}) diff --git a/options.html b/options.html new file mode 100644 index 0000000..9d87be2 --- /dev/null +++ b/options.html @@ -0,0 +1,15 @@ + + + + + + + +
+ + +
+ + + + diff --git a/options.js b/options.js new file mode 100644 index 0000000..e864a1d --- /dev/null +++ b/options.js @@ -0,0 +1,40 @@ +const browserTypeFirefox = 1; +const browserTypeChrome = 2; + +const getBrowser = () => { + try { + return [browser, browserTypeFirefox]; + } catch(ReferenceError) { + return [chrome, browserTypeChrome]; + } +}; + +const [browser, browserType] = getBrowser(); + +const defaultTabLimit = 10; + +const tabLimitStorageKey = "tab-limit"; + +/////// TODO: extract above + +const tabLimitInput = () => document.getElementById("tab-limit-input"); + +const tabLimitForm = () => document.getElementById("tab-limit-form"); + +const setTabLimitInputValue = (v) => tabLimitInput().value = v; + +const saveOptions = (e) => { + e.preventDefault(); + browser.storage.local.set({ + [tabLimitStorageKey]: tabLimitInput().value + }); +}; + +const restoreOptions = () => { + browser.storage.local.get(tabLimitStorageKey, (result) => { + setTabLimitInputValue(result[tabLimitStorageKey] || defaultTabLimit); + }); +}; + +document.addEventListener("DOMContentLoaded", restoreOptions); +tabLimitForm().addEventListener("submit", saveOptions); -- 2.51.2