1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2024-11-04 20:28:52 +01:00
CanvasBlocker/options/options.js

129 lines
3.9 KiB
JavaScript
Raw Normal View History

/* jslint moz: true */
/* 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/. */
browser.storage.local.get().then(function(data){
Object.keys(data).forEach(function(key){
settings[key] = data[key];
});
settings.isStillDefault = false;
logging.clearQueue();
return settings;
}).then(function(settings){
2017-06-25 22:11:28 +02:00
function traverse(node, func){
func(node);
Array.from(node.childNodes).forEach(function(child){traverse(child, func);});
}
// getting the translation of all the messages
message("transate all messages");
2017-06-25 22:11:28 +02:00
traverse(document.body, function(node){
if (node.nodeType == 3){
var lines = node.nodeValue.replace(/\b__MSG_(.+)__\b/g, function(m, key){
2017-06-25 22:11:28 +02:00
try {
return browser.i18n.getMessage(key);
}
catch (e){
return "Unknown i18n key: " + key;
}
}).split(/\n/g);
node.nodeValue = lines.shift();
lines.forEach(function(line){
node.parentNode.appendChild(document.createElement("br"));
node.parentNode.appendChild(document.createTextNode(line));
2017-06-25 22:11:28 +02:00
});
}
});
message("register events to store changes in local storage");
2017-06-25 22:11:28 +02:00
Array.from(document.querySelectorAll("input.setting, select.setting")).forEach(function(input){
var storageName = input.dataset.storageName;
if (input.type === "checkbox"){
input.checked = settings[storageName];
input.addEventListener("click", function(){
message("changed setting", storageName, ":", this.checked);
var value = this.checked;
var obj = {};
obj[storageName] = value;
browser.storage.local.set(obj);
});}
else {
input.value = settings[storageName];
input.addEventListener("change", function(){
var value = this.value;
if (this.type === "number" || this.dataset.type === "number"){
value = parseFloat(value);
}
message("changed setting", storageName, ":", value);
var obj = {};
obj[storageName] = value;
browser.storage.local.set(obj);
});
}
2017-06-25 22:11:28 +02:00
});
var callbacks = {
showReleaseNotes: function(){
verbose("open release notes");
2017-06-25 22:11:28 +02:00
window.open("../releaseNotes.txt", "_blank");
// would be nicer but is not supported in fennec
// browser.windows.create({
// url: "../releaseNotes.txt",
// type: "popup"
// });
2017-06-25 22:11:28 +02:00
},
clearPersistentRnd: function(){
message("clear persistent rnd storage");
notice("empty storage");
2017-06-25 22:11:28 +02:00
browser.storage.local.set({persistentRndStorage: ""});
notice("send message to main script");
2017-07-07 08:50:23 +02:00
browser.runtime.sendMessage({"canvasBlocker-clear-domain-rnd": true});
2017-06-25 22:11:28 +02:00
}
};
Array.from(document.querySelectorAll("button.setting")).forEach(function(button){
var storageName = button.dataset.storageName;
button.addEventListener("click", function(){
if (callbacks[storageName]){
callbacks[storageName]();
}
});
});
function updateDisplay(){
notice("update display");
document.querySelectorAll("tr.settingRow").forEach(function(row){
verbose("evaluate display dependencies for", row.setting);
var displayDependencies = row.setting.displayDependencies;
if (displayDependencies){
row.classList[(
(Array.isArray(displayDependencies)? displayDependencies: [displayDependencies]).some(function(displayDependency){
return Object.keys(displayDependency).every(function(key){
return displayDependency[key].indexOf(settings[key]) !== -1;
});
})
)? "remove": "add"]("hidden");
}
});
}
updateDisplay();
browser.storage.onChanged.addListener(function(change, area){
if (area === "local"){
Object.keys(change).forEach(function(key){
settings[key] = change[key].newValue;
var input = document.querySelector(".setting[data-storage-name=" + key + "]");
if (input){
if (input.type === "checkbox"){
input.checked = change[key].newValue;
}
else {
input.value = change[key].newValue;
}
}
});
updateDisplay();
}
2017-06-25 22:11:28 +02:00
});
});