summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeroen <jeroenpardon@users.noreply.github.com>2020-02-02 08:54:04 +0100
committerGitHub <noreply@github.com>2020-02-02 08:54:04 +0100
commit8bd2e518131aba37d20797814ecaf6d912c95b5f (patch)
treea2630fd38048ab96f5d9a98324af8b009af40a71
parentc8a4cf2486e6b3abe0d644da66d3afefeef9e9ba (diff)
parentac82fa1bd68b36cc6240221ac7d4cf296db79077 (diff)
Merge pull request #5 from raminger/urls_in_search
Added URL handling in search box, thanks
-rwxr-xr-xassets/js/search.js20
1 files changed, 20 insertions, 0 deletions
diff --git a/assets/js/search.js b/assets/js/search.js
index 14bc979..9dcdee7 100755
--- a/assets/js/search.js
+++ b/assets/js/search.js
@@ -110,11 +110,31 @@ function search(text) {
break;
}
}
+ } else if (validURL(text)) {
+ if (containsProtocol(text))
+ window.location = text;
+ else
+ window.location = "https://" + text;
} else {
window.location = "https://www.google.com/search?q=" + text;
}
}
+// Source: https://stackoverflow.com/questions/5717093/check-if-a-javascript-string-is-a-url
+function validURL(str) {
+ var pattern = new RegExp('^(https?:\\/\\/)?' + // protocol
+ '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + // domain name
+ '((\\d{1,3}\\.){3}\\d{1,3}))' + // OR ip (v4) address
+ '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' + // port and path
+ '(\\?[;&a-z\\d%_.~+=-]*)?' + // query string
+ '(\\#[-a-z\\d_]*)?$', 'i'); // fragment locator
+ return !!pattern.test(str);
+}
+
+function containsProtocol(str) {
+ var pattern = new RegExp('^(https?:\\/\\/){1}.*', 'i');
+ return !!pattern.test(str);
+}
String.prototype.replaceAll = function(search, replacement) {
var target = this;