From 9a3b38d50873a12a344a3bc48c8981547076b038 Mon Sep 17 00:00:00 2001 From: Red J Adaya Date: Fri, 9 Aug 2024 02:58:22 +0800 Subject: [PATCH] url bar google search and improved url detection (#204) --- frontend/app/view/webview/webview.tsx | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/frontend/app/view/webview/webview.tsx b/frontend/app/view/webview/webview.tsx index 5187ea07b..60377b398 100644 --- a/frontend/app/view/webview/webview.tsx +++ b/frontend/app/view/webview/webview.tsx @@ -199,14 +199,30 @@ export class WebViewModel implements ViewModel { } ensureUrlScheme(url: string) { - if (/^(localhost|(\d{1,3}\.){3}\d{1,3})(:\d+)?/.test(url)) { - // If the URL starts with localhost or an IP address (with optional port) + if (/^(http|https):/.test(url)) { + // If the URL starts with http: or https:, return it as is + return url; + } + + // Check if the URL looks like a local URL + const isLocal = /^(localhost|(\d{1,3}\.){3}\d{1,3})(:\d+)?$/.test(url.split("/")[0]); + + if (isLocal) { + // If it is a local URL, ensure it has http:// scheme return `http://${url}`; - } else if (!/^[a-zA-Z][a-zA-Z\d+\-.]*:/.test(url)) { - // If the URL doesn't start with a protocol + } + + // Check if the URL looks like a domain + const domainRegex = /^[a-z0-9.-]+\.[a-z]{2,}$/i; + const isDomain = domainRegex.test(url.split("/")[0]); + + if (isDomain) { + // If it looks like a domain, ensure it has https:// scheme return `https://${url}`; } - return url; + + // Otherwise, treat it as a search query + return `https://www.google.com/search?q=${encodeURIComponent(url)}`; } normalizeUrl(url: string) {