diff --git a/src/utils/dom.ts b/src/utils/dom.ts index 06b8cd0..81e9d7f 100644 --- a/src/utils/dom.ts +++ b/src/utils/dom.ts @@ -1,3 +1,5 @@ +import {isValidURL} from './url'; + type DataAttributes = { data: { [key: string]: string | number; @@ -62,32 +64,6 @@ export const lockScroll = () => document.body.style.overflow = 'hidden'; /** free global page scrolling */ export const unlockScroll = () => document.body.style.removeProperty('overflow'); -export const isValidURL = (url: URL) => { - if (!['http:', 'https:'].includes(url.protocol)) { - return false; - } - if (!['', '443', '80'].includes(url.port)) { - return false; - } - if (url.hostname === 'localhost') { - return false; - } - const lastDot = url.hostname.lastIndexOf('.'); - if (lastDot < 1) { - return false; - } - if (url.hostname.slice(lastDot) === '.local') { - return false; - } - if (url.hostname.slice(lastDot + 1).match(/^[\d]+$/)) { // there should be no tld with numbers, possible ipv4 - return false; - } - if (url.hostname.includes(':')) { // possibly an ipv6 addr; certainly an invalid hostname - return false; - } - return true; -} - /** * example usage: * diff --git a/src/utils/url.ts b/src/utils/url.ts index 0309cd9..4503605 100644 --- a/src/utils/url.ts +++ b/src/utils/url.ts @@ -37,3 +37,29 @@ export const getNoxyUrl = ( link.searchParams.set('url', url); return link; }; + +export const isValidURL = (url: URL) => { + if (!['http:', 'https:'].includes(url.protocol)) { + return false; + } + if (!['', '443', '80'].includes(url.port)) { + return false; + } + if (url.hostname === 'localhost') { + return false; + } + const lastDot = url.hostname.lastIndexOf('.'); + if (lastDot < 1) { + return false; + } + if (url.hostname.slice(lastDot) === '.local') { + return false; + } + if (url.hostname.slice(lastDot + 1).match(/^[\d]+$/)) { // there should be no tld with numbers, possible ipv4 + return false; + } + if (url.hostname.includes(':')) { // possibly an ipv6 addr; certainly an invalid hostname + return false; + } + return true; +};