diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2019-12-14 14:15:17 -0500 |
---|---|---|
committer | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2019-12-29 12:33:28 -0500 |
commit | 5045a9a3a063bafae94bc855627cd85f61671d62 (patch) | |
tree | 673a18be80de08e7dfd1fc50ff70b3b902df9eb0 /ext/bg/js/settings/backup.js | |
parent | d2da4f7e62c42ed7f9fd82c8af2f1d9968bec2ce (diff) |
Implement settings export
Diffstat (limited to 'ext/bg/js/settings/backup.js')
-rw-r--r-- | ext/bg/js/settings/backup.js | 122 |
1 files changed, 122 insertions, 0 deletions
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 <alex@foosoft.net> + * Author: Alex Yatskov <alex@foosoft.net> + * + * 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 <http://www.gnu.org/licenses/>. + */ + + +// 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); |