blob: f8d584aa44d9de7ea45d7b0fa383ec66bbc979aa (
plain)
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
|
import { bangs } from "./bang";
const defaultBang = bangs.find((b) => b.t === "g");
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;">
<h1>Unduck</h1>
</div>
`;
}
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(/!([a-z]+)/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(/![a-z]+\s*/i, "").trim();
// Format of the url is:
// https://www.google.com/search?q={{{s}}}
const searchUrl = selectedBang?.u.replace(
"{{{s}}}",
encodeURIComponent(cleanQuery)
);
if (!searchUrl) return null;
return searchUrl;
}
function doRedirect() {
const searchUrl = getBangredirectUrl();
if (!searchUrl) return;
window.location.replace(searchUrl);
}
doRedirect();
|