diff --git a/background/background.js b/background/background.js index 80f3f13a..98043d26 100644 --- a/background/background.js +++ b/background/background.js @@ -202,7 +202,7 @@ if (chrome.contextMenus) { } item = Object.assign({id}, item); delete item.presentIf; - const prefValue = prefs.readOnlyValues[id]; + const prefValue = prefs.get(id); item.title = chrome.i18n.getMessage(item.title); if (!item.type && typeof prefValue === 'boolean') { item.type = 'checkbox'; @@ -230,7 +230,7 @@ if (chrome.contextMenus) { }; const keys = Object.keys(contextMenus); - prefs.subscribe(keys.filter(id => typeof prefs.readOnlyValues[id] === 'boolean'), toggleCheckmark); + prefs.subscribe(keys.filter(id => typeof prefs.get(id) === 'boolean'), toggleCheckmark); prefs.subscribe(keys.filter(id => contextMenus[id].presentIf), togglePresence); createContextMenus(keys); } @@ -309,7 +309,7 @@ window.addEventListener('storageReady', function _() { window.API_METHODS.getStylesForFrame = enabled ? getStylesForFrame : getStyles; }; prefs.subscribe(['exposeIframes'], updateAPI); - updateAPI(null, prefs.readOnlyValues.exposeIframes); + updateAPI(null, prefs.get('exposeIframes')); } // ************************************************************************* @@ -317,7 +317,7 @@ window.addEventListener('storageReady', function _() { function webNavigationListener(method, {url, tabId, frameId}) { Promise.all([ getStyles({matchUrl: url, asHash: true}), - frameId && prefs.readOnlyValues.exposeIframes && getTab(tabId), + frameId && prefs.get('exposeIframes') && getTab(tabId), ]).then(([styles, tab]) => { if (method && URLS.supported(url) && tabId >= 0) { if (method === 'styleApply') { diff --git a/background/util.js b/background/util.js new file mode 100644 index 00000000..7b85af96 --- /dev/null +++ b/background/util.js @@ -0,0 +1,5 @@ +'use strict'; + +if (typeof localStorage === 'object' && localStorage) { + localStorage._BG_ACCESS = 1; +} diff --git a/js/messaging.js b/js/messaging.js index dcbc1dd7..b4b432de 100644 --- a/js/messaging.js +++ b/js/messaging.js @@ -97,7 +97,7 @@ if (!BG || BG !== window) { BG.API_METHODS = {}; } -const FIREFOX_NO_DOM_STORAGE = FIREFOX && !tryCatch(() => localStorage); +const FIREFOX_NO_DOM_STORAGE = FIREFOX && !tryCatch(() => localStorage && localStorage._BG_ACCESS); if (FIREFOX_NO_DOM_STORAGE) { // may be disabled via dom.storage.enabled Object.defineProperty(window, 'localStorage', {value: {}}); diff --git a/js/prefs.js b/js/prefs.js index b89da994..e92f28dd 100644 --- a/js/prefs.js +++ b/js/prefs.js @@ -115,8 +115,7 @@ var prefs = new function Prefs() { let broadcastPrefs = {}; Object.defineProperties(this, { - defaults: {value: deepCopy(defaults)}, - readOnlyValues: {value: {}}, + defaults: {value: deepCopy(defaults)} }); Object.assign(Prefs.prototype, { @@ -154,7 +153,6 @@ var prefs = new function Prefs() { break; } values[key] = value; - defineReadonlyProperty(this.readOnlyValues, key, value); const hasChanged = !equal(value, oldValue); if (!fromBroadcast || FIREFOX_NO_DOM_STORAGE) { localStorage[key] = typeof defaults[key] === 'object' @@ -231,13 +229,9 @@ var prefs = new function Prefs() { { const importFromBG = () => API.getPrefs().then(prefs => { - const props = {}; for (const id in prefs) { - const value = prefs[id]; - values[id] = value; - props[id] = {value: deepCopy(value)}; + this.set(id, prefs[id], {fromBroadcast: true}); } - Object.defineProperties(this.readOnlyValues, props); }); // Unlike chrome.storage or messaging, HTML5 localStorage is synchronous and always ready, // so we'll mirror the prefs to avoid using the wrong defaults during the startup phase @@ -270,7 +264,6 @@ var prefs = new function Prefs() { this.set(key, value, {broadcast: false, sync: false}); } else { values[key] = value; - defineReadonlyProperty(this.readOnlyValues, key, value); } } return Promise.resolve(); @@ -404,7 +397,7 @@ var prefs = new function Prefs() { // Accepts an array of pref names (values are fetched via prefs.get) // and establishes a two-way connection between the document elements and the actual prefs function setupLivePrefs( - IDs = Object.getOwnPropertyNames(prefs.readOnlyValues) + IDs = Object.getOwnPropertyNames(prefs.defaults) .filter(id => $('#' + id)) ) { const checkedProps = {}; diff --git a/manage/filters.js b/manage/filters.js index 24bb8e7b..c1619687 100644 --- a/manage/filters.js +++ b/manage/filters.js @@ -114,7 +114,7 @@ onDOMready().then(() => { } if (value !== undefined) { el.lastValue = value; - if (el.id in prefs.readOnlyValues) { + if (el.id in prefs.defaults) { prefs.set(el.id, false); } } diff --git a/manage/manage.js b/manage/manage.js index 01b005f8..9160d18d 100644 --- a/manage/manage.js +++ b/manage/manage.js @@ -688,8 +688,8 @@ function highlightEditedStyle() { function usePrefsDuringPageLoad() { - for (const id of Object.getOwnPropertyNames(prefs.readOnlyValues)) { - const value = prefs.readOnlyValues[id]; + for (const id of Object.getOwnPropertyNames(prefs.defaults)) { + const value = prefs.get(id); if (value !== true) continue; const el = document.getElementById(id) || id.includes('expanded') && $(`details[data-pref="${id}"]`); diff --git a/manifest.json b/manifest.json index 2a6a3ad2..055e4533 100644 --- a/manifest.json +++ b/manifest.json @@ -23,6 +23,7 @@ ], "background": { "scripts": [ + "background/util.js", "js/messaging.js", "js/storage-util.js", "js/sections-equal.js", diff --git a/options/options.js b/options/options.js index fba0903c..6cbe0054 100644 --- a/options/options.js +++ b/options/options.js @@ -57,7 +57,7 @@ document.onclick = e => { case 'reset': $$('input') - .filter(input => input.id in prefs.readOnlyValues) + .filter(input => input.id in prefs.defaults) .forEach(input => prefs.reset(input.id)); break; diff --git a/package.json b/package.json index 8ebd7796..07a08b7b 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,8 @@ "semver-bundle": "^0.1.1", "stylelint-bundle": "^8.0.0", "stylus-lang-bundle": "^0.54.5", - "updates": "^4.2.1" + "updates": "^4.2.1", + "web-ext": "^2.9.1" }, "scripts": { "lint": "eslint **/*.js || true", @@ -27,6 +28,7 @@ "update-node": "updates -u && node tools/remove-modules.js && npm install", "update-codemirror": "node tools/update-libraries.js && node tools/update-codemirror-themes.js", "update-versions": "node tools/update-versions", - "zip": "npm run update-versions && node tools/zip.js" + "zip": "npm run update-versions && node tools/zip.js", + "start-firefox": "web-ext run" } }