2017-09-24 00:12:12 +02:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2017-11-07 00:36:44 +01:00
|
|
|
(function(){
|
2017-09-24 00:12:12 +02:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var scope;
|
|
|
|
if ((typeof exports) !== "undefined"){
|
|
|
|
scope = exports;
|
|
|
|
}
|
|
|
|
else {
|
2017-11-07 00:36:44 +01:00
|
|
|
scope = {};
|
|
|
|
window.scope.settings = scope;
|
2017-09-24 00:12:12 +02:00
|
|
|
}
|
|
|
|
|
2017-11-07 00:36:44 +01:00
|
|
|
var logging = {};
|
|
|
|
(function(){
|
|
|
|
var loggingQueue = [];
|
|
|
|
require.on("./logging", function(realLogging){
|
|
|
|
logging = realLogging;
|
|
|
|
|
|
|
|
loggingQueue.forEach(function(logEntry){
|
|
|
|
logging[logEntry.name](...logEntry.args, logEntry.date);
|
|
|
|
});
|
|
|
|
loggingQueue = [];
|
|
|
|
});
|
|
|
|
["error", "warning", "message", "notice", "verbose"].forEach(function(name){
|
|
|
|
logging[name] = function(...args){
|
|
|
|
loggingQueue.push({name, args, date: new Date()});
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}());
|
|
|
|
const settingDefinitions = require("./settingDefinitions.js");
|
2017-12-03 23:47:49 +01:00
|
|
|
const definitionsByName = {};
|
|
|
|
const defaultSymbol = "";
|
2017-11-07 00:36:44 +01:00
|
|
|
|
2017-12-03 23:47:49 +01:00
|
|
|
const eventHandler = {any: {}};
|
|
|
|
eventHandler.any[defaultSymbol] = [];
|
2017-11-07 00:36:44 +01:00
|
|
|
eventHandler.all = eventHandler.any;
|
|
|
|
const settings = {};
|
2017-12-03 23:47:49 +01:00
|
|
|
let urlContainer;
|
2018-07-02 00:29:41 +02:00
|
|
|
let hideContainer;
|
2017-09-24 00:12:12 +02:00
|
|
|
|
2017-12-03 23:47:49 +01:00
|
|
|
function isDefinitionInvalid(settingDefinition, newValue){
|
|
|
|
if (newValue === undefined && settingDefinition.optional){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if (settingDefinition.fixed){
|
|
|
|
return "fixed";
|
|
|
|
}
|
|
|
|
else if ((typeof newValue) !== (typeof settingDefinition.defaultValue)){
|
|
|
|
return "wrongType";
|
|
|
|
}
|
|
|
|
else if (Array.isArray(settingDefinition.defaultValue)){
|
|
|
|
if (!Array.isArray(newValue)){
|
2017-11-14 01:07:27 +01:00
|
|
|
return "wrongType";
|
2017-11-07 00:36:44 +01:00
|
|
|
}
|
2017-12-03 23:47:49 +01:00
|
|
|
var entriesInvalid = newValue.reduce(function(v, entry){
|
|
|
|
v = v || settingDefinition.entries.reduce(function(v, entryDefinition){
|
|
|
|
return v || isDefinitionInvalid(entryDefinition, entry[entryDefinition.name]);
|
|
|
|
}, false);
|
|
|
|
if (!v){
|
|
|
|
if (Object.keys(entry).some(function(key){
|
|
|
|
return !settingDefinition.entries.some(function(entryDefinition){
|
|
|
|
return key === entryDefinition.name;
|
|
|
|
});
|
|
|
|
})){
|
|
|
|
return "noOption";
|
|
|
|
}
|
2017-11-14 01:07:27 +01:00
|
|
|
}
|
2017-12-03 23:47:49 +01:00
|
|
|
return v;
|
|
|
|
}, false);
|
|
|
|
if (entriesInvalid){
|
|
|
|
return entriesInvalid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (
|
|
|
|
settingDefinition.options &&
|
|
|
|
!settingDefinition.options.includes(newValue)
|
|
|
|
){
|
|
|
|
return "noOption";
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function createGetter(settingDefinition){
|
|
|
|
if (settingDefinition.dynamic){
|
|
|
|
return function getValue(){
|
|
|
|
return settingDefinition.getter(scope);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else if (settingDefinition.urlSpecific){
|
|
|
|
return function getValue(url){
|
|
|
|
if (url){
|
|
|
|
var matching = urlContainer.get().filter(function(urlSetting){
|
|
|
|
return urlSetting.hasOwnProperty(settingDefinition.name);
|
|
|
|
}).filter(function(urlSetting){
|
|
|
|
return urlSetting.match(url);
|
|
|
|
});
|
|
|
|
if (matching.length){
|
|
|
|
return matching[0][settingDefinition.name];
|
|
|
|
}
|
2017-11-14 01:07:27 +01:00
|
|
|
}
|
2017-12-03 23:47:49 +01:00
|
|
|
return settings[settingDefinition.name];
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return function getValue(){
|
|
|
|
return settings[settingDefinition.name];
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function createSetter(settingDefinition){
|
|
|
|
if (settingDefinition.dynamic){
|
|
|
|
return function setValue(newValue){
|
|
|
|
settingDefinition.setter(scope);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const name = settingDefinition.name;
|
|
|
|
const isValid = function isValid(newValue){
|
|
|
|
var invalid = settingDefinition.invalid(newValue);
|
|
|
|
if (invalid){
|
|
|
|
if (invalid === "fixed"){
|
|
|
|
logging.warning("Trying to set the fixed setting", name, ":", newValue);
|
|
|
|
}
|
|
|
|
else if (invalid === "wrongType"){
|
|
|
|
logging.warning("Wrong type provided for setting", name, ":", newValue);
|
|
|
|
}
|
|
|
|
else if (invalid === "noOption"){
|
|
|
|
logging.warning("Provided value outside specified options for ", name, ":", newValue);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
logging.warning("Unknown invalid state:", invalid);
|
|
|
|
}
|
|
|
|
return false;
|
2017-11-14 01:07:27 +01:00
|
|
|
}
|
2017-12-03 23:47:49 +01:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
const storeValue = function storeValue(newValue){
|
2018-07-12 01:18:49 +02:00
|
|
|
logging.verbose("Trying to store new value for %s", name, newValue);
|
2017-11-10 23:45:09 +01:00
|
|
|
settings[name] = newValue;
|
|
|
|
if (!settingDefinition.transient){
|
|
|
|
var storeObject = {};
|
|
|
|
storeObject[name] = newValue;
|
2018-07-17 12:54:10 +02:00
|
|
|
var promise = browser.storage.local.set(storeObject);
|
|
|
|
promise.then(function(){
|
2018-07-12 01:18:49 +02:00
|
|
|
logging.verbose("New value stored for %s:", name, newValue);
|
|
|
|
}, function(err){
|
|
|
|
logging.warning("Unable to store new value for %s:", name, newValue, err);
|
|
|
|
});
|
2018-07-17 12:54:10 +02:00
|
|
|
return promise;
|
2018-07-12 01:18:49 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
logging.warning("Transient setting %s cannot be stored.", name);
|
2017-11-10 23:45:09 +01:00
|
|
|
}
|
2017-12-03 23:47:49 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
if (settingDefinition.urlSpecific){
|
|
|
|
return function setValue(newValue, url){
|
2018-07-12 01:18:49 +02:00
|
|
|
logging.verbose("New value for %s (%s):", name, url, newValue);
|
2017-12-03 23:47:49 +01:00
|
|
|
if (isValid(newValue)){
|
|
|
|
if (url){
|
|
|
|
var urlContainerValue = urlContainer.get();
|
|
|
|
var matching = urlContainerValue.filter(function(urlSetting){
|
|
|
|
return urlSetting.match(url);
|
|
|
|
});
|
|
|
|
if (!matching.length){
|
|
|
|
let newEntry = {url};
|
|
|
|
newEntry[settingDefinition.name] = newValue;
|
|
|
|
urlContainerValue.push(newEntry);
|
|
|
|
matching = [newEntry];
|
|
|
|
}
|
|
|
|
matching[0][settingDefinition.name] = newValue;
|
2018-07-17 12:54:10 +02:00
|
|
|
return urlContainer.set(urlContainerValue);
|
2017-12-03 23:47:49 +01:00
|
|
|
}
|
|
|
|
else {
|
2018-07-17 12:54:10 +02:00
|
|
|
return storeValue(newValue);
|
2017-12-03 23:47:49 +01:00
|
|
|
}
|
|
|
|
}
|
2018-07-12 01:18:49 +02:00
|
|
|
else{
|
|
|
|
logging.warning("Invalid value for %s (%s):", name, url, newValue);
|
|
|
|
}
|
2017-12-03 23:47:49 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return function setValue(newValue){
|
|
|
|
logging.verbose("New value for %s:", name, newValue);
|
|
|
|
if (isValid(newValue)){
|
2018-07-17 12:54:10 +02:00
|
|
|
return storeValue(newValue);
|
2017-12-03 23:47:49 +01:00
|
|
|
}
|
2018-07-12 01:18:49 +02:00
|
|
|
else{
|
|
|
|
logging.warning("Invalid value for %s:", name, newValue);
|
|
|
|
}
|
2017-12-03 23:47:49 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function createResetter(settingDefinition){
|
|
|
|
if (settingDefinition.dynamic){
|
|
|
|
return function(){};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const name = settingDefinition.name;
|
|
|
|
let reset = function(){
|
|
|
|
settings[name] = settingDefinition.defaultValue;
|
|
|
|
browser.storage.local.remove(name);
|
|
|
|
};
|
|
|
|
if (settingDefinition.urlSpecific){
|
|
|
|
return function(url){
|
|
|
|
if (url){
|
|
|
|
var urlContainerValue = urlContainer.get();
|
|
|
|
var matching = urlContainerValue.filter(function(urlSetting){
|
|
|
|
return urlSetting.match(url);
|
|
|
|
});
|
|
|
|
if (matching.length){
|
|
|
|
delete matching[0][name];
|
|
|
|
if (Object.keys(matching[0]).every(function(key){return key === "url";})){
|
|
|
|
urlContainerValue = urlContainerValue.filter(function(urlSetting){
|
|
|
|
return urlSetting !== matching[0];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
urlContainer.set(urlContainerValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
};
|
2017-11-10 23:45:09 +01:00
|
|
|
}
|
2017-12-03 23:47:49 +01:00
|
|
|
else {
|
|
|
|
return reset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 00:29:41 +02:00
|
|
|
scope.on = function onSettingsChange(name, callback, url){
|
|
|
|
if (Array.isArray(name)){
|
|
|
|
name.forEach(function(name){
|
|
|
|
onSettingsChange(name, callback, url);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (eventHandler.hasOwnProperty(name)){
|
|
|
|
if (!url){
|
|
|
|
url = defaultSymbol;
|
|
|
|
}
|
|
|
|
if (!eventHandler[name].hasOwnProperty(url)){
|
|
|
|
eventHandler[name][url] = [];
|
|
|
|
}
|
|
|
|
eventHandler[name][url].push(callback);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
logging.warning("Unable to register event handler for unknown setting", name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-12-03 23:47:49 +01:00
|
|
|
settingDefinitions.forEach(function(settingDefinition){
|
|
|
|
if (settingDefinition.urlContainer){
|
|
|
|
urlContainer = settingDefinition;
|
|
|
|
settingDefinition.refresh = function(){
|
|
|
|
settingDefinition.set(settingDefinition.get());
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
var name = settingDefinition.name;
|
|
|
|
definitionsByName[name] = settingDefinition;
|
2018-06-21 00:03:17 +02:00
|
|
|
if (typeof settingDefinition.defaultValue === "function"){
|
|
|
|
settingDefinition.defaultValue = settingDefinition.defaultValue();
|
|
|
|
}
|
2017-12-03 23:47:49 +01:00
|
|
|
settings[name] = settingDefinition.defaultValue;
|
|
|
|
eventHandler[name] = {};
|
|
|
|
|
|
|
|
settingDefinition.on = function on(callback, url){
|
|
|
|
if (!settingDefinition.dynamic){
|
|
|
|
scope.on(name, callback, url);
|
|
|
|
}
|
|
|
|
if (settingDefinition.dependencies){
|
|
|
|
settingDefinition.dependencies.forEach(function(dependency){
|
|
|
|
scope.on(dependency, function(){
|
|
|
|
callback({name, newValue: settingDefinition.get()});
|
|
|
|
}, url);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
settingDefinition.invalid = function invalid(newValue){
|
|
|
|
return isDefinitionInvalid(settingDefinition, newValue);
|
2017-11-07 00:36:44 +01:00
|
|
|
};
|
2017-12-03 23:47:49 +01:00
|
|
|
settingDefinition.get = createGetter(settingDefinition);
|
|
|
|
|
|
|
|
settingDefinition.set = createSetter(settingDefinition);
|
|
|
|
|
|
|
|
settingDefinition.reset = createResetter(settingDefinition);
|
2018-07-02 00:29:41 +02:00
|
|
|
|
2017-12-03 23:47:49 +01:00
|
|
|
if (settingDefinition.urlSpecific){
|
|
|
|
if (!urlContainer){
|
|
|
|
logging.error("Unable to use url specific settings without url-container");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
settingDefinition.urlContainer = urlContainer;
|
|
|
|
let entry = Object.create(settingDefinition);
|
|
|
|
entry.optional = true;
|
|
|
|
urlContainer.entries.push(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-07 00:36:44 +01:00
|
|
|
Object.defineProperty(
|
|
|
|
scope,
|
|
|
|
name,
|
|
|
|
{
|
|
|
|
get: settingDefinition.get,
|
2017-11-07 19:51:49 +01:00
|
|
|
set: settingDefinition.set,
|
|
|
|
enumerable: true
|
2017-11-07 00:36:44 +01:00
|
|
|
}
|
2017-09-24 00:12:12 +02:00
|
|
|
);
|
2018-07-02 00:29:41 +02:00
|
|
|
|
|
|
|
if (settingDefinition.hideContainer){
|
|
|
|
hideContainer = settingDefinition;
|
|
|
|
let changeListeners = {};
|
|
|
|
settingDefinition.setHideByName = function(name, value){
|
|
|
|
logging.verbose("set hide of", name, "to", value);
|
|
|
|
const hideStore = settingDefinition.get();
|
|
|
|
hideStore[name] = value;
|
|
|
|
settingDefinition.set(hideStore);
|
|
|
|
(changeListeners[name] || []).forEach(function(listener){
|
|
|
|
listener(value);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
settingDefinition.getHideByName = function(name){
|
|
|
|
const hideStore = settingDefinition.get();
|
|
|
|
return hideStore[name] || false;
|
|
|
|
};
|
|
|
|
settingDefinition.onHideChange = function(name, listener){
|
|
|
|
if (!changeListeners[name]){
|
|
|
|
changeListeners[name] = [];
|
|
|
|
}
|
|
|
|
changeListeners[name].push(listener);
|
|
|
|
};
|
|
|
|
settingDefinition.on(function(event){
|
|
|
|
const value = event.newValue;
|
|
|
|
Object.keys(value).forEach(function(name){
|
|
|
|
if (value[name]){
|
|
|
|
(changeListeners[name] || []).forEach(function(listener){
|
|
|
|
listener(true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
settingDefinition.hideAble = false;
|
|
|
|
}
|
2017-09-24 00:12:12 +02:00
|
|
|
});
|
|
|
|
|
2017-11-07 00:36:44 +01:00
|
|
|
scope.getDefinition = function(name){
|
2017-12-03 23:47:49 +01:00
|
|
|
var foundDefinition = definitionsByName[name];
|
|
|
|
if (foundDefinition){
|
|
|
|
return Object.create(foundDefinition);
|
2017-11-07 00:36:44 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
};
|
2018-07-02 00:29:41 +02:00
|
|
|
|
|
|
|
scope.getContainers = function(){
|
|
|
|
return {
|
|
|
|
url: Object.create(urlContainer),
|
|
|
|
hide: Object.create(hideContainer)
|
|
|
|
};
|
|
|
|
};
|
2017-11-07 00:36:44 +01:00
|
|
|
|
2017-12-03 23:47:49 +01:00
|
|
|
scope.set = function(name, ...args){
|
|
|
|
var foundDefinition = definitionsByName[name];
|
|
|
|
if (foundDefinition){
|
|
|
|
return foundDefinition.set(...args);
|
|
|
|
}
|
|
|
|
else {
|
2018-08-22 22:16:49 +02:00
|
|
|
logging.error("Try to set unknown setting:", name);
|
2017-12-03 23:47:49 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
scope.get = function(name, ...args){
|
|
|
|
var foundDefinition = definitionsByName[name];
|
|
|
|
if (foundDefinition){
|
|
|
|
return foundDefinition.get(...args);
|
|
|
|
}
|
|
|
|
else {
|
2018-08-22 22:16:49 +02:00
|
|
|
logging.error("Try to get unknown setting:", name);
|
2017-12-03 23:47:49 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-09-24 00:12:12 +02:00
|
|
|
scope.forEach = function forEachSetting(...args){
|
2017-12-03 23:47:49 +01:00
|
|
|
settingDefinitions.filter(function(settingDefinition){
|
|
|
|
return !settingDefinition.dynamic;
|
|
|
|
}).map(function(settingDefinition){
|
2017-09-24 00:12:12 +02:00
|
|
|
return Object.create(settingDefinition);
|
|
|
|
}).forEach(...args);
|
2017-11-07 00:36:44 +01:00
|
|
|
};
|
2017-12-03 23:47:49 +01:00
|
|
|
|
|
|
|
const resetSymbol = Symbol("reset");
|
2017-11-07 00:36:44 +01:00
|
|
|
function changeValue(name, newValue){
|
2017-12-03 23:47:49 +01:00
|
|
|
var settingDefinition = scope.getDefinition(name);
|
2017-12-19 23:04:03 +01:00
|
|
|
if (settingDefinition){
|
|
|
|
var oldValue = settings[name];
|
|
|
|
if (newValue === resetSymbol){
|
|
|
|
newValue = settingDefinition.defaultValue;
|
|
|
|
}
|
|
|
|
settings[name] = newValue;
|
|
|
|
((eventHandler[name] || {})[defaultSymbol] || []).forEach(function(callback){
|
|
|
|
callback({name, newValue, oldValue});
|
2017-12-03 23:47:49 +01:00
|
|
|
});
|
2017-12-19 23:04:03 +01:00
|
|
|
|
|
|
|
if (settingDefinition.urlSpecific){
|
|
|
|
urlContainer.get().forEach(function(entry){
|
|
|
|
if (!entry.hasOwnProperty(name)){
|
|
|
|
((eventHandler[name] || {})[entry.url] || []).forEach(function(callback){
|
|
|
|
callback({name, newValue, oldValue, url: entry.url});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-12-03 23:47:49 +01:00
|
|
|
}
|
2017-09-24 00:12:12 +02:00
|
|
|
}
|
2017-11-07 00:36:44 +01:00
|
|
|
|
|
|
|
logging.verbose("registering storage onchange listener");
|
|
|
|
browser.storage.onChanged.addListener(function(changes, area){
|
|
|
|
if (area === "local"){
|
|
|
|
logging.notice("settings changed", changes);
|
2017-12-03 23:47:49 +01:00
|
|
|
var delayedChange = [];
|
2017-11-07 00:36:44 +01:00
|
|
|
Object.entries(changes).forEach(function(entry){
|
|
|
|
const [name, change] = entry;
|
2017-12-03 23:47:49 +01:00
|
|
|
if (urlContainer && name === urlContainer.name){
|
|
|
|
// changes in the url container have to trigger after the other changes
|
|
|
|
delayedChange.push(entry);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (change.hasOwnProperty("newValue")){
|
|
|
|
changeValue(name, change.newValue);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
changeValue(name, resetSymbol);
|
|
|
|
}
|
|
|
|
}
|
2017-11-07 00:36:44 +01:00
|
|
|
});
|
2017-12-03 23:47:49 +01:00
|
|
|
delayedChange.forEach(function(entry){
|
|
|
|
const [name, change] = entry;
|
|
|
|
if (change.hasOwnProperty("newValue")){
|
|
|
|
changeValue(name, change.newValue);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
changeValue(name, resetSymbol);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
eventHandler.any[""].forEach(function(callback){
|
2017-11-07 00:36:44 +01:00
|
|
|
callback();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2017-11-10 23:45:09 +01:00
|
|
|
|
2017-12-03 23:47:49 +01:00
|
|
|
if (urlContainer){
|
|
|
|
urlContainer.on(function({newValue, oldValue}){
|
|
|
|
newValue.forEach(function(urlSetting){
|
|
|
|
var regExp;
|
2018-09-06 08:34:08 +02:00
|
|
|
var domain = !!urlSetting.url.match(/^[A-Za-z0-9_.-]+$/);
|
2017-12-03 23:47:49 +01:00
|
|
|
if (domain){
|
|
|
|
regExp = new RegExp(
|
|
|
|
"(?:^|\\.)" + urlSetting.url.replace(/([\\+*?[^\]$(){}=!|.])/g, "\\$1") + "\\.?$",
|
|
|
|
"i"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
regExp = new RegExp(urlSetting.url, "i");
|
|
|
|
}
|
|
|
|
const match = function(url){
|
|
|
|
if (!url){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if (
|
|
|
|
url instanceof String ||
|
|
|
|
(typeof url) === "string"
|
|
|
|
){
|
|
|
|
return url === urlSetting.url;
|
|
|
|
}
|
|
|
|
else if (domain){
|
|
|
|
return (url.hostname || "").match(regExp);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return url.href.match(regExp);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Object.defineProperty(
|
|
|
|
urlSetting,
|
|
|
|
"match",
|
|
|
|
{
|
|
|
|
enumerable: false,
|
|
|
|
writable: true,
|
|
|
|
configurable: true,
|
|
|
|
value: match
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
var newUrls = newValue.map(function(entry){return entry.url;});
|
|
|
|
var oldUrls = oldValue.map(function(entry){return entry.url;});
|
|
|
|
var matching = {};
|
|
|
|
newUrls.forEach(function(url, i){
|
|
|
|
matching[url] = {new: i, old: oldUrls.indexOf(url)};
|
|
|
|
});
|
|
|
|
oldUrls.forEach(function(url, i){
|
|
|
|
if (!matching[url]){
|
|
|
|
matching[url] = {new: -1, old: i};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
Object.keys(matching).forEach(function(url){
|
|
|
|
var oldEntry = oldValue[matching[url].old] || {};
|
|
|
|
var newEntry = newValue[matching[url].new] || {};
|
|
|
|
urlContainer.entries.forEach(function(settingDefinition){
|
|
|
|
var name = settingDefinition.name;
|
|
|
|
var oldValue = oldEntry[name];
|
|
|
|
var newValue = newEntry[name];
|
|
|
|
|
|
|
|
if (oldValue !== newValue){
|
|
|
|
((eventHandler[name] || {})[url] || []).forEach(function(callback){
|
|
|
|
callback({name, newValue, oldValue, url});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-11-10 23:45:09 +01:00
|
|
|
const settingsMigration = {
|
2018-09-07 23:52:29 +02:00
|
|
|
validVersions: [undefined, 0.1, 0.2, 0.3, 0.4],
|
2017-11-10 23:45:09 +01:00
|
|
|
transitions: {
|
|
|
|
"": function(oldStorage){
|
|
|
|
return {
|
|
|
|
storageVersion: 0.2
|
|
|
|
};
|
|
|
|
},
|
|
|
|
0.1: function(oldStorage){
|
|
|
|
var newStorage = {
|
|
|
|
storageVersion: 0.2
|
|
|
|
};
|
|
|
|
if (oldStorage.hasOwnProperty("askOnlyOnce")){
|
|
|
|
newStorage.askOnlyOnce = oldStorage.askOnlyOnce? "individual": "no";
|
|
|
|
}
|
2017-12-03 23:47:49 +01:00
|
|
|
return newStorage;
|
|
|
|
},
|
|
|
|
0.2: function(oldStorage){
|
|
|
|
var newStorage = {
|
|
|
|
storageVersion: 0.3,
|
|
|
|
urlSettings: (
|
|
|
|
oldStorage.urlSettings &&
|
|
|
|
Array.isArray(oldStorage.urlSettings)
|
|
|
|
)? oldStorage.urlSettings: []
|
|
|
|
};
|
|
|
|
|
|
|
|
var urlSettings = {};
|
|
|
|
|
2017-12-08 20:25:08 +01:00
|
|
|
(oldStorage.blackList || "").split(",")
|
2017-12-19 23:00:35 +01:00
|
|
|
.map(function(url){return url.trim();})
|
|
|
|
.filter(function(url){return !!url;})
|
2017-12-08 20:25:08 +01:00
|
|
|
.forEach(function(url){
|
|
|
|
var entry = urlSettings[url];
|
|
|
|
if (!entry){
|
|
|
|
entry = {url, blockMode: "block"};
|
|
|
|
urlSettings[url] = entry;
|
|
|
|
newStorage.urlSettings.push(entry);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
(oldStorage.whiteList || "").split(",")
|
2017-12-19 23:00:35 +01:00
|
|
|
.map(function(url){return url.trim();})
|
|
|
|
.filter(function(url){return !!url;})
|
2017-12-08 20:25:08 +01:00
|
|
|
.forEach(function(url){
|
|
|
|
var entry = urlSettings[url];
|
|
|
|
if (!entry){
|
|
|
|
entry = {url, blockMode: "allow"};
|
|
|
|
urlSettings[url] = entry;
|
|
|
|
newStorage.urlSettings.push(entry);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
(oldStorage.ignoreList || "").split(",")
|
2017-12-19 23:00:35 +01:00
|
|
|
.map(function(url){return url.trim();})
|
|
|
|
.filter(function(url){return !!url;})
|
2017-12-08 20:25:08 +01:00
|
|
|
.forEach(function(url){
|
|
|
|
var entry = urlSettings[url];
|
|
|
|
if (!entry){
|
|
|
|
entry = {url, showNotifications: false};
|
|
|
|
urlSettings[url] = entry;
|
|
|
|
newStorage.urlSettings.push(entry);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
entry.showNotifications = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
["whiteList", "blackList", "ignoreList"].forEach(function(list){
|
|
|
|
if (oldStorage.hasOwnProperty(list)){
|
|
|
|
newStorage[list] = "";
|
2017-12-03 23:47:49 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-11-10 23:45:09 +01:00
|
|
|
return newStorage;
|
2018-09-07 23:52:29 +02:00
|
|
|
},
|
|
|
|
0.3: function(oldStorage){
|
|
|
|
var newStorage = {
|
|
|
|
storageVersion: 0.4
|
|
|
|
};
|
|
|
|
if (oldStorage.hasOwnProperty("apiWhiteList")){
|
|
|
|
const protectedAPIFeatures = {};
|
|
|
|
Object.keys(oldStorage.apiWhiteList).forEach(function(key){
|
|
|
|
protectedAPIFeatures[key] = !oldStorage.apiWhiteList[key];
|
|
|
|
});
|
|
|
|
newStorage.protectedAPIFeatures = protectedAPIFeatures;
|
|
|
|
}
|
|
|
|
return newStorage;
|
|
|
|
},
|
2017-11-10 23:45:09 +01:00
|
|
|
}
|
|
|
|
};
|
2017-11-07 00:36:44 +01:00
|
|
|
|
|
|
|
logging.verbose("loading settings");
|
2017-11-27 12:28:01 +01:00
|
|
|
let initialized = false;
|
|
|
|
const initEvents = [];
|
|
|
|
scope.init = function(storage){
|
|
|
|
if (initialized){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
initialized = true;
|
2017-11-07 00:36:44 +01:00
|
|
|
logging.message("settings loaded");
|
2017-11-10 23:45:09 +01:00
|
|
|
if (!storage.storageVersion){
|
|
|
|
logging.message("No storage version found. Initializing storage.");
|
|
|
|
browser.storage.local.remove(Object.keys(storage));
|
|
|
|
storage = settingsMigration.transitions[""]({});
|
|
|
|
browser.storage.local.set(storage);
|
|
|
|
}
|
|
|
|
else if (storage.storageVersion !== settings.storageVersion){
|
|
|
|
var toChange = {};
|
|
|
|
while (storage.storageVersion !== settings.storageVersion){
|
2018-09-07 23:52:29 +02:00
|
|
|
logging.message("Old storage found (",
|
|
|
|
storage.storageVersion, "expected", settings.storageVersion,
|
|
|
|
")");
|
2017-11-10 23:45:09 +01:00
|
|
|
if (settingsMigration.transitions[storage.storageVersion]){
|
|
|
|
var changes = settingsMigration.transitions[storage.storageVersion](storage);
|
|
|
|
Object.entries(changes).forEach(function(entry){
|
|
|
|
const [name, value] = entry;
|
|
|
|
toChange[name] = value;
|
|
|
|
storage[name] = value;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
logging.warning("Unable to migrate storage.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logging.notice("Changed settings:", toChange);
|
|
|
|
browser.storage.local.set(toChange);
|
|
|
|
}
|
2017-12-03 23:47:49 +01:00
|
|
|
var delayedChange = [];
|
2017-11-07 00:36:44 +01:00
|
|
|
Object.entries(storage).forEach(function(entry){
|
2017-12-03 23:47:49 +01:00
|
|
|
const [name, value] = entry;
|
|
|
|
if (urlContainer && name === urlContainer.name){
|
|
|
|
// changes in the url container have to trigger after the other changes
|
|
|
|
delayedChange.push(entry);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
changeValue(name, value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
delayedChange.forEach(function(entry){
|
2017-11-07 00:36:44 +01:00
|
|
|
const [name, value] = entry;
|
|
|
|
changeValue(name, value);
|
|
|
|
});
|
|
|
|
changeValue("isStillDefault", false);
|
2017-11-27 12:28:01 +01:00
|
|
|
|
|
|
|
initEvents.forEach(function(callback){callback();});
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
scope.loaded = browser.storage.local.get().then(scope.init);
|
2017-11-07 00:36:44 +01:00
|
|
|
scope.onloaded = function(callback){
|
|
|
|
if (scope.isStillDefault){
|
2017-11-27 12:28:01 +01:00
|
|
|
initEvents.push(callback);
|
2017-11-07 00:36:44 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
};
|
2017-12-04 00:26:26 +01:00
|
|
|
scope.forceLoad = function(){
|
|
|
|
while (settings.isStillDefault){
|
|
|
|
logging.message("Starting synchronous request to wait for settings.");
|
|
|
|
try {
|
|
|
|
let xhr = new XMLHttpRequest();
|
|
|
|
xhr.open("GET", "https://[::]", false);
|
|
|
|
xhr.send();
|
|
|
|
xhr = null;
|
|
|
|
}
|
|
|
|
catch (e){
|
|
|
|
logging.verbose("Error in XHR:", e);
|
|
|
|
}
|
|
|
|
logging.message("settings still default?", settings.isStillDefault);
|
|
|
|
}
|
|
|
|
};
|
2018-07-17 13:07:50 +02:00
|
|
|
scope.startupReset = function(){
|
|
|
|
scope.forEach(function(definition){
|
|
|
|
if (definition.resetOnStartup){
|
|
|
|
definition.set(definition.defaultValue);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2017-11-07 00:36:44 +01:00
|
|
|
Object.seal(scope);
|
2017-09-24 00:12:12 +02:00
|
|
|
}());
|