From 429e5bd8529eaae8378fd5adb2d3349b385ffd9c Mon Sep 17 00:00:00 2001 From: buzz-lightsnack-2007 <73412182+buzz-lightsnack-2007@users.noreply.github.com> Date: Mon, 15 Apr 2024 15:16:53 +0800 Subject: [PATCH] put net.js inside a proper module --- scripts/net.js | 68 ++++++++++++++++++++++++++------------------------ 1 file changed, 35 insertions(+), 33 deletions(-) diff --git a/scripts/net.js b/scripts/net.js index b2744c1..1bf88de 100644 --- a/scripts/net.js +++ b/scripts/net.js @@ -11,40 +11,42 @@ Download a file from the network or locally. @param {boolean} STRICT strictly follow the file type provided @returns {Promise} the downloaded file */ -export async function download(URL, TYPE, VERIFY_ONLY = false, STRICT = false) { - const texts = (await import(chrome.runtime.getURL(`gui/scripts/read.js`))) - .default; - const alerts = (await import(chrome.runtime.getURL(`gui/scripts/alerts.js`))).default; - - let CONNECT, DATA; - - try { - CONNECT = await fetch(URL); - - if (CONNECT.ok && !VERIFY_ONLY) { - DATA = await CONNECT.text(); - - if ( - TYPE - ? TYPE.toLowerCase().includes(`json`) || TYPE.toLowerCase().includes(`dictionary`) - : false - ) { - try { - DATA = JSON.parse(DATA); - // When not in JSON, run this. - } catch(err) { - if (STRICT) { - // Should not allow the data to be returned since it's not correct. - DATA = null; - throw new TypeError(texts.localized(`error_msg_notJSON`, false)); - } else {alerts.warn(texts.localized(`error_msg_notJSON`, false));} +export default class net { + static async download(URL, TYPE, VERIFY_ONLY = false, STRICT = false) { + const texts = (await import(chrome.runtime.getURL(`gui/scripts/read.js`))) + .default; + const alerts = (await import(chrome.runtime.getURL(`gui/scripts/alerts.js`))).default; + + let CONNECT, DATA; + + try { + CONNECT = await fetch(URL); + + if (CONNECT.ok && !VERIFY_ONLY) { + DATA = await CONNECT.text(); + + if ( + TYPE + ? TYPE.toLowerCase().includes(`json`) || TYPE.toLowerCase().includes(`dictionary`) + : false + ) { + try { + DATA = JSON.parse(DATA); + // When not in JSON, run this. + } catch(err) { + if (STRICT) { + // Should not allow the data to be returned since it's not correct. + DATA = null; + 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; }