diff --git a/src/main.ts b/src/main.ts index 13ebe48..ee4d274 100644 --- a/src/main.ts +++ b/src/main.ts @@ -2,8 +2,8 @@ import { bangs } from "./bang"; import "./global.css"; function noSearchDefaultPageRender() { - const app = document.querySelector("#app")!; - app.innerHTML = ` + const app = document.querySelector("#app")!; + app.innerHTML = `

Und*ck

@@ -12,7 +12,7 @@ function noSearchDefaultPageRender() {
`; - const copyButton = app.querySelector(".copy-button")!; - const copyIcon = copyButton.querySelector("img")!; - const urlInput = app.querySelector(".url-input")!; + const copyButton = app.querySelector(".copy-button")!; + const copyIcon = copyButton.querySelector("img")!; + const urlInput = app.querySelector(".url-input")!; - copyButton.addEventListener("click", async () => { - await navigator.clipboard.writeText(urlInput.value); - copyIcon.src = "/clipboard-check.svg"; + copyButton.addEventListener("click", async () => { + await navigator.clipboard.writeText(urlInput.value); + copyIcon.src = "/clipboard-check.svg"; - setTimeout(() => { - copyIcon.src = "/clipboard.svg"; - }, 2000); - }); + setTimeout(() => { + copyIcon.src = "/clipboard.svg"; + }, 2000); + }); } const LS_DEFAULT_BANG = localStorage.getItem("default-bang") ?? "g"; const defaultBang = bangs.find((b) => b.t === LS_DEFAULT_BANG); function getBangredirectUrl() { - const url = new URL(window.location.href); - const query = url.searchParams.get("q")?.trim() ?? ""; - if (!query) { - noSearchDefaultPageRender(); - return null; - } + const url = new URL(window.location.href); + const query = url.searchParams.get("q")?.trim() ?? ""; + if (!query) { + noSearchDefaultPageRender(); + return null; + } - const match = query.match(/!(\S+)/i); + const match = query.match(/!(\S+)/i); - const bangCandidate = match?.[1]?.toLowerCase(); - const selectedBang = bangs.find((b) => b.t === bangCandidate) ?? defaultBang; + const bangCandidate = match?.[1]?.toLowerCase(); + const selectedBang = bangs.find((b) => b.t === bangCandidate) ?? defaultBang; - // Remove the first bang from the query - const cleanQuery = query.replace(/!\S+\s*/i, "").trim(); + // Remove the first bang from the query + const cleanQuery = query.replace(/!\S+\s*/i, "").trim(); - // Format of the url is: - // https://www.google.com/search?q={{{s}}} - const searchUrl = selectedBang?.u.replace( - "{{{s}}}", - // Replace %2F with / to fix formats like "!ghr+t3dotgg/unduck" - encodeURIComponent(cleanQuery).replace(/%2F/g, "/") - ); - if (!searchUrl) return null; + // Format of the url is: + // https://www.google.com/search?q={{{s}}} + const searchUrl = selectedBang?.u.replace( + "{{{s}}}", + // Replace %2F with / to fix formats like "!ghr+t3dotgg/unduck" + encodeURIComponent(cleanQuery).replace(/%2F/g, "/") + ); + if (!searchUrl) return null; - return searchUrl; + return searchUrl; } function doRedirect() { - const searchUrl = getBangredirectUrl(); - if (!searchUrl) return; - window.location.replace(searchUrl); + const searchUrl = getBangredirectUrl(); + if (!searchUrl) return; + window.location.replace(searchUrl); } doRedirect();