-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
49 lines (41 loc) · 1.95 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Initialize default search engine (Google by default if no engine is saved in localStorage)
let currentEngine = localStorage.getItem('searchEngine') || 'Google';
// Function to set the search engine
function setSearchEngine(engine) {
currentEngine = engine;
document.getElementById('search-input').placeholder = `Search with ${engine}`;
document.getElementById('search-engine-dropdown').classList.add('hidden'); // Hide dropdown after selection
localStorage.setItem('searchEngine', engine); // Save the selected engine to localStorage
}
// Function to perform the search
function search() {
const query = document.getElementById('search-input').value;
let searchUrl = '';
if (currentEngine === 'Google') {
searchUrl = `https://www.google.com/search?q=${query}`;
} else if (currentEngine === 'DuckDuckGo') {
searchUrl = `https://duckduckgo.com/?q=${query}`;
} else if (currentEngine === 'Bing') {
searchUrl = `https://www.bing.com/search?q=${query}`;
}
if (query) {
window.open(searchUrl, '_blank');
}
}
// Add event listener for Enter key press inside search input
document.getElementById('search-input').addEventListener('keypress', function(event) {
if (event.key === 'Enter') {
search(); // Trigger the search function when Enter is pressed
}
});
// Focus the search input field when the page loads and set the default search engine
window.onload = function() {
document.getElementById('search-input').focus(); // Automatically focus on the search input
// Set the placeholder to match the saved search engine on page load
document.getElementById('search-input').placeholder = `Search with ${currentEngine}`;
};
// Toggle dropdown visibility when magnifying glass is clicked
document.getElementById('search-icon').addEventListener('click', function() {
const dropdown = document.getElementById('search-engine-dropdown');
dropdown.classList.toggle('hidden');
});