1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2024-06-01 10:58:05 +02:00
CanvasBlocker/options/options.js

130 lines
4.0 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;
2017-09-29 00:31:00 +02:00
logging.setPrefix("options page");
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
2017-08-07 08:50:09 +02:00
logging.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
});
}
});
2017-08-07 08:50:09 +02:00
logging.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(){
2017-08-07 08:50:09 +02:00
logging.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);
}
2017-08-07 08:50:09 +02:00
logging.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(){
2017-08-07 08:50:09 +02:00
logging.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(){
2017-08-07 08:50:09 +02:00
logging.message("clear persistent rnd storage");
logging.notice("empty storage");
2017-06-25 22:11:28 +02:00
browser.storage.local.set({persistentRndStorage: ""});
2017-08-07 08:50:09 +02:00
logging.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(){
2017-08-07 08:50:09 +02:00
logging.notice("update display");
document.querySelectorAll("tr.settingRow").forEach(function(row){
2017-08-07 08:50:09 +02:00
logging.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
});
});