allow url search in searchbox

This commit is contained in:
Gerome Matilla 2020-06-13 08:02:10 +08:00
parent ab3582987b
commit f44eb62ec7

View File

@ -1,18 +1,42 @@
class SearchQuerySend { class SearchQuerySend {
constructor() { constructor() {
// super();
this._searchBox = document.querySelector('#searchBox'); this._searchBox = document.querySelector('#searchBox');
this._keyUpEvent = this._keyUpEvent.bind(this); this._keyUpEvent = this._keyUpEvent.bind(this);
this._registerKeyUpEvent(); 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 = () => { _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); window.location.href = encodeURI(searchEngineSettings.getSearchQueryPrefix() + this._searchBox.value);
}; };
_keyUpEvent = event => { _keyUpEvent = event => {
// Cancel the default action, if needed
event.preventDefault();
if (event.key === 'Tab') return; if (event.key === 'Tab') return;
// Number 13 is the "Enter" key on the keyboard // Number 13 is the "Enter" key on the keyboard
@ -23,9 +47,6 @@ class SearchQuerySend {
return; return;
} }
// Cancel the default action, if needed
event.preventDefault();
// Search the web // Search the web
this._sendQuery() this._sendQuery()
}; };