Spaces:
Runtime error
Runtime error
neon_arch
commited on
Commit
•
a727d38
1
Parent(s):
9253d9e
✨ feat: implement code to query the engine based on the options selected under the search bar (#210)
Browse files- public/static/index.js +18 -9
public/static/index.js
CHANGED
@@ -2,16 +2,25 @@
|
|
2 |
* Selects the input element for the search box
|
3 |
* @type {HTMLInputElement}
|
4 |
*/
|
5 |
-
const searchBox = document.querySelector('input')
|
6 |
|
7 |
/**
|
8 |
* Redirects the user to the search results page with the query parameter
|
9 |
*/
|
10 |
function searchWeb() {
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
}
|
16 |
|
17 |
/**
|
@@ -19,7 +28,7 @@ function searchWeb() {
|
|
19 |
* @param {KeyboardEvent} e - The keyboard event object
|
20 |
*/
|
21 |
searchBox.addEventListener('keyup', (e) => {
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
})
|
|
|
2 |
* Selects the input element for the search box
|
3 |
* @type {HTMLInputElement}
|
4 |
*/
|
5 |
+
const searchBox = document.querySelector('input')
|
6 |
|
7 |
/**
|
8 |
* Redirects the user to the search results page with the query parameter
|
9 |
*/
|
10 |
function searchWeb() {
|
11 |
+
const query = searchBox.value.trim()
|
12 |
+
try {
|
13 |
+
let safeSearchLevel = document.querySelector('.search_options select').value
|
14 |
+
if (query) {
|
15 |
+
window.location.href = `search?q=${encodeURIComponent(
|
16 |
+
query,
|
17 |
+
)}&safesearch=${encodeURIComponent(safeSearchLevel)}`
|
18 |
+
}
|
19 |
+
} catch (error) {
|
20 |
+
if (query) {
|
21 |
+
window.location.href = `search?q=${encodeURIComponent(query)}`
|
22 |
+
}
|
23 |
+
}
|
24 |
}
|
25 |
|
26 |
/**
|
|
|
28 |
* @param {KeyboardEvent} e - The keyboard event object
|
29 |
*/
|
30 |
searchBox.addEventListener('keyup', (e) => {
|
31 |
+
if (e.key === 'Enter') {
|
32 |
+
searchWeb()
|
33 |
+
}
|
34 |
+
})
|