aboutsummaryrefslogtreecommitdiff
path: root/src/shared/urls.js
diff options
context:
space:
mode:
authorShin'ya Ueoka <ueokande@i-beam.org>2018-07-29 00:04:24 +0900
committerGitHub <noreply@github.com>2018-07-29 00:04:24 +0900
commitaf90ef59566210fa8715816d639ae1025f2aa2fb (patch)
tree0c66e88229b11c9b154772a6a93dbb6def0d6dcf /src/shared/urls.js
parented2bd7d75ee1e7aa1db7d03c3f908c740ded1983 (diff)
parent09c5247dbadb468a764eef308634503484d398c5 (diff)
Merge pull request #441 from ueokande/search-on-paste
Search on paste
Diffstat (limited to 'src/shared/urls.js')
-rw-r--r--src/shared/urls.js25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/shared/urls.js b/src/shared/urls.js
new file mode 100644
index 0000000..87b1a48
--- /dev/null
+++ b/src/shared/urls.js
@@ -0,0 +1,25 @@
+const trimStart = (str) => {
+ // NOTE String.trimStart is available on Firefox 61
+ return str.replace(/^\s+/, '');
+};
+
+const normalizeUrl = (keywords, searchSettings) => {
+ try {
+ return new URL(keywords).href;
+ } catch (e) {
+ if (keywords.includes('.') && !keywords.includes(' ')) {
+ return 'http://' + keywords;
+ }
+ let template = searchSettings.engines[searchSettings.default];
+ let query = keywords;
+
+ let first = trimStart(keywords).split(' ')[0];
+ if (Object.keys(searchSettings.engines).includes(first)) {
+ template = searchSettings.engines[first];
+ query = trimStart(trimStart(keywords).slice(first.length));
+ }
+ return template.replace('{}', encodeURIComponent(query));
+ }
+};
+
+export { normalizeUrl };