2023-06-07 06:44:54 -04:00
|
|
|
/**
|
|
|
|
* Selects the input element for the search box
|
|
|
|
* @type {HTMLInputElement}
|
|
|
|
*/
|
2023-09-22 12:59:06 -04:00
|
|
|
const searchBox = document.querySelector('input')
|
2023-06-07 06:44:54 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Redirects the user to the search results page with the query parameter
|
|
|
|
*/
|
|
|
|
function searchWeb() {
|
2023-09-22 12:59:06 -04:00
|
|
|
const query = searchBox.value.trim()
|
|
|
|
try {
|
|
|
|
let safeSearchLevel = document.querySelector('.search_options select').value
|
|
|
|
if (query) {
|
|
|
|
window.location.href = `search?q=${encodeURIComponent(
|
|
|
|
query,
|
|
|
|
)}&safesearch=${encodeURIComponent(safeSearchLevel)}`
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
if (query) {
|
|
|
|
window.location.href = `search?q=${encodeURIComponent(query)}`
|
|
|
|
}
|
|
|
|
}
|
2023-04-22 07:35:07 -04:00
|
|
|
}
|
|
|
|
|
2023-06-07 06:44:54 -04:00
|
|
|
/**
|
|
|
|
* Listens for the 'Enter' key press event on the search box and calls the searchWeb function
|
|
|
|
* @param {KeyboardEvent} e - The keyboard event object
|
|
|
|
*/
|
|
|
|
searchBox.addEventListener('keyup', (e) => {
|
2023-09-22 12:59:06 -04:00
|
|
|
if (e.key === 'Enter') {
|
|
|
|
searchWeb()
|
|
|
|
}
|
|
|
|
})
|
2024-10-08 08:09:41 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A function that clears the search input text when the clear button is clicked.
|
|
|
|
*/
|
|
|
|
function clearSearchText() {
|
|
|
|
document.querySelector('.search_bar input').value = ''
|
|
|
|
}
|