feat: webui editing support multiple encodings (#197)
This commit is contained in:
parent
10ec34872d
commit
e43554b795
1 changed files with 20 additions and 2 deletions
|
@ -535,8 +535,15 @@ async function setupEditPage() {
|
||||||
try {
|
try {
|
||||||
const res = await fetch(baseUrl());
|
const res = await fetch(baseUrl());
|
||||||
await assertResOK(res);
|
await assertResOK(res);
|
||||||
const text = await res.text();
|
const encoding = getEncoding(res.headers.get("content-type"));
|
||||||
$editor.value = text;
|
if (encoding === "utf-8") {
|
||||||
|
$editor.value = await res.text();
|
||||||
|
} else {
|
||||||
|
const bytes = await res.arrayBuffer();
|
||||||
|
const dataView = new DataView(bytes)
|
||||||
|
const decoder = new TextDecoder(encoding)
|
||||||
|
$editor.value = decoder.decode(dataView);
|
||||||
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
alert(`Failed get file, ${err.message}`);
|
alert(`Failed get file, ${err.message}`);
|
||||||
}
|
}
|
||||||
|
@ -796,3 +803,14 @@ async function assertResOK(res) {
|
||||||
throw new Error(await res.text())
|
throw new Error(await res.text())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getEncoding(contentType) {
|
||||||
|
const charset = contentType?.split(";")[1];
|
||||||
|
if (/charset/i.test(charset)) {
|
||||||
|
let encoding = charset.split("=")[1];
|
||||||
|
if (encoding) {
|
||||||
|
return encoding.toLowerCase()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 'utf-8'
|
||||||
|
}
|
Loading…
Reference in a new issue