the-glorious-startpage/js/search-query-send.js
Gerome Matilla ae6e1254ea
Quality control (#27)
* fix paddings on screen

* fix  test Variable Assigned to Object Injection Sink

* fix parse int missing base

* fix security issues(regex not included)

* fix missing base

* fixes padding

* minor fixes

* regex

* cleanup

* minor cleanup in webmenu

* cleanups

* cleanups spaces to tab

* cleanups

* spacing tabs fixes test

* cleanup

* cleanup

* multitransition new line

* cleanup

* cleanup

* cleanup

* cleanup

* readme

* comments

* cleanup

* Avoid assignments in operands

* cleanup
2020-06-16 20:07:54 +08:00

59 lines
1.2 KiB
JavaScript

class SearchQuerySend {
constructor() {
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 = () => {
// 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
if (event.key === 'Enter') {
// Don't accept empty strings
if (searchBox.value < 1) {
return;
}
// Search the web
this._sendQuery();
}
}
_registerKeyUpEvent = () => {
this._searchBox.addEventListener('keyup', this._keyUpEvent);
}
}