Fix settings export/import with non-ASCII content

This commit is contained in:
lewdum 2023-08-02 16:09:54 -03:00
parent af97dd7484
commit 28d180e944
No known key found for this signature in database
GPG key ID: 9523605540EA21DB

View file

@ -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)