2020-06-10 03:09:43 +02:00
|
|
|
class SearchQuerySend {
|
2020-06-09 04:02:03 +02:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this._searchBox = document.querySelector('#searchBox');
|
|
|
|
this._keyUpEvent = this._keyUpEvent.bind(this);
|
2020-06-13 02:02:10 +02:00
|
|
|
|
2020-06-09 04:02:03 +02:00
|
|
|
this._registerKeyUpEvent();
|
|
|
|
}
|
|
|
|
|
2020-06-13 02:02:10 +02:00
|
|
|
// 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
|
2020-06-09 04:02:03 +02:00
|
|
|
_sendQuery = () => {
|
2020-06-13 02:02:10 +02:00
|
|
|
|
|
|
|
// If search query is a url, open it
|
|
|
|
if (this._isURL(this._searchBox.value)) {
|
|
|
|
window.location.href = encodeURI(this._searchBox.value);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Web search
|
2020-06-10 03:09:43 +02:00
|
|
|
window.location.href = encodeURI(searchEngineSettings.getSearchQueryPrefix() + this._searchBox.value);
|
2020-06-09 04:02:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
_keyUpEvent = event => {
|
2020-06-13 02:02:10 +02:00
|
|
|
// Cancel the default action, if needed
|
|
|
|
event.preventDefault();
|
|
|
|
|
2020-06-06 05:34:20 +02:00
|
|
|
if (event.key === 'Tab') return;
|
|
|
|
|
2020-06-04 05:27:00 +02:00
|
|
|
// Number 13 is the "Enter" key on the keyboard
|
|
|
|
if (event.key === 'Enter') {
|
|
|
|
|
|
|
|
// Don't accept empty strings
|
|
|
|
if (searchBox.value < 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search the web
|
2020-06-09 04:02:03 +02:00
|
|
|
this._sendQuery()
|
2020-06-04 05:27:00 +02:00
|
|
|
};
|
2020-06-09 04:02:03 +02:00
|
|
|
}
|
2020-06-04 05:27:00 +02:00
|
|
|
|
2020-06-09 04:02:03 +02:00
|
|
|
_registerKeyUpEvent = () => {
|
|
|
|
this._searchBox.addEventListener('keyup', this._keyUpEvent);
|
2020-06-04 05:27:00 +02:00
|
|
|
}
|
2020-06-09 04:02:03 +02:00
|
|
|
}
|