diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2017-09-18 21:48:34 +0900 |
---|---|---|
committer | Shin'ya Ueoka <ueokande@i-beam.org> | 2017-09-18 21:48:34 +0900 |
commit | 0e924a27d1fee402312e40f3cba978e1adb20661 (patch) | |
tree | 0ea7406979fc8f2b40da3196a3de0a757e7dc58f /src/actions | |
parent | 8a8222158cd2b2d2277b3cc956ad97517e64c65d (diff) |
search with engines
Diffstat (limited to 'src/actions')
-rw-r--r-- | src/actions/command.js | 33 |
1 files changed, 29 insertions, 4 deletions
diff --git a/src/actions/command.js b/src/actions/command.js index 3e879a6..022e4f6 100644 --- a/src/actions/command.js +++ b/src/actions/command.js @@ -2,11 +2,36 @@ import * as tabs from '../background/tabs'; import * as histories from '../background/histories'; import * as consoleActions from './console'; -const normalizeUrl = (string) => { +const DEFAULT_SEARCH_ENGINES = { + default: 'google', + engines: { + 'google': 'https://google.com/search?q={}', + 'yahoo': 'https://search.yahoo.com/search?p={}', + 'bing': 'https://www.bing.com/search?q={}', + 'duckduckgo': 'https://duckduckgo.com/?q={}', + 'twitter': 'https://twitter.com/search?q={}', + 'wikipedia': 'https://en.wikipedia.org/w/index.php?search={}' + } +}; + +const normalizeUrl = (string, searchConfig) => { try { return new URL(string).href; } catch (e) { - return 'http://' + string; + if (string.includes('.') && !string.includes(' ')) { + return 'http://' + string; + } + let query = encodeURI(string); + let template = searchConfig.engines[ + searchConfig.default + ]; + for (let key in searchConfig.engines) { + if (string.startsWith(key + ' ')) { + query = encodeURI(string.replace(key + ' ', '')); + template = searchConfig.engines[key]; + } + } + return template.replace('{}', query); } }; @@ -43,10 +68,10 @@ const doCommand = (name, remaining) => { case 'o': case 'open': // TODO use search engined and pass keywords to them - return openCommand(normalizeUrl(remaining)); + return openCommand(normalizeUrl(remaining, DEFAULT_SEARCH_ENGINES)); case 't': case 'tabopen': - return tabopenCommand(normalizeUrl(remaining)); + return tabopenCommand(normalizeUrl(remaining, DEFAULT_SEARCH_ENGINES)); case 'b': case 'buffer': return bufferCommand(remaining); |