diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2019-11-11 03:31:58 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-11-11 03:31:58 +0000 |
commit | ae77b386f4da2cbd46c45fe6fece58a5e521fff5 (patch) | |
tree | c5ea2e8c001399a385f99c05c7790eb23df9ccae /src/shared | |
parent | f93ed4149b1a88d0e887fc8270239e0a28ee23db (diff) | |
parent | ddd3b08a4a3b393375b6e9716697b9442fcfa66d (diff) |
Merge pull request #666 from chen-chao/localhost
support url started with localhost
Diffstat (limited to 'src/shared')
-rw-r--r-- | src/shared/urls.ts | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/src/shared/urls.ts b/src/shared/urls.ts index 64ea4f2..8fa9a49 100644 --- a/src/shared/urls.ts +++ b/src/shared/urls.ts @@ -7,6 +7,29 @@ const trimStart = (str: string): string => { const SUPPORTED_PROTOCOLS = ['http:', 'https:', 'ftp:', 'mailto:', 'about:']; +const isLocalhost = (url: string): boolean => { + if (url === 'localhost') { + return true; + } + + let [host, port] = url.split(':', 2); + return host === 'localhost' && !isNaN(Number(port)); +}; + +const isMissingHttp = (keywords: string): boolean => { + if (keywords.includes('.') && !keywords.includes(' ')) { + return true; + } + + try { + let 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); @@ -16,9 +39,11 @@ const searchUrl = (keywords: string, search: Search): string => { } catch (e) { // fallthrough } - if (keywords.includes('.') && !keywords.includes(' ')) { + + if (isMissingHttp(keywords)) { return 'http://' + keywords; } + let template = search.engines[search.defaultEngine]; let query = keywords; |