diff --git a/ext/bg/js/settings/backup.js b/ext/bg/js/settings/backup.js new file mode 100644 index 00000000..6494cd65 --- /dev/null +++ b/ext/bg/js/settings/backup.js @@ -0,0 +1,122 @@ +/* + * Copyright (C) 2019 Alex Yatskov + * Author: Alex Yatskov + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +// Exporting + +let _settingsExportToken = null; +let _settingsExportRevoke = null; +const SETTINGS_EXPORT_CURRENT_VERSION = 0; + +function _getSettingsExportDateString(date, dateSeparator, dateTimeSeparator, timeSeparator, resolution) { + const values = [ + date.getUTCFullYear().toString(), + dateSeparator, + (date.getUTCMonth() + 1).toString().padStart(2, '0'), + dateSeparator, + date.getUTCDate().toString().padStart(2, '0'), + dateTimeSeparator, + date.getUTCHours().toString().padStart(2, '0'), + timeSeparator, + date.getUTCMinutes().toString().padStart(2, '0'), + timeSeparator, + date.getUTCSeconds().toString().padStart(2, '0') + ]; + return values.slice(0, resolution * 2 - 1).join(''); +} + +async function _getSettingsExportData(date) { + const optionsFull = await apiOptionsGetFull(); + const environment = await apiGetEnvironmentInfo(); + + const fieldTemplatesDefault = profileOptionsGetDefaultFieldTemplates(); + + // Format options + for (const {options} of optionsFull.profiles) { + if (options.anki.fieldTemplates === fieldTemplatesDefault || !options.anki.fieldTemplates) { + delete options.anki.fieldTemplates; // Default + } + } + + const data = { + version: SETTINGS_EXPORT_CURRENT_VERSION, + date: _getSettingsExportDateString(date, '-', ' ', ':', 6), + url: chrome.runtime.getURL('/'), + manifest: chrome.runtime.getManifest(), + environment, + userAgent: navigator.userAgent, + options: optionsFull + }; + + return data; +} + +function _saveBlob(blob, fileName) { + if (typeof navigator === 'object' && typeof navigator.msSaveBlob === 'function') { + if (navigator.msSaveBlob(blob)) { + return; + } + } + + const blobUrl = URL.createObjectURL(blob); + + const a = document.createElement('a'); + a.href = blobUrl; + a.download = fileName; + a.rel = 'noopener'; + a.target = '_blank'; + + const revoke = () => { + URL.revokeObjectURL(blobUrl); + a.href = ''; + _settingsExportRevoke = null; + }; + _settingsExportRevoke = revoke; + + a.dispatchEvent(new MouseEvent('click')); + setTimeout(revoke, 60000); +} + +async function _onSettingsExportClick() { + if (_settingsExportRevoke !== null) { + _settingsExportRevoke(); + _settingsExportRevoke = null; + } + + const date = new Date(Date.now()); + + const token = {}; + _settingsExportToken = token; + const data = await _getSettingsExportData(date); + if (_settingsExportToken !== token) { + // A new export has been started + return; + } + _settingsExportToken = null; + + const fileName = `yomichan-settings-${_getSettingsExportDateString(date, '-', '-', '-', 6)}.json`; + const blob = new Blob([JSON.stringify(data, null, 4)], {type: 'application/json'}); + _saveBlob(blob, fileName); +} + + +// Setup + +window.addEventListener('DOMContentLoaded', () => { + document.querySelector('#settings-export').addEventListener('click', _onSettingsExportClick, false); +}, false); diff --git a/ext/bg/settings.html b/ext/bg/settings.html index e0036f57..ec0e2939 100644 --- a/ext/bg/settings.html +++ b/ext/bg/settings.html @@ -853,6 +853,22 @@ + +
+

Backup

+ +

+ Yomichan can import and export settings files which can be used to restore settings, + share settings across devices, or help to debug problems. + These files will only contain settings and will not contain dictionaries. + Dictionaries must be imported separately. +

+ +
+ +
+
+

Support Development

@@ -901,6 +917,7 @@ +