replace URL constructor with regex validation for remote URLs

- URL constructor was too strict and failing on valid domains
- new regex pattern validates common URL formats more permissively
- ensures scheme prepending works in both main logic and fetchRemotePageHTML
- fixes issues with domains like www.inrupt.com
This commit is contained in:
Russell Ballestrini 2025-07-12 11:13:40 -04:00
parent c4c216316f
commit 34e5e25bfb

View file

@ -13,12 +13,10 @@ async function fetchRemotePageHTML(url) {
normalizedUrl = 'https://' + normalizedUrl;
}
// Validate URL format
let targetUrl;
try {
targetUrl = new URL(normalizedUrl);
} catch (urlError) {
throw new Error(`Invalid URL format: ${normalizedUrl}`);
// Validate URL format using regex - more permissive than URL constructor
const urlPattern = /^https?:\/\/[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?)*(\.[a-zA-Z]{2,})(:\d+)?(\/[^\s]*)?$/;
if (!urlPattern.test(normalizedUrl)) {
throw new Error(`Please enter a valid website URL (e.g., example.com or https://example.com)`);
}
// Use our CORS proxy at cors-proxy.uncloseai.com with query parameter
@ -331,6 +329,10 @@ export function openTranslateModal() {
if (!remoteUrl) {
throw new Error("Please enter a URL to translate");
}
// Auto-add https:// if no scheme is provided
if (!remoteUrl.startsWith('http://') && !remoteUrl.startsWith('https://')) {
remoteUrl = 'https://' + remoteUrl;
}
// Fetch full HTML content from remote URL
rawContent = await fetchRemotePageHTML(remoteUrl);
}