[Glitch] Profile editing: Add warning for links
Port 12c6c6dcf9 to glitch-soc
Signed-off-by: Claire <claire.github-309c@sitedethib.com>
This commit is contained in:
1 parent
3ed3ce4e05
commit
006b06199f
3 files changed
+66
-1
No files matched your search
@@ -9,3 +9,29 @@ export function isValidUrl(
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the input string is probably a URL without a protocol. Note this is not full URL validation,
|
||||
* and is mostly used to detect link-like inputs.
|
||||
* @see https://www.xjavascript.com/blog/check-if-a-javascript-string-is-a-url/
|
||||
* @param input The input string to check
|
||||
*/
|
||||
export function isUrlWithoutProtocol(input: string): boolean {
|
||||
if (!input.length || input.includes(' ') || input.includes('://')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
const url = new URL(`http://${input}`);
|
||||
const { host } = url;
|
||||
return (
|
||||
host !== '' && // Host is not empty
|
||||
host.includes('.') && // Host contains at least one dot
|
||||
!host.endsWith('.') && // No trailing dot
|
||||
!host.includes('..') && // No consecutive dots
|
||||
/\.[\w]{2,}$/.test(host) // TLD is at least 2 characters
|
||||
);
|
||||
} catch {}
|
||||
|
||||
return false;
|
||||
}
|
||||
Reference in new issue
Block a user