diff --git a/scripts/net.js b/scripts/net.js index 17348d2..1b9faa8 100644 --- a/scripts/net.js +++ b/scripts/net.js @@ -2,7 +2,7 @@ This script provides network utilities. */ -/* +/* Download a file from the network or locally. @param {string} URL the URL to download @@ -11,17 +11,31 @@ Download a file from the network or locally. @returns {Promise} the downloaded file */ 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), data; if (connect.ok && !verify_only) { - if (type ? type.includes(`json`) || type.includes(`dictionary`) : false) { - data = connect.json(); - } else { - data = connect.text(); + data = await connect.text(); + + try { + } 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 (verify_only) ? connect.ok : data; + return verify_only ? connect.ok : data; }