Something went wrong. Try again.
Cross-browser extension for limiting tabs
Something went wrong. Try again.
1.3 kB · 42 lines
JavaScript
at master
12345678910111213141516171819202122232425262728293031323334353637383940414243//// shared code - keep in-sync in background.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;});
//// background code//
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(); const inputValue = tabLimitInput().value; const newTabLimit = +inputValue > 0 ? +inputValue : defaultTabLimit; browser .storage .local .set({[tabLimitStorageKey]: newTabLimit}) .then(() => { // if the user enters an invalid value, make sure the form reflects the default value setTabLimitInputValue(newTabLimit); window.close(); });};
const restoreOptions = () => getTabLimit().then(setTabLimitInputValue);
document.addEventListener("DOMContentLoaded", restoreOptions);tabLimitForm().addEventListener("submit", saveOptions);