From 28d180e9441a856eee293c1aa57c5ef725cdcfc6 Mon Sep 17 00:00:00 2001 From: lewdum Date: Wed, 2 Aug 2023 16:09:54 -0300 Subject: [PATCH] Fix settings export/import with non-ASCII content --- src/services/export_import/export_import.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/services/export_import/export_import.js b/src/services/export_import/export_import.js index ac67cf9c..7edde51f 100644 --- a/src/services/export_import/export_import.js +++ b/src/services/export_import/export_import.js @@ -4,11 +4,14 @@ export const newExporter = ({ }) => ({ exportData () { const stringified = JSON.stringify(getExportedObject(), null, 2) // Pretty-print and indent with 2 spaces + const bytes = new TextEncoder().encode(stringified) + const ascii = Array.from(bytes, (x) => String.fromCodePoint(x)).join("") + const data = window.btoa(ascii) // Create an invisible link with a data url and simulate a click const e = document.createElement('a') e.setAttribute('download', `${filename}.json`) - e.setAttribute('href', 'data:application/json;base64,' + window.btoa(stringified)) + e.setAttribute('href', 'data:application/json;base64,' + data) e.style.display = 'none' document.body.appendChild(e) @@ -33,7 +36,9 @@ export const newImporter = ({ const reader = new FileReader() reader.onload = ({ target }) => { try { - const parsed = JSON.parse(target.result) + const bytes = Uint8Array.from(target.result, (x) => x.codePointAt(0)) + const data = new TextDecoder().decode(bytes) + const parsed = JSON.parse(data) const validationResult = validator(parsed) if (validationResult === true) { onImport(parsed)