From f44eb62ec7b66a1bceb5ccc7f455c8b78b0abd47 Mon Sep 17 00:00:00 2001 From: Gerome Matilla Date: Sat, 13 Jun 2020 08:02:10 +0800 Subject: [PATCH] allow url search in searchbox --- js/search-query-send.js | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/js/search-query-send.js b/js/search-query-send.js index 4bbdd68..c259c11 100644 --- a/js/search-query-send.js +++ b/js/search-query-send.js @@ -1,18 +1,42 @@ class SearchQuerySend { constructor() { - // super(); this._searchBox = document.querySelector('#searchBox'); this._keyUpEvent = this._keyUpEvent.bind(this); + this._registerKeyUpEvent(); } + // Check if search query is a valid url + _isURL = u => { + let dummyInput; + + if (!dummyInput) { + dummyInput = document.createElement('input'); + dummyInput.setAttribute('type', 'url'); + } + + dummyInput.value = u; + return dummyInput.validity.valid; + } + + // Search query _sendQuery = () => { - // Search + + // If search query is a url, open it + if (this._isURL(this._searchBox.value)) { + window.location.href = encodeURI(this._searchBox.value); + return; + } + + // Web search window.location.href = encodeURI(searchEngineSettings.getSearchQueryPrefix() + this._searchBox.value); }; _keyUpEvent = event => { + // Cancel the default action, if needed + event.preventDefault(); + if (event.key === 'Tab') return; // Number 13 is the "Enter" key on the keyboard @@ -23,9 +47,6 @@ class SearchQuerySend { return; } - // Cancel the default action, if needed - event.preventDefault(); - // Search the web this._sendQuery() };