pub / serci

Search the web with !keywords
git clone https://https://src.jayvii.de/pub/serci.git
Home | Log | Files | Exports | Refs | README | RSS

commit 9b9da13251d2e1fec60631846fc3d22c29c08df6
parent 74a355be89889782d0e460d041651203b70f62af
Author: JayVii <jayvii[AT]posteo[DOT]de>
Date:   Mon, 16 Sep 2024 11:32:30 +0200

feat: let user choose default search engine

Diffstat:
Mindex.php | 61+++++++++++++++++++++++++++++++++++++++++++++++++++++--------
1 file changed, 53 insertions(+), 8 deletions(-)

diff --git a/index.php b/index.php @@ -20,6 +20,17 @@ $query = null; } + /* Handle Default Search Engine cookie (if set) */ + if (!is_null($_COOKIE["default_search"])) { + /* Use search engine stored in cookie; refresh cookie */ + $default_search = $_COOKIE["default_search"]; + setcookie( + "default_search", + $default_search, + time() + (60 * 60 * 24 * 365) + ); + } + if (strlen($query) > 0) { /* Find keywords (only first one is considered) */ @@ -30,7 +41,7 @@ ); $keyword = strtolower($keyword); - /* Based on given keyword, choose a search engine */ + /* Based on given keyword, choose a search engine or the fallback */ if (array_search($keyword, array_keys($searchkeys)) !== false) { $search = $searchkeys[$keyword]; } else { @@ -130,15 +141,33 @@ <!-- Search Engine Lists --> <h3>Search Engines</h3> <p> - Search Providers: <?php echo count($searches); ?> - <br> - Default Search: - <a href="<?php echo $searches[$default_search]["website"]; ?>" - target="_blank"> + Choose your default search: + <select + name="default_search" + id="default_search" + onchange="select_default_searchengine()" + > <?php - echo $searches[$default_search]["name"]; + /* Cycle through all available search engines + * Create <option> objects for each one + * Mark the default search engine as "selected" + */ + foreach (array_keys($searches) as $search) { ?> - </a> + <option + value="<?php echo $search; ?>" + <?php + if ($search == $default_search) { + echo "selected"; + } + ?> + > + <?php echo $searches[$search]["name"]; ?> + </option> + <?php + } + ?> + </select> </p> <!-- Toggle-Button --> @@ -203,6 +232,22 @@ </body> + <!-- Script for selecting default Search Engine --> + <script> + select_default_searchengine = function() { + // Fetch the default search selection + var selections = document.querySelector('#default_search'); + var selection = selections.selectedOptions[0].value; + // Set the expiration time for the cookie (1 year) + const d = new Date(); + d.setTime(d.getTime() + (365*24*60*60*1000)); + let expires = "expires="+ d.toUTCString(); + // Set a cookie for the selection + document.cookie = "default_search=" + selection + ";" + + expires + ";SameSite=Strict;"; + } + </script> + <!-- Script for toggling Search Engine tables --> <script> toggle_details = function() {