From f7b5a1ae5d2b3c4727cd1e3c71b747daaa40250a Mon Sep 17 00:00:00 2001
From: varjolintu
Date: Wed, 16 Oct 2019 15:09:05 +0300
Subject: [PATCH] Import/Export settings
---
keepassxc-browser/_locales/en/messages.json | 22 +++++++-
keepassxc-browser/options/options.html | 26 ++++++++-
keepassxc-browser/options/options.js | 58 +++++++++++++++++++++
3 files changed, 103 insertions(+), 3 deletions(-)
diff --git a/keepassxc-browser/_locales/en/messages.json b/keepassxc-browser/_locales/en/messages.json
index 74f7c08..7abe5c3 100644
--- a/keepassxc-browser/_locales/en/messages.json
+++ b/keepassxc-browser/_locales/en/messages.json
@@ -531,10 +531,18 @@
"message": "Cancel",
"description": "Cancel button text when removing database key or custom login fields."
},
- "optionsButtonReset": {
+ "optionsButtonReset": {
"message": "Reset",
"description": "Reset button text for keyboard shortcuts."
},
+ "optionsButtonImport": {
+ "message": "Import settings",
+ "description": "Import settings button text."
+ },
+ "optionsButtonExport": {
+ "message": "Export settings",
+ "description": "Export settings button text."
+ },
"optionsLabelDefaultGroup": {
"message": "Default group for saving new passwords:",
"description": "Default group options text."
@@ -715,6 +723,18 @@
"message": "Error: Shortcut for $1 has not been changed!",
"description": "Error message for changing keyboard shortcuts."
},
+ "optionsImportExportSettings": {
+ "message": "Import/Export settings",
+ "description": "Settings page header text for import/export."
+ },
+ "optionsImportSettingsDialogTitle": {
+ "message": "Import settings from external file?",
+ "description": "Import settings confirmation dialog title text."
+ },
+ "optionsImportSettingsDialogText": {
+ "message": "Current settings will be overridden. Do you really want to import the settings file?",
+ "description": "Import settings confirmation dialog help text."
+ },
"optionsConnectedDatabasesText": {
"message": "The following KeePassXC databases are connected to KeePassXC-Browser.",
"description": "Info text about connected databases."
diff --git a/keepassxc-browser/options/options.html b/keepassxc-browser/options/options.html
index 543e184..0f51046 100644
--- a/keepassxc-browser/options/options.html
+++ b/keepassxc-browser/options/options.html
@@ -39,6 +39,12 @@
+
+
+
+
+
+
@@ -188,9 +194,25 @@
-
-
+
+
diff --git a/keepassxc-browser/options/options.js b/keepassxc-browser/options/options.js
index 098b312..94b1d95 100644
--- a/keepassxc-browser/options/options.js
+++ b/keepassxc-browser/options/options.js
@@ -162,6 +162,64 @@ options.initGeneralSettings = function() {
options.settings['defaultGroup'] = '';
options.saveSettings();
});
+
+ let temporarySettings;
+ $('#dialogImportSettings').modal({ keyboard: true, show: false, backdrop: true });
+ $('#importSettingsButton').click(function() {
+ var link = document.createElement('input');
+ link.setAttribute('type', 'file');
+ link.onchange = function(e) {
+ const reader = new FileReader();
+
+ if (e.target.files.length > 0) {
+ reader.readAsText(e.target.files[0]);
+ }
+
+ reader.onloadend = function(e) {
+ try {
+ const contents = JSON.parse(e.target.result);
+
+ // A quick check that this is the KeePassXC-Browser settings file
+ if (!contents['checkUpdateKeePassXC'] ||
+ !contents['autoCompleteUsernames'] ||
+ !contents['autoFillAndSend']) {
+ console.log('Error: Not a KeePassXC-Browser settings file.');
+ return;
+ }
+
+ // Verify the import
+ temporarySettings = contents;
+ $('#dialogImportSettings').data('hash', $(this).closest('tr').data('hash'));
+ $('#dialogImportSettings .modal-body:first span:first').text($(this).closest('tr').children('td:first').text());
+ $('#dialogImportSettings').modal('show');
+ $('#dialogImportSettings').on('shown.bs.modal', () => {
+ $('#dialogImportSettings').find('[autofocus]').focus();
+ });
+ } catch (e) {
+ console.log('Error loading JSON settings file.');
+ }
+ };
+ };
+
+ link.click();
+ });
+
+ $('#exportSettingsButton').click(function() {
+ const link = document.createElement('a');
+ const file = new Blob([ JSON.stringify(options.settings)], { type: 'application/json' });
+ link.href = URL.createObjectURL(file);
+ link.download = 'keepassxc-browser_settings.json';
+ link.click();
+ });
+
+ $('#dialogImportSettings .modal-footer:first button.yes:first').click(function(e) {
+ $('#dialogImportSettings').modal('hide');
+
+ if (temporarySettings) {
+ options.settings = temporarySettings;
+ options.saveSettings();
+ }
+ });
};
options.showKeePassXCVersions = function(response) {