improve url validation
This commit is contained in:
@@ -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 ''
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user