Fix: prefs doesn't work in FF's private windows. Add web-ext. Drop prefs.readOnlyValues

This commit is contained in:
eight 2018-09-28 00:26:29 +08:00
parent 5f60c519ce
commit 39a6d1909f
9 changed files with 22 additions and 21 deletions

View File

@ -202,7 +202,7 @@ if (chrome.contextMenus) {
} }
item = Object.assign({id}, item); item = Object.assign({id}, item);
delete item.presentIf; delete item.presentIf;
const prefValue = prefs.readOnlyValues[id]; const prefValue = prefs.get(id);
item.title = chrome.i18n.getMessage(item.title); item.title = chrome.i18n.getMessage(item.title);
if (!item.type && typeof prefValue === 'boolean') { if (!item.type && typeof prefValue === 'boolean') {
item.type = 'checkbox'; item.type = 'checkbox';
@ -230,7 +230,7 @@ if (chrome.contextMenus) {
}; };
const keys = Object.keys(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); prefs.subscribe(keys.filter(id => contextMenus[id].presentIf), togglePresence);
createContextMenus(keys); createContextMenus(keys);
} }
@ -309,7 +309,7 @@ window.addEventListener('storageReady', function _() {
window.API_METHODS.getStylesForFrame = enabled ? getStylesForFrame : getStyles; window.API_METHODS.getStylesForFrame = enabled ? getStylesForFrame : getStyles;
}; };
prefs.subscribe(['exposeIframes'], updateAPI); 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}) { function webNavigationListener(method, {url, tabId, frameId}) {
Promise.all([ Promise.all([
getStyles({matchUrl: url, asHash: true}), getStyles({matchUrl: url, asHash: true}),
frameId && prefs.readOnlyValues.exposeIframes && getTab(tabId), frameId && prefs.get('exposeIframes') && getTab(tabId),
]).then(([styles, tab]) => { ]).then(([styles, tab]) => {
if (method && URLS.supported(url) && tabId >= 0) { if (method && URLS.supported(url) && tabId >= 0) {
if (method === 'styleApply') { if (method === 'styleApply') {

5
background/util.js Normal file
View File

@ -0,0 +1,5 @@
'use strict';
if (typeof localStorage === 'object' && localStorage) {
localStorage._BG_ACCESS = 1;
}

View File

@ -97,7 +97,7 @@ if (!BG || BG !== window) {
BG.API_METHODS = {}; 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) { if (FIREFOX_NO_DOM_STORAGE) {
// may be disabled via dom.storage.enabled // may be disabled via dom.storage.enabled
Object.defineProperty(window, 'localStorage', {value: {}}); Object.defineProperty(window, 'localStorage', {value: {}});

View File

@ -115,8 +115,7 @@ var prefs = new function Prefs() {
let broadcastPrefs = {}; let broadcastPrefs = {};
Object.defineProperties(this, { Object.defineProperties(this, {
defaults: {value: deepCopy(defaults)}, defaults: {value: deepCopy(defaults)}
readOnlyValues: {value: {}},
}); });
Object.assign(Prefs.prototype, { Object.assign(Prefs.prototype, {
@ -154,7 +153,6 @@ var prefs = new function Prefs() {
break; break;
} }
values[key] = value; values[key] = value;
defineReadonlyProperty(this.readOnlyValues, key, value);
const hasChanged = !equal(value, oldValue); const hasChanged = !equal(value, oldValue);
if (!fromBroadcast || FIREFOX_NO_DOM_STORAGE) { if (!fromBroadcast || FIREFOX_NO_DOM_STORAGE) {
localStorage[key] = typeof defaults[key] === 'object' localStorage[key] = typeof defaults[key] === 'object'
@ -231,13 +229,9 @@ var prefs = new function Prefs() {
{ {
const importFromBG = () => const importFromBG = () =>
API.getPrefs().then(prefs => { API.getPrefs().then(prefs => {
const props = {};
for (const id in prefs) { for (const id in prefs) {
const value = prefs[id]; this.set(id, prefs[id], {fromBroadcast: true});
values[id] = value;
props[id] = {value: deepCopy(value)};
} }
Object.defineProperties(this.readOnlyValues, props);
}); });
// Unlike chrome.storage or messaging, HTML5 localStorage is synchronous and always ready, // 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 // 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}); this.set(key, value, {broadcast: false, sync: false});
} else { } else {
values[key] = value; values[key] = value;
defineReadonlyProperty(this.readOnlyValues, key, value);
} }
} }
return Promise.resolve(); return Promise.resolve();
@ -404,7 +397,7 @@ var prefs = new function Prefs() {
// Accepts an array of pref names (values are fetched via prefs.get) // 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 // and establishes a two-way connection between the document elements and the actual prefs
function setupLivePrefs( function setupLivePrefs(
IDs = Object.getOwnPropertyNames(prefs.readOnlyValues) IDs = Object.getOwnPropertyNames(prefs.defaults)
.filter(id => $('#' + id)) .filter(id => $('#' + id))
) { ) {
const checkedProps = {}; const checkedProps = {};

View File

@ -114,7 +114,7 @@ onDOMready().then(() => {
} }
if (value !== undefined) { if (value !== undefined) {
el.lastValue = value; el.lastValue = value;
if (el.id in prefs.readOnlyValues) { if (el.id in prefs.defaults) {
prefs.set(el.id, false); prefs.set(el.id, false);
} }
} }

View File

@ -688,8 +688,8 @@ function highlightEditedStyle() {
function usePrefsDuringPageLoad() { function usePrefsDuringPageLoad() {
for (const id of Object.getOwnPropertyNames(prefs.readOnlyValues)) { for (const id of Object.getOwnPropertyNames(prefs.defaults)) {
const value = prefs.readOnlyValues[id]; const value = prefs.get(id);
if (value !== true) continue; if (value !== true) continue;
const el = document.getElementById(id) || const el = document.getElementById(id) ||
id.includes('expanded') && $(`details[data-pref="${id}"]`); id.includes('expanded') && $(`details[data-pref="${id}"]`);

View File

@ -23,6 +23,7 @@
], ],
"background": { "background": {
"scripts": [ "scripts": [
"background/util.js",
"js/messaging.js", "js/messaging.js",
"js/storage-util.js", "js/storage-util.js",
"js/sections-equal.js", "js/sections-equal.js",

View File

@ -57,7 +57,7 @@ document.onclick = e => {
case 'reset': case 'reset':
$$('input') $$('input')
.filter(input => input.id in prefs.readOnlyValues) .filter(input => input.id in prefs.defaults)
.forEach(input => prefs.reset(input.id)); .forEach(input => prefs.reset(input.id));
break; break;

View File

@ -17,7 +17,8 @@
"semver-bundle": "^0.1.1", "semver-bundle": "^0.1.1",
"stylelint-bundle": "^8.0.0", "stylelint-bundle": "^8.0.0",
"stylus-lang-bundle": "^0.54.5", "stylus-lang-bundle": "^0.54.5",
"updates": "^4.2.1" "updates": "^4.2.1",
"web-ext": "^2.9.1"
}, },
"scripts": { "scripts": {
"lint": "eslint **/*.js || true", "lint": "eslint **/*.js || true",
@ -27,6 +28,7 @@
"update-node": "updates -u && node tools/remove-modules.js && npm install", "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-codemirror": "node tools/update-libraries.js && node tools/update-codemirror-themes.js",
"update-versions": "node tools/update-versions", "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"
} }
} }