From 437453233da28243d6ab55e9ce7b44dfa48915e0 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Tue, 10 Oct 2023 10:16:55 -0400 Subject: [PATCH] Support restoring from `application/json` file Related issue: https://github.com/uBlockOrigin/uBlock-issues/issues/2853 --- src/js/settings.js | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/js/settings.js b/src/js/settings.js index 8653528a403f1..0c196d30b860b 100644 --- a/src/js/settings.js +++ b/src/js/settings.js @@ -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); @@ -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') @@ -76,8 +87,6 @@ const handleImportFilePicker = function() { }); }; - const fr = new FileReader(); - fr.onload = fileReaderOnLoadHandler; fr.readAsText(file); };