improve url validation

This commit is contained in:
2024-04-10 09:22:35 +02:00
parent 248c3e94ab
commit 3af6013dbb

View File

@@ -12,8 +12,21 @@ type CheckResponse = {
function extractDomain(url: string): string {
try {
const urlObj = new URL(url)
return urlObj.hostname.replace('www.', '')
// Clean up the URL first
let cleanUrl = url.trim()
if (!cleanUrl.startsWith('http://') && !cleanUrl.startsWith('https://')) {
cleanUrl = 'https://' + cleanUrl
}
const urlObj = new URL(cleanUrl)
let domain = urlObj.hostname.toLowerCase()
// Remove common prefixes
domain = domain.replace(/^www\./, '')
domain = domain.replace(/^m\./, '')
domain = domain.replace(/^mobile\./, '')
return domain
} catch {
return ''
}