the fallback is always text

This commit is contained in:
buzz-lightsnack-2007 2024-04-02 19:59:26 +08:00
parent 62ad4d58bf
commit 76bf6cefaa

View file

@ -2,7 +2,7 @@
This script provides network utilities. This script provides network utilities.
*/ */
/* /*
Download a file from the network or locally. Download a file from the network or locally.
@param {string} URL the URL to download @param {string} URL the URL to download
@ -11,17 +11,31 @@ Download a file from the network or locally.
@returns {Promise} the downloaded file @returns {Promise} the downloaded file
*/ */
export async function download(URL, type, verify_only = false) { export async function download(URL, type, verify_only = false) {
const alert = await import(chrome.runtime.getURL(`gui/scripts/alerts.js`))
.default;
const texts = (await import(chrome.runtime.getURL(`gui/scripts/read.js`)))
.default;
let connect = await fetch(URL), let connect = await fetch(URL),
data; data;
if (connect.ok && !verify_only) { if (connect.ok && !verify_only) {
if (type ? type.includes(`json`) || type.includes(`dictionary`) : false) { data = await connect.text();
data = connect.json();
} else { try {
data = connect.text(); } catch (err) {
if (
type
? type.toLowerCase().includes(`json`) ||
type.toLowerCase().includes(`dictionary`)
: false
) {
data = JSON.parse(data);
}
alert.error(texts.localized(`error_msg_notJSON`, false));
} }
} }
// Return the filter. // Return the filter.
return (verify_only) ? connect.ok : data; return verify_only ? connect.ok : data;
} }