-
Notifications
You must be signed in to change notification settings - Fork 349
Expand file tree
/
Copy pathmain.ts
More file actions
97 lines (84 loc) · 3.22 KB
/
main.ts
File metadata and controls
97 lines (84 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { bangs } from "./bang";
import "./global.css";
function noSearchDefaultPageRender() {
const app = document.querySelector<HTMLDivElement>("#app")!;
app.innerHTML = `
<div style="display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100vh;">
<div class="content-container">
<h1>Und*ck</h1>
<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>
<div class="url-container">
<input
type="text"
class="url-input"
value="https://unduck.link?q=%s"
readonly
/>
<button class="copy-button">
<img src="/clipboard.svg" alt="Copy" />
</button>
</div>
</div>
<footer class="footer">
<a href="https://t3.chat" target="_blank">t3.chat</a>
•
<a href="https://x.com/theo" target="_blank">theo</a>
•
<a href="https://github.com/t3dotgg/unduck" target="_blank">github</a>
</footer>
</div>
`;
const copyButton = app.querySelector<HTMLButtonElement>(".copy-button")!;
const copyIcon = copyButton.querySelector("img")!;
const urlInput = app.querySelector<HTMLInputElement>(".url-input")!;
copyButton.addEventListener("click", async () => {
await navigator.clipboard.writeText(urlInput.value);
copyIcon.src = "/clipboard-check.svg";
setTimeout(() => {
copyIcon.src = "/clipboard.svg";
}, 2000);
});
}
const LS_DEFAULT_BANG = localStorage.getItem("default-bang") ?? "g";
const LS_DEFAULT_T3_BANG = localStorage.getItem("default-t3-bang") ?? "g25f";
const defaultBang = bangs.find((b) => b.t === LS_DEFAULT_BANG);
const defaultT3Bang = t3Bangs.find((b) => b.t === LS_DEFAULT_T3_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+?)(?=@|\s|$)/i);
const t3Match = query.match(/@(\S+?)(?=\s|$)/i);
const bangCandidate = match?.[1]?.toLowerCase();
const selectedBang = bangs.find((b) => b.t === bangCandidate) ?? defaultBang;
let selectedUrl = selectedBang?.u;
if (selectedBang?.t === "t3") {
const t3BangCandidate = t3Match?.[1]?.toLowerCase();
const selectedT3Bang =
t3Bangs.find((b) => b.t === t3BangCandidate) ?? defaultT3Bang;
selectedUrl = selectedT3Bang?.u;
}
// Remove the first bang from the query
const cleanQuery = query.replace(/!\S+\s*/i, "").trim();
// If the query is just `!gh`, use `github.com` instead of `github.com/search?q=`
if (cleanQuery === "")
return selectedBang ? `https://${selectedBang.d}` : null;
// Format of the url is:
// https://www.google.com/search?q={{{s}}}
const searchUrl = selectedUrl?.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();