From 2f5a65cc3ea4ee8f95aee6e0dec0dec792736629 Mon Sep 17 00:00:00 2001 From: wearrrrr Date: Mon, 5 Aug 2024 23:24:57 -0500 Subject: [PATCH] Refactor how AluStore handles the search parameter, and refactor settings.ts --- src/alu.d.ts | 2 +- src/components/ProxyRegistrar.astro | 48 +++---------------- src/components/SettingsContent/ProxyTab.astro | 10 ++-- src/components/ts/AluStore.ts | 5 +- src/components/ts/settings.ts | 4 +- 5 files changed, 16 insertions(+), 53 deletions(-) diff --git a/src/alu.d.ts b/src/alu.d.ts index 95f8a95..41100c8 100644 --- a/src/alu.d.ts +++ b/src/alu.d.ts @@ -14,6 +14,6 @@ export declare global { isCustom: boolean; } >; - type ValidStoreKeys = "proxy" | "search" | "openpage" | "wisp" | "bareUrl" | "transport" | "searxng" | "theme" | "lang" | "cloak"; + type ValidStoreKeys = "proxy" | "search" | "openpage" | "wisp" | "bareUrl" | "transport" | "theme" | "lang" | "cloak"; } } diff --git a/src/components/ProxyRegistrar.astro b/src/components/ProxyRegistrar.astro index de8d9c7..61dd0a5 100644 --- a/src/components/ProxyRegistrar.astro +++ b/src/components/ProxyRegistrar.astro @@ -4,11 +4,11 @@ import { Notyf } from "notyf"; const form = document.querySelector("form"); - let input = document.querySelector("input"); + let input = document.querySelector("input")!; document.addEventListener("astro:after-swap", initForm); function initForm() { - const formEle = document.querySelector("form"); - input = document.querySelector("input"); + const formEle = document.querySelector("form") as HTMLFormElement; + input = document.querySelector("input") as HTMLInputElement; if (formEle) formEle.addEventListener("submit", formEventListener); } if (form) { @@ -24,10 +24,9 @@ }, 500); async function getProxyURL() { - const preference = getProxyPreference(); - let url = input!.value.trim(); + const preference = Alu.store.get("proxy").value; + let url = input.value.trim(); if (!isUrl(url)) url = getSearchEngine() + url; - else if (!(url.startsWith("https://") || url.startsWith("http://"))) url = "http://" + url; if (preference === "ultraviolet") { return window.__uv$config.prefix + window.__uv$config.encodeUrl(url); } else if (preference == "rammerhead") { @@ -89,7 +88,7 @@ const backwardsButton = document.getElementById("nav-backwards") as HTMLImageElement; const forwardsButton = document.getElementById("nav-forwards") as HTMLImageElement; const shareButton = document.getElementById("nav-share") as HTMLImageElement; - const preference = getProxyPreference(); + const preference = Alu.store.get("proxy").value; if (preference === "ultraviolet") { iframe.src = window.__uv$config.prefix + window.__uv$config.encodeUrl(url); } else if (preference == "rammerhead") { @@ -196,43 +195,10 @@ } function getSearchEngine() { - const searchEngine = Alu.store.get("search"); - switch (searchEngine.value.toLowerCase()) { - case "google": { - return "https://google.com/search?q="; - } - case "bing": { - return "https://bing.com/search?q=d"; - } - case "brave": { - return "https://search.brave.com/search?q="; - } - case "searx": { - const searxngURL = Alu.store.get("searxng"); - // Ugly hack to remove the trailing slash :) - if (searxngURL.value.endsWith("/")) searxngURL.value = searxngURL.value.slice(0, -1); - return searxngURL.value + "/search?q="; - } - default: { - return "https://google.com/search?q="; - } - } - } - - function getProxyPreference() { - const proxy = Alu.store.get("proxy"); - switch (proxy.value) { - case "ultraviolet": - return "ultraviolet"; - case "rammerhead": - return "rammerhead"; - default: - return "ultraviolet"; - } + return Alu.store.get("search").value; } function updateProxiedFavicon(iframe: HTMLIFrameElement) { - if (!iframe) return; const proxiedFavicon = document.getElementById("proxied-favicon") as HTMLImageElement; if (iframe) { if (iframe.contentDocument) { diff --git a/src/components/SettingsContent/ProxyTab.astro b/src/components/SettingsContent/ProxyTab.astro index b24c5bf..bc325bb 100644 --- a/src/components/SettingsContent/ProxyTab.astro +++ b/src/components/SettingsContent/ProxyTab.astro @@ -12,10 +12,10 @@ const proxyList = [ ]; const searchEngineList = [ - { name: "Google", value: "google" }, - { name: "Bing", value: "bing" }, - { name: "Brave", value: "brave" }, - { name: "Searx", value: "searx" }, + { name: "Google", value: "https://google.com/search?q=" }, + { name: "Bing", value: "https://bing.com/search?q=" }, + { name: "Brave", value: "https://search.brave.com/search?q=" }, + { name: "Searx", value: "https://searxng.site/?q=" }, ]; const openPageWith = [ @@ -64,5 +64,5 @@ const wispURLList = [
- +
diff --git a/src/components/ts/AluStore.ts b/src/components/ts/AluStore.ts index 7c9c9b1..c2b4b9c 100644 --- a/src/components/ts/AluStore.ts +++ b/src/components/ts/AluStore.ts @@ -5,7 +5,7 @@ const KEYSTORE: Alu.DefaultKeys = { }, search: { name: "Google", - value: "google", + value: "https://google.com/search?q=", }, openpage: { name: "Embed", @@ -22,9 +22,6 @@ const KEYSTORE: Alu.DefaultKeys = { name: "Epoxy", value: "/epoxy/index.mjs", }, - searxng: { - value: "https://searxng.site", - }, theme: { name: "Alu", value: "alu", diff --git a/src/components/ts/settings.ts b/src/components/ts/settings.ts index 4b850f2..78dc3ff 100644 --- a/src/components/ts/settings.ts +++ b/src/components/ts/settings.ts @@ -174,7 +174,7 @@ function setupProxySettings() { applyDropdownEventListeners(searchEngineDropdown!, checkSearxng); checkSearxng(); - applyInputListeners(searxngUrlInput, "searxng"); + applyInputListeners(searxngUrlInput, "search"); applyInputListeners(bareURLInput, "bareUrl"); } @@ -299,7 +299,7 @@ function checkSearxng() { // Callback for search engine dropdown const searchEngine = Alu.store.get("search"); const searxInput = document.getElementsByClassName("setting__searxng-url")[0] as HTMLElement; - if (searchEngine.value == "searx") { + if (searchEngine.name == "Searx") { searxInput.style.opacity = "1"; } else { searxInput.style.opacity = "0";