Settings backup refactor (#551)

* Update backup.js to be a class

* Move utilReadFileArrayBuffer
This commit is contained in:
toasted-nutbread 2020-05-24 13:56:46 -04:00 committed by GitHub
parent 66e1185686
commit 13f57cccba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 317 additions and 315 deletions

View File

@ -22,16 +22,26 @@
* utilBackend * utilBackend
* utilBackgroundIsolate * utilBackgroundIsolate
* utilIsolate * utilIsolate
* utilReadFileArrayBuffer
*/ */
// Exporting class SettingsBackup {
constructor() {
this._settingsExportToken = null;
this._settingsExportRevoke = null;
this._currentVersion = 0;
}
let _settingsExportToken = null; prepare() {
let _settingsExportRevoke = null; document.querySelector('#settings-export').addEventListener('click', this._onSettingsExportClick.bind(this), false);
const SETTINGS_EXPORT_CURRENT_VERSION = 0; document.querySelector('#settings-import').addEventListener('click', this._onSettingsImportClick.bind(this), false);
document.querySelector('#settings-import-file').addEventListener('change', this._onSettingsImportFileChange.bind(this), false);
document.querySelector('#settings-reset').addEventListener('click', this._onSettingsResetClick.bind(this), false);
document.querySelector('#settings-reset-modal-confirm').addEventListener('click', this._onSettingsResetConfirmClick.bind(this), false);
}
function _getSettingsExportDateString(date, dateSeparator, dateTimeSeparator, timeSeparator, resolution) { // Private
_getSettingsExportDateString(date, dateSeparator, dateTimeSeparator, timeSeparator, resolution) {
const values = [ const values = [
date.getUTCFullYear().toString(), date.getUTCFullYear().toString(),
dateSeparator, dateSeparator,
@ -46,9 +56,9 @@ function _getSettingsExportDateString(date, dateSeparator, dateTimeSeparator, ti
date.getUTCSeconds().toString().padStart(2, '0') date.getUTCSeconds().toString().padStart(2, '0')
]; ];
return values.slice(0, resolution * 2 - 1).join(''); return values.slice(0, resolution * 2 - 1).join('');
} }
async function _getSettingsExportData(date) { async _getSettingsExportData(date) {
const optionsFull = await api.optionsGetFull(); const optionsFull = await api.optionsGetFull();
const environment = await api.getEnvironmentInfo(); const environment = await api.getEnvironmentInfo();
const fieldTemplatesDefault = await api.getDefaultAnkiFieldTemplates(); const fieldTemplatesDefault = await api.getDefaultAnkiFieldTemplates();
@ -61,8 +71,8 @@ async function _getSettingsExportData(date) {
} }
const data = { const data = {
version: SETTINGS_EXPORT_CURRENT_VERSION, version: this._currentVersion,
date: _getSettingsExportDateString(date, '-', ' ', ':', 6), date: this._getSettingsExportDateString(date, '-', ' ', ':', 6),
url: chrome.runtime.getURL('/'), url: chrome.runtime.getURL('/'),
manifest: chrome.runtime.getManifest(), manifest: chrome.runtime.getManifest(),
environment, environment,
@ -71,9 +81,9 @@ async function _getSettingsExportData(date) {
}; };
return data; return data;
} }
function _saveBlob(blob, fileName) { _saveBlob(blob, fileName) {
if (typeof navigator === 'object' && typeof navigator.msSaveBlob === 'function') { if (typeof navigator === 'object' && typeof navigator.msSaveBlob === 'function') {
if (navigator.msSaveBlob(blob)) { if (navigator.msSaveBlob(blob)) {
return; return;
@ -91,52 +101,60 @@ function _saveBlob(blob, fileName) {
const revoke = () => { const revoke = () => {
URL.revokeObjectURL(blobUrl); URL.revokeObjectURL(blobUrl);
a.href = ''; a.href = '';
_settingsExportRevoke = null; this._settingsExportRevoke = null;
}; };
_settingsExportRevoke = revoke; this._settingsExportRevoke = revoke;
a.dispatchEvent(new MouseEvent('click')); a.dispatchEvent(new MouseEvent('click'));
setTimeout(revoke, 60000); setTimeout(revoke, 60000);
} }
async function _onSettingsExportClick() { async _onSettingsExportClick() {
if (_settingsExportRevoke !== null) { if (this._settingsExportRevoke !== null) {
_settingsExportRevoke(); this._settingsExportRevoke();
_settingsExportRevoke = null; this._settingsExportRevoke = null;
} }
const date = new Date(Date.now()); const date = new Date(Date.now());
const token = {}; const token = {};
_settingsExportToken = token; this._settingsExportToken = token;
const data = await _getSettingsExportData(date); const data = await this._getSettingsExportData(date);
if (_settingsExportToken !== token) { if (this._settingsExportToken !== token) {
// A new export has been started // A new export has been started
return; return;
} }
_settingsExportToken = null; this._settingsExportToken = null;
const fileName = `yomichan-settings-${_getSettingsExportDateString(date, '-', '-', '-', 6)}.json`; const fileName = `yomichan-settings-${this._getSettingsExportDateString(date, '-', '-', '-', 6)}.json`;
const blob = new Blob([JSON.stringify(data, null, 4)], {type: 'application/json'}); const blob = new Blob([JSON.stringify(data, null, 4)], {type: 'application/json'});
_saveBlob(blob, fileName); this._saveBlob(blob, fileName);
} }
_readFileArrayBuffer(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => resolve(reader.result);
reader.onerror = () => reject(reader.error);
reader.readAsArrayBuffer(file);
});
}
// Importing // Importing
async function _settingsImportSetOptionsFull(optionsFull) { async _settingsImportSetOptionsFull(optionsFull) {
return utilIsolate(utilBackend().setFullOptions( return utilIsolate(utilBackend().setFullOptions(
utilBackgroundIsolate(optionsFull) utilBackgroundIsolate(optionsFull)
)); ));
} }
function _showSettingsImportError(error) { _showSettingsImportError(error) {
yomichan.logError(error); yomichan.logError(error);
document.querySelector('#settings-import-error-modal-message').textContent = `${error}`; document.querySelector('#settings-import-error-modal-message').textContent = `${error}`;
$('#settings-import-error-modal').modal('show'); $('#settings-import-error-modal').modal('show');
} }
async function _showSettingsImportWarnings(warnings) { async _showSettingsImportWarnings(warnings) {
const modalNode = $('#settings-import-warning-modal'); const modalNode = $('#settings-import-warning-modal');
const buttons = document.querySelectorAll('.settings-import-warning-modal-import-button'); const buttons = document.querySelectorAll('.settings-import-warning-modal-import-button');
const messageContainer = document.querySelector('#settings-import-warning-modal-message'); const messageContainer = document.querySelector('#settings-import-warning-modal-message');
@ -190,9 +208,9 @@ async function _showSettingsImportWarnings(warnings) {
button.addEventListener('click', onButtonClick, false); button.addEventListener('click', onButtonClick, false);
} }
}); });
} }
function _isLocalhostUrl(urlString) { _isLocalhostUrl(urlString) {
try { try {
const url = new URL(urlString); const url = new URL(urlString);
switch (url.hostname.toLowerCase()) { switch (url.hostname.toLowerCase()) {
@ -210,9 +228,9 @@ function _isLocalhostUrl(urlString) {
// NOP // NOP
} }
return false; return false;
} }
function _settingsImportSanitizeProfileOptions(options, dryRun) { _settingsImportSanitizeProfileOptions(options, dryRun) {
const warnings = []; const warnings = [];
const anki = options.anki; const anki = options.anki;
@ -225,7 +243,7 @@ function _settingsImportSanitizeProfileOptions(options, dryRun) {
} }
} }
const server = anki.server; const server = anki.server;
if (typeof server === 'string' && server.length > 0 && !_isLocalhostUrl(server)) { if (typeof server === 'string' && server.length > 0 && !this._isLocalhostUrl(server)) {
warnings.push('anki.server uses a non-localhost URL'); warnings.push('anki.server uses a non-localhost URL');
if (!dryRun) { if (!dryRun) {
delete anki.server; delete anki.server;
@ -236,7 +254,7 @@ function _settingsImportSanitizeProfileOptions(options, dryRun) {
const audio = options.audio; const audio = options.audio;
if (isObject(audio)) { if (isObject(audio)) {
const customSourceUrl = audio.customSourceUrl; const customSourceUrl = audio.customSourceUrl;
if (typeof customSourceUrl === 'string' && customSourceUrl.length > 0 && !_isLocalhostUrl(customSourceUrl)) { if (typeof customSourceUrl === 'string' && customSourceUrl.length > 0 && !this._isLocalhostUrl(customSourceUrl)) {
warnings.push('audio.customSourceUrl uses a non-localhost URL'); warnings.push('audio.customSourceUrl uses a non-localhost URL');
if (!dryRun) { if (!dryRun) {
delete audio.customSourceUrl; delete audio.customSourceUrl;
@ -245,9 +263,9 @@ function _settingsImportSanitizeProfileOptions(options, dryRun) {
} }
return warnings; return warnings;
} }
function _settingsImportSanitizeOptions(optionsFull, dryRun) { _settingsImportSanitizeOptions(optionsFull, dryRun) {
const warnings = new Set(); const warnings = new Set();
const profiles = optionsFull.profiles; const profiles = optionsFull.profiles;
@ -257,7 +275,7 @@ function _settingsImportSanitizeOptions(optionsFull, dryRun) {
const options = profile.options; const options = profile.options;
if (!isObject(options)) { continue; } if (!isObject(options)) { continue; }
const warnings2 = _settingsImportSanitizeProfileOptions(options, dryRun); const warnings2 = this._settingsImportSanitizeProfileOptions(options, dryRun);
for (const warning of warnings2) { for (const warning of warnings2) {
warnings.add(warning); warnings.add(warning);
} }
@ -265,19 +283,19 @@ function _settingsImportSanitizeOptions(optionsFull, dryRun) {
} }
return warnings; return warnings;
} }
function _utf8Decode(arrayBuffer) { _utf8Decode(arrayBuffer) {
try { try {
return new TextDecoder('utf-8').decode(arrayBuffer); return new TextDecoder('utf-8').decode(arrayBuffer);
} catch (e) { } catch (e) {
const binaryString = String.fromCharCode.apply(null, new Uint8Array(arrayBuffer)); const binaryString = String.fromCharCode.apply(null, new Uint8Array(arrayBuffer));
return decodeURIComponent(escape(binaryString)); return decodeURIComponent(escape(binaryString));
} }
} }
async function _importSettingsFile(file) { async _importSettingsFile(file) {
const dataString = _utf8Decode(await utilReadFileArrayBuffer(file)); const dataString = this._utf8Decode(await this._readFileArrayBuffer(file));
const data = JSON.parse(dataString); const data = JSON.parse(dataString);
// Type check // Type check
@ -297,7 +315,7 @@ async function _importSettingsFile(file) {
if (!( if (!(
version >= 0 && version >= 0 &&
version <= SETTINGS_EXPORT_CURRENT_VERSION version <= this._currentVersion
)) { )) {
throw new Error(`Unsupported version: ${version}`); throw new Error(`Unsupported version: ${version}`);
} }
@ -312,65 +330,58 @@ async function _importSettingsFile(file) {
optionsFull = optionsUpdateVersion(optionsFull, {}); optionsFull = optionsUpdateVersion(optionsFull, {});
// Check for warnings // Check for warnings
const sanitizationWarnings = _settingsImportSanitizeOptions(optionsFull, true); const sanitizationWarnings = this._settingsImportSanitizeOptions(optionsFull, true);
// Show sanitization warnings // Show sanitization warnings
if (sanitizationWarnings.size > 0) { if (sanitizationWarnings.size > 0) {
const {result, sanitize} = await _showSettingsImportWarnings(sanitizationWarnings); const {result, sanitize} = await this._showSettingsImportWarnings(sanitizationWarnings);
if (!result) { return; } if (!result) { return; }
if (sanitize !== false) { if (sanitize !== false) {
_settingsImportSanitizeOptions(optionsFull, false); this._settingsImportSanitizeOptions(optionsFull, false);
} }
} }
// Assign options // Assign options
await _settingsImportSetOptionsFull(optionsFull); await this._settingsImportSetOptionsFull(optionsFull);
// Reload settings page // Reload settings page
window.location.reload(); window.location.reload();
} }
function _onSettingsImportClick() { _onSettingsImportClick() {
document.querySelector('#settings-import-file').click(); document.querySelector('#settings-import-file').click();
} }
function _onSettingsImportFileChange(e) { async _onSettingsImportFileChange(e) {
const files = e.target.files; const files = e.target.files;
if (files.length === 0) { return; } if (files.length === 0) { return; }
const file = files[0]; const file = files[0];
e.target.value = null; e.target.value = null;
_importSettingsFile(file).catch(_showSettingsImportError); try {
} await this._importSettingsFile(file);
} catch (error) {
this._showSettingsImportError(error);
}
}
// Resetting
// Resetting _onSettingsResetClick() {
function _onSettingsResetClick() {
$('#settings-reset-modal').modal('show'); $('#settings-reset-modal').modal('show');
} }
async function _onSettingsResetConfirmClick() { async _onSettingsResetConfirmClick() {
$('#settings-reset-modal').modal('hide'); $('#settings-reset-modal').modal('hide');
// Get default options // Get default options
const optionsFull = optionsGetDefault(); const optionsFull = optionsGetDefault();
// Assign options // Assign options
await _settingsImportSetOptionsFull(optionsFull); await this._settingsImportSetOptionsFull(optionsFull);
// Reload settings page // Reload settings page
window.location.reload(); window.location.reload();
} }
// Setup
function backupInitialize() {
document.querySelector('#settings-export').addEventListener('click', _onSettingsExportClick, false);
document.querySelector('#settings-import').addEventListener('click', _onSettingsImportClick, false);
document.querySelector('#settings-import-file').addEventListener('change', _onSettingsImportFileChange, false);
document.querySelector('#settings-reset').addEventListener('click', _onSettingsResetClick, false);
document.querySelector('#settings-reset-modal-confirm').addEventListener('click', _onSettingsResetConfirmClick, false);
} }

View File

@ -16,13 +16,13 @@
*/ */
/* global /* global
* SettingsBackup
* ankiInitialize * ankiInitialize
* ankiTemplatesInitialize * ankiTemplatesInitialize
* ankiTemplatesUpdateValue * ankiTemplatesUpdateValue
* api * api
* appearanceInitialize * appearanceInitialize
* audioSettingsInitialize * audioSettingsInitialize
* backupInitialize
* dictSettingsInitialize * dictSettingsInitialize
* getOptionsContext * getOptionsContext
* onAnkiOptionsChanged * onAnkiOptionsChanged
@ -302,7 +302,7 @@ async function onReady() {
await dictSettingsInitialize(); await dictSettingsInitialize();
ankiInitialize(); ankiInitialize();
ankiTemplatesInitialize(); ankiTemplatesInitialize();
backupInitialize(); new SettingsBackup().prepare();
storageInfoInitialize(); storageInfoInitialize();

View File

@ -65,12 +65,3 @@ function utilBackend() {
} }
return backend; return backend;
} }
function utilReadFileArrayBuffer(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => resolve(reader.result);
reader.onerror = () => reject(reader.error);
reader.readAsArrayBuffer(file);
});
}