2020-06-04 05:20:12 +02:00
|
|
|
var localStorage = window.localStorage;
|
|
|
|
|
|
|
|
var searchBox = document.getElementById("searchBox");
|
2020-06-07 09:18:09 +02:00
|
|
|
const selectEngine = document.getElementById("searchEngineSelect");
|
|
|
|
const searchEngineAsDefault = document.getElementById("searchEngineAsDefault");
|
2020-06-04 05:20:12 +02:00
|
|
|
|
|
|
|
// Get default search engine
|
2020-06-07 09:18:09 +02:00
|
|
|
let defaultEngine = localStorage.getItem('searchEngine') || 'google';
|
2020-06-04 05:20:12 +02:00
|
|
|
|
|
|
|
let searchQueryPrefix;
|
|
|
|
|
2020-06-07 09:18:09 +02:00
|
|
|
const placeholderPrefix = " Search with ";
|
2020-06-05 04:39:40 +02:00
|
|
|
|
2020-06-04 05:20:12 +02:00
|
|
|
// Update query string and placeholder
|
|
|
|
const selectQueryString = () => {
|
|
|
|
|
|
|
|
if (defaultEngine === "google") {
|
|
|
|
searchQueryPrefix = 'http://www.google.com/search?q=';
|
2020-06-05 04:39:40 +02:00
|
|
|
searchBox.placeholder = placeholderPrefix + "Google";
|
2020-06-04 05:20:12 +02:00
|
|
|
|
|
|
|
} else if (defaultEngine === "duckduckgo") {
|
|
|
|
searchQueryPrefix = 'https://duckduckgo.com/?q=';
|
2020-06-05 04:39:40 +02:00
|
|
|
searchBox.placeholder = placeholderPrefix + "Duckduckgo";
|
2020-06-04 05:20:12 +02:00
|
|
|
|
|
|
|
} else if (defaultEngine === "ecosia") {
|
|
|
|
searchQueryPrefix = 'https://www.ecosia.org/search?q=';
|
2020-06-05 04:39:40 +02:00
|
|
|
searchBox.placeholder = placeholderPrefix + "Ecosia";
|
2020-06-04 05:20:12 +02:00
|
|
|
|
|
|
|
} else if (defaultEngine === "yahoo") {
|
|
|
|
searchQueryPrefix = 'http://search.yahoo.com/search?p=';
|
2020-06-05 04:39:40 +02:00
|
|
|
searchBox.placeholder = placeholderPrefix + "Yahoo";
|
2020-06-04 05:20:12 +02:00
|
|
|
|
|
|
|
} else if (defaultEngine === "bing") {
|
|
|
|
searchQueryPrefix = 'https://www.bing.com/search?q=';
|
2020-06-05 04:39:40 +02:00
|
|
|
searchBox.placeholder = placeholderPrefix + "Bing";
|
2020-06-04 05:20:12 +02:00
|
|
|
|
|
|
|
} else {
|
|
|
|
searchQueryPrefix = 'http://www.google.com/search?q=';
|
2020-06-05 04:39:40 +02:00
|
|
|
searchBox.placeholder = placeholderPrefix + "Google";
|
2020-06-04 05:20:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use this to select the default search engine on startup
|
|
|
|
const selectTheEngine = () => {
|
|
|
|
// Available values: google, duckduckgo, ecosia, etc.
|
|
|
|
selectEngine.value = defaultEngine;
|
|
|
|
selectQueryString();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update settings if the value changes
|
|
|
|
selectEngine.onchange = () => {
|
|
|
|
// Get Value
|
2020-06-07 09:18:09 +02:00
|
|
|
const selectedEngine = selectEngine.options[selectEngine.selectedIndex].value;
|
2020-06-04 05:20:12 +02:00
|
|
|
defaultEngine = selectedEngine;
|
|
|
|
selectTheEngine()
|
|
|
|
}
|
|
|
|
|
2020-06-07 08:34:27 +02:00
|
|
|
searchEngineAsDefault.onclick = () => {
|
2020-06-07 09:18:09 +02:00
|
|
|
const selectCurrentIndex = selectEngine.options[selectEngine.selectedIndex]
|
2020-06-04 05:20:12 +02:00
|
|
|
alert('Success! ' + selectCurrentIndex.text +
|
|
|
|
' is now your default search engine!');
|
|
|
|
|
|
|
|
// Save search engine
|
|
|
|
localStorage.setItem('searchEngine', selectCurrentIndex.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize
|
|
|
|
window.onload = selectTheEngine();
|