commit 08996e9dc96b7e56c3ca95a38b6a603251356955
parent a8553c213a57dd62656f38b9385c14ce318c06ea
Author: JayVii <jayvii[AT]posteo[DOT]de>
Date: Fri, 18 Oct 2024 10:25:25 +0200
feat: move javascript to external script file
Diffstat:
A | assets/js/filter_engines.js | | | 78 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
M | index.php | | | 66 | +----------------------------------------------------------------- |
2 files changed, 79 insertions(+), 65 deletions(-)
diff --git a/assets/js/filter_engines.js b/assets/js/filter_engines.js
@@ -0,0 +1,78 @@
+/* Script for selecting default Search Engine ------------------------------- */
+
+make_default = function(selection) {
+
+ // Set a cookie for the selection
+ document.cookie = "default_search=" + selection + ";" +
+ "Max-Age=31536000;" + // 60 x 60 x 24 x 365 = 1 year
+ "Domain=" + document.domain + ";" +
+ "SameSite=Strict;";
+
+ // Set value in searchbar
+ document.getElementById("default_search").value = selection;
+
+ // Gather Entries of Search Engines list
+ var entries = document.querySelectorAll("article");
+
+ // Remove pre-existing "Default" Markers
+ document.querySelectorAll("#default_category").forEach(
+ function(obj){ obj.remove(); }
+ );
+
+ // Create "Default" Marker
+ let marker = document.createElement("mark");
+ marker.id = "default_category";
+ marker.innerText = "#Default";
+
+ // Update Search Engines List
+ for (let i = 0; i < entries.length; i++) {
+ if (entries[i].id == selection) {
+ entries[i].classList.add("selected");
+ entries[i].querySelector(".categories").prepend(marker);
+ } else {
+ entries[i].classList.remove("selected");
+ }
+ }
+
+}
+
+/* Script to search through the list of search engines ---------------------- */
+
+search_engines = function(bar) {
+
+ // Get text from search bar
+ var pat = document.getElementById(bar).value.toLowerCase();
+
+ // Gather article objects that contain the search engines
+ var entries = document.querySelectorAll("article");
+
+ // Compare text in article objects with text in search bar
+ // Mark those hidden that do not match
+ for (let i = 0; i < entries.length; i++) {
+ if (entries[i].innerText.toLowerCase().match(pat) === null) {
+ entries[i].classList.add("hidden");
+ } else {
+ entries[i].classList.remove("hidden");
+ }
+ }
+
+}
+
+/* Script to search through search engines via category buttons ------------- */
+
+search_for_category = function(category) {
+
+ // Insert category name into category-searchbar
+ document.getElementById("engines_searchbar").value = "#" + category;
+
+ // collapse category summary
+ document.getElementById("categories").open = false;
+
+ // filter through search engines list
+ search_engines("engines_searchbar");
+
+ // Scroll back to search bar
+ setTimeout(function() { document.location = "#engines"; }, 10);
+
+}
+
diff --git a/index.php b/index.php
@@ -97,6 +97,7 @@
href="/opensearch.xml"
title="serĉi - search with !keywords"
>
+ <script async src=/assets/js/filter_engines.js></script>
<title>serĉi - search with !keywords</title>
</head>
@@ -405,69 +406,4 @@
</body>
- <!-- Script for selecting default Search Engine -->
- <script>
- make_default = function(selection) {
- // Set a cookie for the selection
- document.cookie = "default_search=" + selection + ";" +
- "Max-Age=31536000;" + // 60 x 60 x 24 x 365 = 1 year
- "Domain=" + document.domain + ";" +
- "SameSite=Strict;";
- // Set value in searchbar
- document.getElementById("default_search").value = selection;
- // Gather Entries of Search Engines list
- var entries = document.querySelectorAll("article")
- // Remove pre-existing "Default" Markers
- document.querySelectorAll("#default_category").forEach(
- function(obj){ obj.remove(); }
- );
- // Create "Default" Marker
- let marker = document.createElement("mark");
- marker.id = "default_category";
- marker.innerText = "#Default";
- // Update Search Engines List
- for (let i = 0; i < entries.length; i++) {
- if (entries[i].id == selection) {
- entries[i].classList.add("selected");
- entries[i].querySelector(".categories").prepend(marker);
- } else {
- entries[i].classList.remove("selected");
- }
- }
- }
- </script>
-
- <!-- Script to search through the list of search engines -->
- <script>
- search_engines = function(bar) {
- // Get text from search bar
- var pat = document.getElementById(bar).value.toLowerCase();
- // Compare text in article objects with text in search bar
- // Mark those hidden that do not match
- var entries = document.querySelectorAll("article");
- for (let i = 0; i < entries.length; i++) {
- if (entries[i].innerText.toLowerCase().match(pat) === null) {
- entries[i].classList.add("hidden");
- } else {
- entries[i].classList.remove("hidden");
- }
- }
- }
- </script>
-
- <!-- Script to search through search engines via category buttons -->
- <script>
- search_for_category = function(category) {
- // Insert category name into category-searchbar
- document.getElementById("engines_searchbar").value = "#" + category;
- // collapse category summary
- document.getElementById("categories").open = false;
- // filter through search engines list
- search_engines("engines_searchbar");
- // Scroll back to search bar
- setTimeout(function() { document.location = "#engines"; }, 10);
- }
- </script>
-
</html>
-