A fast, local-first "redirection engine" for !bang users with a few extra features ^-^
1import { bangs } from "./bang"; 2 3function noSearchDefaultPageRender() { 4 const app = document.querySelector<HTMLDivElement>("#app"); 5 if (!app) throw new Error("App element not found"); 6 app.innerHTML = ` 7 <div style="display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100vh;"> 8 <div class="content-container"> 9 <h1>┐( ˘_˘ )┌</h1> 10 <p>DuckDuckGo's bang redirects are too slow. Add the following URL as a custom search engine to your browser. Enables <a href="https://duckduckgo.com/bang.html" target="_blank">all of DuckDuckGo's bangs.</a></p> 11 <div class="url-container"> 12 <input 13 type="text" 14 class="url-input" 15 value="https://unduck.link?q=%s" 16 readonly 17 /> 18 <button class="copy-button"> 19 <img src="/clipboard.svg" alt="Copy" /> 20 </button> 21 </div> 22 </div> 23 <footer class="footer"> 24 made with ♥ by <a href="https://github.com/taciturnaxolotl" target="_blank">Kieran Klukas</a> as <a href="https://github.com/taciturnaxolotl/unduck" target="_blank">open source</a> software 25 </footer> 26 </div> 27 `; 28 29 const copyButton = app.querySelector<HTMLButtonElement>(".copy-button"); 30 if (!copyButton) throw new Error("Copy button not found"); 31 const copyIcon = copyButton.querySelector("img"); 32 if (!copyIcon) throw new Error("Copy icon not found"); 33 const urlInput = app.querySelector<HTMLInputElement>(".url-input"); 34 if (!urlInput) throw new Error("URL input not found"); 35 36 urlInput.value = `${window.location.protocol}//${window.location.host}?q=%s`; 37 38 copyButton.addEventListener("click", async () => { 39 await navigator.clipboard.writeText(urlInput.value); 40 copyIcon.src = "/clipboard-check.svg"; 41 42 setTimeout(() => { 43 copyIcon.src = "/clipboard.svg"; 44 }, 2000); 45 }); 46} 47 48const LS_DEFAULT_BANG = localStorage.getItem("default-bang") ?? "g"; 49const defaultBang = bangs.find((b) => b.t === LS_DEFAULT_BANG); 50 51function getBangredirectUrl() { 52 const url = new URL(window.location.href); 53 const query = url.searchParams.get("q")?.trim() ?? ""; 54 if (!query) { 55 noSearchDefaultPageRender(); 56 return null; 57 } 58 59 const match = query.match(/!(\S+)/i); 60 61 const bangCandidate = match?.[1]?.toLowerCase(); 62 const selectedBang = bangs.find((b) => b.t === bangCandidate) ?? defaultBang; 63 64 // Remove the first bang from the query 65 const cleanQuery = query.replace(/!\S+\s*/i, "").trim(); 66 67 // Format of the url is: 68 // https://www.google.com/search?q={{{s}}} 69 const searchUrl = selectedBang?.u.replace( 70 "{{{s}}}", 71 // Replace %2F with / to fix formats like "!ghr+t3dotgg/unduck" 72 encodeURIComponent(cleanQuery).replace(/%2F/g, "/"), 73 ); 74 if (!searchUrl) return null; 75 76 return searchUrl; 77} 78 79function doRedirect() { 80 const searchUrl = getBangredirectUrl(); 81 if (!searchUrl) return; 82 window.location.replace(searchUrl); 83} 84 85doRedirect();