utils: cleanup and move isvalidurl to utils/url

pull/72/head
OFF0 1 year ago
parent 23188c161f
commit cadd0302a5
Signed by: offbyn
GPG Key ID: 94A2F643C51F37FA

@ -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:
*

@ -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;
};

Loading…
Cancel
Save