put net.js inside a proper module

This commit is contained in:
buzz-lightsnack-2007 2024-04-15 15:16:53 +08:00
parent b5bcb59de2
commit 429e5bd852

View file

@ -11,40 +11,42 @@ Download a file from the network or locally.
@param {boolean} STRICT strictly follow the file type provided @param {boolean} STRICT strictly follow the file type provided
@returns {Promise} the downloaded file @returns {Promise} the downloaded file
*/ */
export async function download(URL, TYPE, VERIFY_ONLY = false, STRICT = false) { export default class net {
const texts = (await import(chrome.runtime.getURL(`gui/scripts/read.js`))) static async download(URL, TYPE, VERIFY_ONLY = false, STRICT = false) {
.default; const texts = (await import(chrome.runtime.getURL(`gui/scripts/read.js`)))
const alerts = (await import(chrome.runtime.getURL(`gui/scripts/alerts.js`))).default; .default;
const alerts = (await import(chrome.runtime.getURL(`gui/scripts/alerts.js`))).default;
let CONNECT, DATA;
let CONNECT, DATA;
try {
CONNECT = await fetch(URL); try {
CONNECT = await fetch(URL);
if (CONNECT.ok && !VERIFY_ONLY) {
DATA = await CONNECT.text(); if (CONNECT.ok && !VERIFY_ONLY) {
DATA = await CONNECT.text();
if (
TYPE if (
? TYPE.toLowerCase().includes(`json`) || TYPE.toLowerCase().includes(`dictionary`) TYPE
: false ? TYPE.toLowerCase().includes(`json`) || TYPE.toLowerCase().includes(`dictionary`)
) { : false
try { ) {
DATA = JSON.parse(DATA); try {
// When not in JSON, run this. DATA = JSON.parse(DATA);
} catch(err) { // When not in JSON, run this.
if (STRICT) { } catch(err) {
// Should not allow the data to be returned since it's not correct. if (STRICT) {
DATA = null; // Should not allow the data to be returned since it's not correct.
throw new TypeError(texts.localized(`error_msg_notJSON`, false)); DATA = null;
} else {alerts.warn(texts.localized(`error_msg_notJSON`, false));} throw new TypeError(texts.localized(`error_msg_notJSON`, false));
} else {alerts.warn(texts.localized(`error_msg_notJSON`, false));}
};
}; };
}; }
} catch(err) {
throw err;
} }
} catch(err) {
throw err; // Return the filter.
return VERIFY_ONLY ? CONNECT.ok : DATA;
} }
// Return the filter.
return VERIFY_ONLY ? CONNECT.ok : DATA;
} }