From 1ce691bec685355324a807dc3c464b59382d81d6 Mon Sep 17 00:00:00 2001 From: buzzcode2007 <73412182+buzz_lightsnack_2007@users.noreply.github.com> Date: Wed, 3 Apr 2024 10:13:52 +0800 Subject: [PATCH] Allow strict file checking If the file is unexpected, an unstrict file checking will only warn the user that the file is unexpected, and it should pass it off anyway. --- scripts/net.js | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/scripts/net.js b/scripts/net.js index 77b1b5c..89b3b80 100644 --- a/scripts/net.js +++ b/scripts/net.js @@ -6,40 +6,46 @@ Download a file from the network or locally. @param {string} URL the URL to download -@param {string} type the expected type of file -@param {boolean} verify_only whether to verify the file only, not return its content +@param {string} TYPE the expected TYPE of file +@param {boolean} VERIFY_ONLY whether to verify the file only, not return its content +@param {boolean} STRICT strictly follow the file type provided @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; +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 = await fetch(URL), - data; + let CONNECT, DATA; - if (connect.ok && !verify_only) { - try { - data = await connect.text(); + try { + CONNECT = await fetch(URL); + + if (CONNECT.ok && !VERIFY_ONLY) { + DATA = await CONNECT.text(); if ( - type - ? type.toLowerCase().includes(`json`) || type.toLowerCase().includes(`dictionary`) + TYPE + ? TYPE.toLowerCase().includes(`json`) || TYPE.toLowerCase().includes(`dictionary`) : false ) { try { - data = JSON.parse(data); + DATA = JSON.parse(DATA); // When not in JSON, run this. } catch(err) { - throw new TypeError(texts.localized(`error_msg_notJSON`, false)); + 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) { - alert.error(err.name, err.message, err.stack); } + } catch(err) { + alerts.error(err.name, err.message, err.stack); + let DATA = null; } // Return the filter. - return verify_only ? connect.ok : data; + return VERIFY_ONLY ? CONNECT.ok : DATA; }