From 84eed2fa1335f3660271a247a48e8b880a5e7e39 Mon Sep 17 00:00:00 2001 From: Tamius Han Date: Mon, 15 Jul 2019 22:54:41 +0200 Subject: [PATCH] Import/export settings --- src/manifest.json | 3 ++ src/options/GeneralSettings.vue | 76 ++++++++++++++++++++++++++++++--- 2 files changed, 74 insertions(+), 5 deletions(-) diff --git a/src/manifest.json b/src/manifest.json index 9106a71..7eb5243 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -55,5 +55,8 @@ ], "permissions": [ "tabs", "storage", "activeTab", "", "webNavigation" + ], + "optional_permissions": [ + "downloads" ] } diff --git a/src/options/GeneralSettings.vue b/src/options/GeneralSettings.vue index 3f5b465..a22ff5d 100644 --- a/src/options/GeneralSettings.vue +++ b/src/options/GeneralSettings.vue @@ -116,13 +116,31 @@
- Reset settings + Import, export, reset settings
-
- + Settings import failed. The settings file is probably corrupted. +
+
+
+ + +
+
@@ -132,6 +150,7 @@ import Button from '../common/components/Button'; import Stretch from '../common/enums/stretch.enum'; import ExtensionMode from '../common/enums/extension-mode.enum'; import VideoAlignment from '../common/enums/video-alignment.enum'; +import BrowserDetect from '../ext/conf/BrowserDetect'; export default { components: { @@ -146,6 +165,7 @@ export default { ExtensionMode: ExtensionMode, VideoAlignment: VideoAlignment, stretchThreshold: 0, + corruptedSettingsWarning: false, } }, created () { @@ -178,8 +198,54 @@ export default { resetSettings() { this.settings.active = JSON.parse(JSON.stringify(this.settings.default)); this.settings.save(); + }, + exportSettings() { + browser.permissions.request({permissions: ['downloads']}); + const blob = new Blob([JSON.stringify(this.settings.active)], {type: 'application/json'}); + const fileUrl = URL.createObjectURL(blob); + if (BrowserDetect.firefox) { + browser.downloads.download({saveAs: true, filename: 'ultrawidify-settings.json', url: fileUrl}); + } + }, + async importSettings($event) { + let file, text, settingsObj; + this.corruptedSettingsWarning = false; + + try { + file = $event.target.files[0]; + } catch (e) { + console.error("error grabbing a file!"); + this.corruptedSettingsWarning = true; + return; + } + + try { + text = await file.text(); + settingsObj = JSON.parse(text); + } catch (e) { + console.error("error parsing file to json"); + this.corruptedSettingsWarning = true; + return; + } + + // validate settings + for (const key in this.settings.default) { + if (!settingsObj[key]) { + console.error("corrupted settings!") + this.corruptedSettingsWarning = true; + return; + } + } + + this.settings.active = settingsObj; + this.settings.save(); } } } +