1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2024-12-23 05:10:27 +01:00
CanvasBlocker/lib/settings.js

471 lines
12 KiB
JavaScript
Raw Normal View History

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
}
const logging = require("./logging");
2017-11-07 00:36:44 +01:00
const settingDefinitions = require("./settingDefinitions.js");
2018-09-16 01:22:40 +02:00
const settingContainers = require("./settingContainers");
const definitionsByName = {};
const defaultSymbol = "";
2017-11-07 00:36:44 +01:00
const eventHandler = {any: {}};
eventHandler.any[defaultSymbol] = [];
2017-11-07 00:36:44 +01:00
eventHandler.all = eventHandler.any;
const settings = {};
2017-09-24 00:12:12 +02: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
}
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
}
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){
2018-09-16 01:22:40 +02:00
var match = settingContainers.getUrlValueContainer(settingDefinition.name, url);
if (match){
return match[settingDefinition.name];
}
2017-11-14 01:07:27 +01:00
}
return settings[settingDefinition.name];
};
}
else {
return function getValue(){
return settings[settingDefinition.name];
};
}
}
function getDefaultValue(settingDefinition){
let defaultValue = settingDefinition.defaultValue;
if ((typeof defaultValue) === "object"){
if (Array.isArray(defaultValue)){
return defaultValue.slice();
}
else {
return Object.create(defaultValue);
}
}
return defaultValue;
}
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
}
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);
settings[name] = newValue;
if (!settingDefinition.transient){
var storeObject = {};
storeObject[name] = newValue;
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);
});
return promise;
2018-07-12 01:18:49 +02:00
}
else {
logging.warning("Transient setting %s cannot be stored.", name);
}
};
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);
if (isValid(newValue)){
if (url){
2018-09-16 01:22:40 +02:00
return settingContainers.setUrlValue(name, newValue, url);
}
else {
return storeValue(newValue);
}
}
2018-07-12 01:18:49 +02:00
else{
logging.warning("Invalid value for %s (%s):", name, url, newValue);
}
};
}
else {
return function setValue(newValue){
logging.verbose("New value for %s:", name, newValue);
if (isValid(newValue)){
return storeValue(newValue);
}
2018-07-12 01:18:49 +02:00
else{
logging.warning("Invalid value for %s:", name, newValue);
}
};
}
}
}
function createResetter(settingDefinition){
if (settingDefinition.dynamic){
return function(){};
}
else {
const name = settingDefinition.name;
let reset = function(){
settings[name] = getDefaultValue(settingDefinition);
browser.storage.local.remove(name);
};
if (settingDefinition.urlSpecific){
return function(url){
if (url){
2018-09-16 01:22:40 +02:00
settingContainers.resetUrlValue(name, url);
}
else {
reset();
}
};
}
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);
}
}
};
settingDefinitions.forEach(function(settingDefinition){
var name = settingDefinition.name;
definitionsByName[name] = settingDefinition;
if (typeof settingDefinition.defaultValue === "function"){
settingDefinition.defaultValue = settingDefinition.defaultValue();
}
settings[name] = getDefaultValue(settingDefinition);
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
};
settingDefinition.get = createGetter(settingDefinition);
settingDefinition.set = createSetter(settingDefinition);
settingDefinition.reset = createResetter(settingDefinition);
2018-07-02 00:29:41 +02:00
if (settingDefinition.urlSpecific){
2018-09-16 01:22:40 +02:00
if (!settingContainers.urlContainer){
logging.error("Unable to use url specific settings without url-container");
}
else {
2018-09-16 01:22:40 +02:00
settingDefinition.urlContainer = settingContainers.urlContainer;
let entry = Object.create(settingDefinition);
entry.optional = true;
2018-09-16 01:22:40 +02:00
settingContainers.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
2018-09-16 01:22:40 +02:00
settingContainers.check(settingDefinition);
2017-09-24 00:12:12 +02:00
});
2017-11-07 00:36:44 +01:00
scope.getDefinition = function(name){
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 {
2018-09-16 01:22:40 +02:00
url: Object.create(settingContainers.urlContainer),
hide: Object.create(settingContainers.hideContainer),
expand: Object.create(settingContainers.expandContainer)
2018-07-02 00:29:41 +02:00
};
};
2017-11-07 00:36:44 +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);
}
};
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-09-24 00:12:12 +02:00
scope.forEach = function forEachSetting(...args){
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
};
const resetSymbol = Symbol("reset");
2017-11-07 00:36:44 +01:00
function changeValue(name, newValue){
var settingDefinition = scope.getDefinition(name);
if (settingDefinition){
var oldValue = settings[name];
if (newValue === resetSymbol){
newValue = getDefaultValue(settingDefinition);
}
settings[name] = newValue;
((eventHandler[name] || {})[defaultSymbol] || []).forEach(function(callback){
callback({name, newValue, oldValue});
});
if (settingDefinition.urlSpecific){
2018-09-16 01:22:40 +02:00
settingContainers.urlContainer.get().forEach(function(entry){
if (!entry.hasOwnProperty(name)){
((eventHandler[name] || {})[entry.url] || []).forEach(function(callback){
callback({name, newValue, oldValue, url: entry.url});
});
}
});
}
}
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);
var delayedChange = [];
2017-11-07 00:36:44 +01:00
Object.entries(changes).forEach(function(entry){
const [name, change] = entry;
2018-09-16 01:22:40 +02:00
if (settingContainers.urlContainer && name === settingContainers.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
});
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();
});
}
});
settingContainers.initializeUrlContainer(eventHandler);
2017-11-07 00:36:44 +01:00
logging.verbose("loading settings");
let initialized = false;
scope.isInitialized = function(){
return initialized;
};
const initEvents = [];
scope.init = function(storage){
if (initialized){
return false;
}
initialized = true;
2017-11-07 00:36:44 +01:00
logging.message("settings loaded");
2018-09-14 16:29:30 +02:00
if (require.exists("./settingsMigration")){
const settingsMigration = require("./settingsMigration");
2018-09-16 01:22:40 +02:00
settingsMigration.check(
storage,
{settings, logging, changeValue, urlContainer: settingContainers.urlContainer}
);
}
var delayedChange = [];
2017-11-07 00:36:44 +01:00
Object.entries(storage).forEach(function(entry){
const [name, value] = entry;
2018-09-16 01:22:40 +02:00
if (settingContainers.urlContainer && name === settingContainers.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);
initEvents.forEach(function(callback){callback();});
return true;
};
if (window.scope.settingsData){
scope.init(window.scope.settingsData);
scope.loaded = Promise.resolve(false);
}
else {
scope.loaded = browser.storage.local.get().then(scope.init);
}
2017-11-07 00:36:44 +01:00
scope.onloaded = function(callback){
if (scope.isStillDefault){
initEvents.push(callback);
2017-11-07 00:36:44 +01:00
}
else {
callback();
}
};
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(getDefaultValue(definition));
2018-07-17 13:07:50 +02:00
}
});
};
2017-11-07 00:36:44 +01:00
Object.seal(scope);
require.emit("./settings");
2017-09-24 00:12:12 +02:00
}());