From 34e5e25bfb341d5dd1b6d23dd1874f0198c5f95d Mon Sep 17 00:00:00 2001 From: Russell Ballestrini Date: Sat, 12 Jul 2025 11:13:40 -0400 Subject: [PATCH] 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 --- src/translate-modal.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/translate-modal.js b/src/translate-modal.js index 9551047..786bcb0 100644 --- a/src/translate-modal.js +++ b/src/translate-modal.js @@ -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); }