the-glorious-startpage/js/search-engine-settings.js

102 lines
3.0 KiB
JavaScript
Raw Normal View History

2020-06-09 04:02:03 +02:00
class SearchEngineSettings {
2020-06-09 04:02:03 +02:00
constructor() {
this._localStorage = window.localStorage;
2020-06-09 04:02:03 +02:00
this._searchBox = document.querySelector('#searchBox');
this._selectSearchEngine = document.querySelector('#searchEngineSelect');
this._selectSearchEngineApply = document.querySelector('#searchEngineAsDefault');
this._placeholderPrefix = ' Search with ';
this._searchQueryPrefix = 'http://www.google.com/search?q=';
this._searchEngines = config.getSearchEngines();
2020-06-09 04:02:03 +02:00
this._onClickEvent = this._onClickEvent.bind(this);
this._onChangeEvent = this._onChangeEvent.bind(this);
2020-06-09 04:02:03 +02:00
this._init();
}
2020-06-09 04:02:03 +02:00
_init = () => {
this._updateDefaultSearchEngine();
this._createSearchEngineOptions();
2020-06-09 04:02:03 +02:00
this._selectTheEngine();
this._registerOnChangeEvent();
this._registerOnClickEvent();
}
_updateDefaultSearchEngine = () => {
// Update default search engine and current search engine
this._defaultSearchEngine = this._localStorage.getItem('searchEngine') || 'google';
this._currentSearchEngine = this._defaultSearchEngine;
}
2020-06-09 04:02:03 +02:00
// Get query prefix
getSearchQueryPrefix = () => {
return this._searchQueryPrefix;
}
2020-06-09 04:02:03 +02:00
// Parse the this._searchEngines object to automatically create a selection
_createSearchEngineOptions = () => {
Object.keys(this._searchEngines)
.forEach(key => {
const seValue = key;
const seData = this._searchEngines[key];
const seOption = document.createElement('option');
seOption.value = seValue;
seOption.innerText = seData.name;
this._selectSearchEngine.appendChild(seOption);
})
// Call to update query string and placeholder
this._updateSearchEngineElements();
}
2020-06-09 04:02:03 +02:00
// Update query string and placeholder
_updateSearchEngineElements = () => {
2020-06-09 04:02:03 +02:00
const seData = this._searchEngines[this._currentSearchEngine];
this._searchQueryPrefix = seData.prefix;
this._searchBox.placeholder = this._placeholderPrefix + seData.name;
2020-06-09 04:02:03 +02:00
}
// Use this to select the current/default search engine on startup
2020-06-09 04:02:03 +02:00
_selectTheEngine = () => {
this._selectSearchEngine.value = this._currentSearchEngine;
this._updateSearchEngineElements();
2020-06-09 04:02:03 +02:00
}
// Execute this on change event of <select>
2020-06-09 04:02:03 +02:00
_onChangeEvent = e => {
const selectedEngine = this._selectSearchEngine.options[this._selectSearchEngine.selectedIndex].value;
this._currentSearchEngine = selectedEngine;
this._selectTheEngine();
2020-06-09 04:02:03 +02:00
}
_registerOnChangeEvent = () => {
this._selectSearchEngine.onchange = this._onChangeEvent;
}
// Apply button callback
2020-06-09 04:02:03 +02:00
_onClickEvent = e => {
// Get selected <options>
const selectCurrentIndex = this._selectSearchEngine.options[this._selectSearchEngine.selectedIndex];
// Alert
2020-06-09 04:02:03 +02:00
alert('Success! ' + selectCurrentIndex.text +
' is now your default search engine!');
// Save and apply default search engine
2020-06-09 04:02:03 +02:00
this._localStorage.setItem('searchEngine', selectCurrentIndex.value);
// Update default search engine and current search engine
this._updateDefaultSearchEngine();
2020-06-09 04:02:03 +02:00
}
2020-06-09 04:02:03 +02:00
_registerOnClickEvent = () => {
this._selectSearchEngineApply.onclick = this._onClickEvent;
}
2020-06-09 04:02:03 +02:00
}