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);
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') {

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 = {};
}
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: {}});

View File

@ -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 = {};

View File

@ -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);
}
}

View File

@ -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}"]`);

View File

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

View File

@ -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;

View File

@ -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"
}
}