Skip to content

Commit

Permalink
Support restoring from application/json file
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhill committed Oct 10, 2023
1 parent 0a18f75 commit 4374532
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/js/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,23 @@ import { setAccentColor, setTheme } from './theme.js';
const handleImportFilePicker = function() {
const file = this.files[0];
if ( file === undefined || file.name === '' ) { return; }
if ( file.type.indexOf('text') !== 0 ) { return; }

const reportError = ( ) => {
window.alert(i18n$('aboutRestoreDataError'));
};

const expectedFileTypes = [
'text/plain',
'application/json',
];
if ( expectedFileTypes.includes(file.type) === false ) {
return reportError();
}

const filename = file.name;
const fr = new FileReader();

const fileReaderOnLoadHandler = function() {
fr.onload = function() {
let userData;
try {
userData = JSON.parse(this.result);
Expand All @@ -61,8 +73,7 @@ const handleImportFilePicker = function() {
userData = undefined;
}
if ( userData === undefined ) {
window.alert(i18n$('aboutRestoreDataError'));
return;
return reportError();
}
const time = new Date(userData.timeStamp);
const msg = i18n$('aboutRestoreDataConfirm')
Expand All @@ -76,8 +87,6 @@ const handleImportFilePicker = function() {
});
};

const fr = new FileReader();
fr.onload = fileReaderOnLoadHandler;
fr.readAsText(file);
};

Expand Down

0 comments on commit 4374532

Please sign in to comment.