unduck/src/main.ts

102 lines
3.2 KiB
TypeScript
Raw Normal View History

2025-02-14 21:48:15 -08:00
import { bangs } from "./bang";
2025-02-14 22:25:23 -08:00
import "./global.css";
2025-02-14 21:30:33 -08:00
2025-02-14 22:18:05 -08:00
function noSearchDefaultPageRender() {
2025-03-05 23:26:00 +00:00
const currentUrl = window.location.href.replace(/\/+$/, "");
2025-02-14 22:18:05 -08:00
const app = document.querySelector<HTMLDivElement>("#app")!;
app.innerHTML = `
<div style="display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100vh;">
2025-02-14 22:25:23 -08:00
<div class="content-container">
2025-02-19 03:58:47 -08:00
<h1>Und*ck</h1>
2025-03-06 23:00:05 +00:00
<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/bangs" target="_blank">all of DuckDuckGo's bangs.</a></p>
2025-02-14 22:25:23 -08:00
<div class="url-container">
<input
type="text"
class="url-input"
2025-03-05 23:26:00 +00:00
value="${currentUrl}?q=%s"
2025-02-14 22:25:23 -08:00
readonly
/>
<button class="copy-button">
2025-02-27 13:51:59 +00:00
<p>Copy</p>
2025-02-14 22:25:23 -08:00
</button>
</div>
2025-03-06 23:00:05 +00:00
<details style="margin-top: 16px;">
<summary>Demo search</summary>
<p>Added so that some browsers treat this page as a search engine</p>
<form class="url-container">
<input
type="text"
name="q"
class="url-input"
placeholder="doom on typescript types !yt"
role="searchbox"
/>
<button type="submit" class="copy-button">
<p>Search</p>
</button>
</form>
</details>
</div>
2025-02-14 22:25:23 -08:00
</div>
2025-02-14 22:26:53 -08:00
<footer class="footer">
2025-02-27 13:29:24 +00:00
<a href="https://github.com/troylusty/unduck" target="_blank">github</a>
2025-02-14 22:57:50 -08:00
2025-02-27 13:29:24 +00:00
forked from
<a href="https://github.com/t3dotgg/unduck" target="_blank">t3dotgg/unduck</a>
2025-02-14 22:26:53 -08:00
</footer>
2025-02-14 22:18:05 -08:00
</div>
`;
2025-02-14 22:25:23 -08:00
const copyButton = app.querySelector<HTMLButtonElement>(".copy-button")!;
2025-02-27 13:51:59 +00:00
const copyIcon = copyButton.querySelector("p")!;
2025-02-14 22:25:23 -08:00
const urlInput = app.querySelector<HTMLInputElement>(".url-input")!;
copyButton.addEventListener("click", async () => {
await navigator.clipboard.writeText(urlInput.value);
2025-02-27 13:51:59 +00:00
copyIcon.textContent = "Copied";
2025-02-14 22:25:23 -08:00
setTimeout(() => {
2025-02-27 13:51:59 +00:00
copyIcon.textContent = "Copy";
2025-02-14 22:25:23 -08:00
}, 2000);
});
2025-02-14 22:18:05 -08:00
}
2025-02-27 13:03:08 +00:00
const LS_DEFAULT_BANG = localStorage.getItem("default-bang") ?? "ddg";
2025-02-14 23:33:45 -08:00
const defaultBang = bangs.find((b) => b.t === LS_DEFAULT_BANG);
2025-02-14 22:03:20 -08:00
function getBangredirectUrl() {
2025-02-14 21:48:15 -08:00
const url = new URL(window.location.href);
const query = url.searchParams.get("q")?.trim() ?? "";
2025-02-14 22:18:05 -08:00
if (!query) {
noSearchDefaultPageRender();
return null;
}
2025-02-14 22:03:20 -08:00
const match = query.match(/!(\S+)/i);
2025-02-14 22:03:20 -08:00
2025-02-14 22:16:04 -08:00
const bangCandidate = match?.[1]?.toLowerCase();
2025-02-14 21:48:15 -08:00
const selectedBang = bangs.find((b) => b.t === bangCandidate) ?? defaultBang;
2025-02-14 22:03:20 -08:00
// Remove the first bang from the query
const cleanQuery = query.replace(/!\S+\s*/i, "").trim();
2025-02-14 21:48:15 -08:00
2025-02-14 22:03:20 -08:00
// Format of the url is:
// https://www.google.com/search?q={{{s}}}
const searchUrl = selectedBang?.u.replace(
"{{{s}}}",
2025-02-14 23:26:36 -08:00
// Replace %2F with / to fix formats like "!ghr+t3dotgg/unduck"
2025-03-06 23:00:05 +00:00
encodeURIComponent(cleanQuery).replace(/%2F/g, "/"),
2025-02-14 22:03:20 -08:00
);
if (!searchUrl) return null;
2025-02-14 21:48:15 -08:00
2025-02-14 22:03:20 -08:00
return searchUrl;
2025-02-14 21:48:15 -08:00
}
2025-02-14 22:18:05 -08:00
function doRedirect() {
const searchUrl = getBangredirectUrl();
if (!searchUrl) return;
window.location.replace(searchUrl);
}
2025-02-14 21:30:33 -08:00
2025-02-14 22:18:05 -08:00
doRedirect();