the fallback is always text
This commit is contained in:
parent
62ad4d58bf
commit
76bf6cefaa
1 changed files with 20 additions and 6 deletions
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue