1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2024-12-22 21:00:23 +01:00

Added save/load directly to/from file option

Fixes #240.
This commit is contained in:
kkapsner 2018-09-02 13:34:20 +02:00
parent f0f13f17a3
commit 9d7c801c2b
6 changed files with 99 additions and 9 deletions

View File

@ -954,10 +954,19 @@
"message": "",
"description": ""
},
"exportSettings_label": {
"inspectSettings_label": {
"message": "Anzeigen",
"description": ""
},
"saveSettings_label": {
"message": "Speichern",
"description": ""
},
"loadSettings_label": {
"message": "Laden",
"description": ""
},
"resetSettings_title": {
"message": "Einstellungen zurücksetzen",

View File

@ -953,8 +953,17 @@
"message": "",
"description": ""
},
"exportSettings_label": {
"message": "Show",
"inspectSettings_label": {
"message": "Inspect",
"description": ""
},
"saveSettings_label": {
"message": "Save",
"description": ""
},
"loadSettings_label": {
"message": "Load",
"description": ""
},

View File

@ -23,10 +23,64 @@
logging.notice("send message to main script");
browser.runtime.sendMessage({"canvasBlocker-clear-domain-rnd": true});
},
exportSettings: function(){
logging.verbose("open settings export");
inspectSettings: function(){
logging.verbose("open settings inspection");
window.open("export.html", "_blank");
},
saveSettings: function(){
logging.verbose("save settings");
const data = {};
settings.forEach(function(def){
data[def.name] = def.get();
});
const blob = new Blob([JSON.stringify(data, null, "\t")], {type: "application/json"});
const link = document.createElement("a");
link.href = window.URL.createObjectURL(blob);
link.target = "_blank";
link.download = "CanvasBlocker-settings.json";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
},
loadSettings: function(){
logging.verbose("load settings");
new Promise(function(resolve, reject){
const input = document.createElement("input");
input.type = "file";
input.addEventListener("change", function(){
if (this.files.length){
var file = this.files[0];
var reader = new FileReader();
reader.onload = function(result){
resolve(this.result);
};
reader.onerror = function(err){
reject(err);
};
reader.readAsText(this.files[0]);
}
});
input.click();
}).then(function(text){
return JSON.parse(text);
}).then(function(json){
const keys = Object.keys(json);
keys.forEach(function(key){
const setting = settings.getDefinition(key);
if (!settings){
throw new Error("Unknown setting " + key + ".");
}
if (!setting.fixed && setting.invalid(json[key])){
throw new Error("Invalid value " + json[key] + " for " + key + ".");
}
});
keys.forEach(function(key){
settings[key] = json[key];
});
}).catch(function(err){
alert(err);
});
},
resetSettings: function(){
if (window.confirm(browser.i18n.getMessage("resetSettings_confirm"))){
browser.storage.local.clear();
@ -172,6 +226,19 @@
inputs: display.inputs.map(settings.getDefinition)
};
}
else if (display.actions){
setting = {
name: display.name,
actions: display.actions.map(function(action){
return {
name: action,
action: callbacks[action]
};
}).filter(function(action){
return action.action;
})
};
}
else if (callbacks[display.name]){
setting = {
name: display.name,

View File

@ -284,6 +284,13 @@
if (setting.action){
interaction = createButton(setting);
}
else if (setting.actions){
interaction = document.createElement("span");
setting.actions.forEach(function(action){
var button = createButton(action);
interaction.appendChild(button);
});
}
else if (setting.inputs){
interaction = document.createElement("span");
setting.inputs.forEach(function(inputSetting){

View File

@ -426,9 +426,7 @@
"settings",
{
"name": "exportSettings",
"displayDependencies": {
"displayAdvancedSettings": [true]
}
"actions": ["inspectSettings", "saveSettings", "loadSettings"]
},
{
"name": "resetSettings",

View File

@ -3,7 +3,7 @@ Version 0.5.4:
-
new features:
-
- added save/load directly to/from file option
fixes:
-