0
0
mirror of https://github.com/neon-mmd/websurfx.git synced 2024-11-22 14:08:23 -05:00
websurfx/public/static/index.js
neon_arch d52da3aeb4
Clear button for the search bar (#608)
* feat(icons): add a new icon for clear button on the search bar (#394)

*  feat: add clear button near the search bar input field (#394)

*  feat: add a function to clear search input text on click event (#394)

*  feat: disable unstable webkit search and clear button (#394)

* 🔖 chore(release): bump the app version (#394)

* 🩹 fix: rename the pseudo css selector according to the `mdn` docs (#394)

Co-authored-by: Jann Marc Villablanca <31008330+jfvillablanca@users.noreply.github.com>

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Co-authored-by: Jann Marc Villablanca <31008330+jfvillablanca@users.noreply.github.com>
2024-10-08 12:09:41 +00:00

42 lines
1.1 KiB
JavaScript

/**
* Selects the input element for the search box
* @type {HTMLInputElement}
*/
const searchBox = document.querySelector('input')
/**
* Redirects the user to the search results page with the query parameter
*/
function searchWeb() {
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)}`
}
}
}
/**
* 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) => {
if (e.key === 'Enter') {
searchWeb()
}
})
/**
* A function that clears the search input text when the clear button is clicked.
*/
function clearSearchText() {
document.querySelector('.search_bar input').value = ''
}