From 33a446399420546db434cf661d52f1db3838c611 Mon Sep 17 00:00:00 2001 From: buzzcode2007 <73412182+buzz-lightsnack-2007@users.noreply.github.com> Date: Sun, 23 Mar 2025 07:49:14 +0000 Subject: [PATCH] Import and modify URL tester from https://codeberg.org/buzzcode2007/ShopAI-Extension/src/branch/development-chromium/src/scripts/utils/URLs.js --- scripts/utilities/URLs.js | 54 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 scripts/utilities/URLs.js diff --git a/scripts/utilities/URLs.js b/scripts/utilities/URLs.js new file mode 100644 index 0000000..a422749 --- /dev/null +++ b/scripts/utilities/URLs.js @@ -0,0 +1,54 @@ +/* +Custom URL tools for ShopAI +*/ + +class URLs { + /* + This constructor creates a URL object and is the same as new URL(). + + @param {string} PATH the URL + */ + constructor (PATH) { + const URL_OBJECT = new URL(PATH); + + /* Copy the object's data into this object. */ + (Object.keys(URL_OBJECT)).forEach((NAME, VALUE) => { + this[NAME] = VALUE; + }); + }; + + /* + Remove the protocol from the URL. + + @param {string} URL the URL to clean + */ + static clean(PATH) { + return((PATH.trim().replace(/(^\w+:|^)\/\//, ``).split(`?`))[0]); + } + + /* + Check if the URL is valid through checking its patterns. + + @param {string} location the URL to check + @return {boolean} the state + */ + static test(location) { + let STATE = false; + + // Convert to URL object. + + STATE = !!(new URL(location)); + + // Fail the URLs that claim to be invalid. + location.split(`.`).forEach((ENTRY) => { + [`invalidTLD`].forEach((FRAGMENT) => { + STATE = (STATE) ? !(ENTRY.includes(FRAGMENT)) : false; + }) + }) + + + return STATE; + } +} + +module.exports = {URLs}; \ No newline at end of file