From a727d389ae8ad07d8ffcdb7c3be3c71fb3a626ea Mon Sep 17 00:00:00 2001 From: neon_arch Date: Fri, 22 Sep 2023 19:59:06 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20implement=20code=20to=20que?= =?UTF-8?q?ry=20the=20engine=20based=20on=20the=20options=20selected=20und?= =?UTF-8?q?er=20the=20search=20bar=20(#210)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/static/index.js | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/public/static/index.js b/public/static/index.js index 050829a..515065a 100644 --- a/public/static/index.js +++ b/public/static/index.js @@ -2,16 +2,25 @@ * Selects the input element for the search box * @type {HTMLInputElement} */ -const searchBox = document.querySelector('input'); +const searchBox = document.querySelector('input') /** * Redirects the user to the search results page with the query parameter */ function searchWeb() { - const query = searchBox.value.trim(); - if (query) { - window.location.href = `search?q=${encodeURIComponent(query)}`; - } + 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)}` + } + } } /** @@ -19,7 +28,7 @@ function searchWeb() { * @param {KeyboardEvent} e - The keyboard event object */ searchBox.addEventListener('keyup', (e) => { - if (e.key === 'Enter') { - searchWeb(); - } -}); + if (e.key === 'Enter') { + searchWeb() + } +})