2017-07-27 19:14:04 +02:00
|
|
|
/* 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/. */
|
2017-07-08 22:43:20 +02:00
|
|
|
browser.storage.local.get().then(function(data){
|
|
|
|
Object.keys(data).forEach(function(key){
|
|
|
|
settings[key] = data[key];
|
|
|
|
});
|
2017-07-27 19:14:04 +02:00
|
|
|
settings.isStillDefault = false;
|
|
|
|
logging.clearQueue();
|
2017-07-08 22:43:20 +02:00
|
|
|
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-07-27 19:14:04 +02:00
|
|
|
message("transate all messages");
|
2017-06-25 22:11:28 +02:00
|
|
|
traverse(document.body, function(node){
|
|
|
|
if (node.nodeType == 3){
|
2017-07-08 22:43:20 +02:00
|
|
|
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;
|
|
|
|
}
|
2017-07-08 22:43:20 +02:00
|
|
|
}).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-07-27 19:14:04 +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;
|
2017-07-02 12:18:20 +02:00
|
|
|
if (input.type === "checkbox"){
|
2017-07-08 22:43:20 +02:00
|
|
|
input.checked = settings[storageName];
|
2017-07-02 12:18:20 +02:00
|
|
|
|
|
|
|
input.addEventListener("click", function(){
|
2017-07-27 19:14:04 +02:00
|
|
|
message("changed setting", storageName, ":", this.checked);
|
2017-07-02 12:18:20 +02:00
|
|
|
var value = this.checked;
|
|
|
|
var obj = {};
|
|
|
|
obj[storageName] = value;
|
|
|
|
browser.storage.local.set(obj);
|
|
|
|
});}
|
|
|
|
else {
|
2017-07-08 22:43:20 +02:00
|
|
|
input.value = settings[storageName];
|
2017-07-02 12:18:20 +02:00
|
|
|
|
|
|
|
input.addEventListener("change", function(){
|
|
|
|
var value = this.value;
|
2017-07-27 19:14:04 +02:00
|
|
|
if (this.type === "number" || this.dataset.type === "number"){
|
2017-07-18 16:11:47 +02:00
|
|
|
value = parseFloat(value);
|
|
|
|
}
|
2017-07-27 19:14:04 +02:00
|
|
|
message("changed setting", storageName, ":", value);
|
2017-07-02 12:18:20 +02:00
|
|
|
var obj = {};
|
|
|
|
obj[storageName] = value;
|
|
|
|
browser.storage.local.set(obj);
|
|
|
|
});
|
|
|
|
}
|
2017-06-25 22:11:28 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
var callbacks = {
|
|
|
|
showReleaseNotes: function(){
|
2017-07-27 19:14:04 +02:00
|
|
|
verbose("open release notes");
|
2017-06-25 22:11:28 +02:00
|
|
|
window.open("../releaseNotes.txt", "_blank");
|
2017-07-16 00:26:40 +02:00
|
|
|
// 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-07-27 19:14:04 +02:00
|
|
|
message("clear persistent rnd storage");
|
|
|
|
notice("empty storage");
|
2017-06-25 22:11:28 +02:00
|
|
|
browser.storage.local.set({persistentRndStorage: ""});
|
2017-07-27 19:14:04 +02:00
|
|
|
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]();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-07-08 22:43:20 +02:00
|
|
|
function updateDisplay(){
|
2017-07-27 19:14:04 +02:00
|
|
|
notice("update display");
|
2017-07-08 22:43:20 +02:00
|
|
|
document.querySelectorAll("tr.settingRow").forEach(function(row){
|
2017-07-27 19:14:04 +02:00
|
|
|
verbose("evaluate display dependencies for", row.setting);
|
2017-07-08 22:43:20 +02:00
|
|
|
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();
|
|
|
|
|
2017-07-02 12:18:20 +02:00
|
|
|
browser.storage.onChanged.addListener(function(change, area){
|
|
|
|
if (area === "local"){
|
|
|
|
Object.keys(change).forEach(function(key){
|
2017-07-08 22:43:20 +02:00
|
|
|
settings[key] = change[key].newValue;
|
2017-07-02 12:18:20 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2017-07-08 22:43:20 +02:00
|
|
|
updateDisplay();
|
2017-07-02 12:18:20 +02:00
|
|
|
}
|
2017-06-25 22:11:28 +02:00
|
|
|
});
|
2017-07-08 22:43:20 +02:00
|
|
|
});
|