From 9d7c801c2b6114524bb565490982c88297f8c69b Mon Sep 17 00:00:00 2001 From: kkapsner Date: Sun, 2 Sep 2018 13:34:20 +0200 Subject: [PATCH] Added save/load directly to/from file option Fixes #240. --- _locales/de/messages.json | 11 +++++- _locales/en/messages.json | 13 +++++-- options/options.js | 71 ++++++++++++++++++++++++++++++++++++-- options/optionsGui.js | 7 ++++ options/settingsDisplay.js | 4 +-- releaseNotes.txt | 2 +- 6 files changed, 99 insertions(+), 9 deletions(-) diff --git a/_locales/de/messages.json b/_locales/de/messages.json index 990284d..9fd93b5 100644 --- a/_locales/de/messages.json +++ b/_locales/de/messages.json @@ -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", diff --git a/_locales/en/messages.json b/_locales/en/messages.json index 0650127..383f776 100644 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -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": "" }, diff --git a/options/options.js b/options/options.js index 0247fc0..d253667 100644 --- a/options/options.js +++ b/options/options.js @@ -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, diff --git a/options/optionsGui.js b/options/optionsGui.js index 462ab36..89c416c 100644 --- a/options/optionsGui.js +++ b/options/optionsGui.js @@ -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){ diff --git a/options/settingsDisplay.js b/options/settingsDisplay.js index b89d5b9..4be9493 100644 --- a/options/settingsDisplay.js +++ b/options/settingsDisplay.js @@ -426,9 +426,7 @@ "settings", { "name": "exportSettings", - "displayDependencies": { - "displayAdvancedSettings": [true] - } + "actions": ["inspectSettings", "saveSettings", "loadSettings"] }, { "name": "resetSettings", diff --git a/releaseNotes.txt b/releaseNotes.txt index eabb51c..fbb1923 100644 --- a/releaseNotes.txt +++ b/releaseNotes.txt @@ -3,7 +3,7 @@ Version 0.5.4: - new features: - - + - added save/load directly to/from file option fixes: -