aboutsummaryrefslogtreecommitdiff
path: root/src/shared/urls.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/shared/urls.ts')
-rw-r--r--src/shared/urls.ts33
1 files changed, 29 insertions, 4 deletions
diff --git a/src/shared/urls.ts b/src/shared/urls.ts
index 64ea4f2..bac929e 100644
--- a/src/shared/urls.ts
+++ b/src/shared/urls.ts
@@ -7,22 +7,47 @@ const trimStart = (str: string): string => {
const SUPPORTED_PROTOCOLS = ['http:', 'https:', 'ftp:', 'mailto:', 'about:'];
+const isLocalhost = (url: string): boolean => {
+ if (url === 'localhost') {
+ return true;
+ }
+
+ const [host, port] = url.split(':', 2);
+ return host === 'localhost' && !isNaN(Number(port));
+};
+
+const isMissingHttp = (keywords: string): boolean => {
+ if (keywords.includes('.') && !keywords.includes(' ')) {
+ return true;
+ }
+
+ try {
+ const u = new URL('http://' + keywords);
+ return isLocalhost(u.host);
+ } catch (e) {
+ // fallthrough
+ }
+ return false;
+};
+
const searchUrl = (keywords: string, search: Search): string => {
try {
- let u = new URL(keywords);
+ const u = new URL(keywords);
if (SUPPORTED_PROTOCOLS.includes(u.protocol.toLowerCase())) {
return u.href;
}
} catch (e) {
// fallthrough
}
- if (keywords.includes('.') && !keywords.includes(' ')) {
+
+ if (isMissingHttp(keywords)) {
return 'http://' + keywords;
}
+
let template = search.engines[search.defaultEngine];
let query = keywords;
- let first = trimStart(keywords).split(' ')[0];
+ const first = trimStart(keywords).split(' ')[0];
if (Object.keys(search.engines).includes(first)) {
template = search.engines[first];
query = trimStart(trimStart(keywords).slice(first.length));
@@ -32,7 +57,7 @@ const searchUrl = (keywords: string, search: Search): string => {
const normalizeUrl = (url: string): string => {
try {
- let u = new URL(url);
+ const u = new URL(url);
if (SUPPORTED_PROTOCOLS.includes(u.protocol.toLowerCase())) {
return u.href;
}