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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
import { getBangs, type KagiBang } from "./bangs";
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>Unk*gi</h1>
<p>Use the Kagi !bangs in any search engine. Add the following URL as a custom search engine to your browser. Enables <a href="https://help.kagi.com/kagi/features/bangs.html" target="_blank">all of Kagi's bangs</a> (also used by Helium Browser).</p>
<div class="url-container">
<input
type="text"
class="url-input"
value="https://unkagi.link?q=%s"
readonly
/>
<button class="copy-button">
<img src="/clipboard.svg" alt="Copy" />
</button>
</div>
</div>
<footer class="footer">
<a href="https://github.com/StellarRounin/unkagi" 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);
});
}
function getBangUrl(
bang: KagiBang,
query: string,
): string {
const encodedQuery = encodeURIComponent(query)
.replace(/%2F/g, "/");
return bang.u.replaceAll("{{{s}}}", encodedQuery);
}
async function main() {
const bangs = await getBangs();
const LS_DEFAULT_BANG =
localStorage.getItem("default-bang") ?? "g";
const defaultBang =
bangs.find((bang) => bang.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();
let selectedBang =
bangs.find(
(bang) =>
bang.t === bangCandidate ||
bang.ts?.includes(bangCandidate ?? ""),
);
// Si no existe el bang solicitado, usar el default
selectedBang ??= defaultBang;
// Remove the first bang from the query
const cleanQuery =
query.replace(/!\S+\s*/i, "").trim();
if (!selectedBang) {
return null;
}
// !gh → abrir github.com
if (cleanQuery === "") {
return `https://${selectedBang.d}`;
}
return getBangUrl(selectedBang, cleanQuery);
}
function doRedirect() {
const searchUrl = getBangRedirectUrl();
if (!searchUrl) {
return;
}
window.location.replace(searchUrl);
}
doRedirect();
}
main().catch((error) => {
console.error("Failed to initialize UnKagi:", error);
});
|