import { bangs } from "./bang"; import "./global.css"; function noSearchDefaultPageRender() { const app = document.querySelector("#app")!; app.innerHTML = `

💣 Bang!

Add the following URL as a custom search engine to your browser in order to enable all of DuckDuckGo's bangs right from your browser's search bar!

How to add Bang! Search Engine

Below are links to the search engine settings in your browser.
Copy the URL for the browser engine you are using & paste it into a new tab.
From there you should see an option to add a search engine. Copy the link above & fill out the form as follows.

Name: Bang!
Engine URL: ${import.meta.env.VITE_BANG_URL}?q=%s
Alias: @bang
`; const copyButton = app.querySelector(".copy-button")!; const copyIcon = copyButton.querySelector("img")!; const urlInput = app.querySelector(".url-input")!; const copyFirefox = app.querySelector(".copy-firefox")!; const copyFirefoxIcon = copyFirefox.querySelector("img")!; const firefoxInput = app.querySelector(".firefox-textbox")!; const copyChrome = app.querySelector(".copy-chrome")!; const copyChromeIcon = copyChrome.querySelector("img")!; const chromeInput = app.querySelector(".chrome-textbox")!; copyButton.addEventListener("click", async () => { await navigator.clipboard.writeText(urlInput.value); copyIcon.src = "/clipboard-check.svg"; setTimeout(() => { copyIcon.src = "/clipboard.svg"; }, 2000); }); copyFirefox.addEventListener("click", async () => { await navigator.clipboard.writeText(firefoxInput.value); copyFirefoxIcon.src = "/clipboard-check.svg"; setTimeout(() => { copyFirefoxIcon.src = "/clipboard.svg"; }, 2000); }); copyChrome.addEventListener("click", async () => { await navigator.clipboard.writeText(chromeInput.value); copyChromeIcon.src = "/clipboard-check.svg"; setTimeout(() => { copyChromeIcon.src = "/clipboard.svg"; }, 2000); }); } const envDefaultBang = import.meta.env.VITE_DEFAULT_BANG ?? "g"; const LS_DEFAULT_BANG = localStorage.getItem("default-bang") ?? envDefaultBang; 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 match = query.match(/!(\S+)/i); 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(); // 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; } function doRedirect() { const searchUrl = getBangredirectUrl(); if (!searchUrl) return; window.location.replace(searchUrl); } doRedirect();