forked from T3-Content/unduck
-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathindex.html
More file actions
340 lines (320 loc) · 10.8 KB
/
Copy pathindex.html
File metadata and controls
340 lines (320 loc) · 10.8 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<script>
// First-load bang redirect — the cold path, before the service worker
// controls the page. This runs first, ahead of every landing-page asset,
// so the catalog fetch starts immediately with nothing competing for the
// connection. On a real bang query we fetch bangs.bin + the fallback
// resolver and redirect, and we deliberately do NOT load the landing UI
// (main.js and the sounds it pulls) — that only matters on the settings
// page. The build hands us the bundle URLs in window.__APP__; loadApp()
// brings the UI up only when we are not redirecting.
(function () {
var url = new URL(window.location.href);
var query = url.searchParams.get("q");
var trimmed = query ? query.trim() : "";
var hasController =
navigator.serviceWorker && navigator.serviceWorker.controller;
// A real bang to resolve right now, with no worker to do it for us.
var isRedirect =
trimmed !== "" &&
trimmed !== "!" &&
trimmed !== "!settings" &&
!hasController;
// Bring up the landing/settings UI. The build strips the module tag
// and hands its URL(s) here; in dev the original tag loads it instead
// (window.__APP__ is undefined there, so this is a no-op).
function loadApp() {
var app = window.__APP__;
if (!app || !app.js) return;
app.js.forEach(function (src) {
var s = document.createElement("script");
s.type = "module";
s.src = src;
document.head.appendChild(s);
});
}
function whenReady(fn) {
if (document.readyState === "loading")
document.addEventListener("DOMContentLoaded", fn);
else fn();
}
// Tell the edge redirect (functions/index.ts) whether this profile
// has settings it cannot see. src/libs.ts raises this cookie when
// settings are stored, but nothing raises it for a profile that was
// configured before the cookie existed, and the settings page may
// never load again. So every page load reconciles it. Never on the
// critical path: callers run this after the catalog fetch is away.
function syncSettingsCookie() {
try {
var custom = localStorage.getItem("custom-bangs");
var configured =
!!localStorage.getItem("default-bang") ||
!!(custom && custom !== "{}");
var secure = location.protocol === "https:" ? "; Secure" : "";
document.cookie = configured
? "unduck-settings=1; Path=/; Max-Age=31536000; SameSite=Lax" + secure
: "unduck-settings=; Path=/; Max-Age=0; SameSite=Lax" + secure;
} catch (e) {}
}
// Give the worker the bang data we just downloaded.
function seedServiceWorker(buffer) {
if (!("serviceWorker" in navigator)) return;
navigator.serviceWorker.ready
.then(function (reg) {
var target = reg.active || navigator.serviceWorker.controller;
if (!target) return;
target.postMessage({ type: "SEED_BANG_DATA", buffer: buffer.slice(0) });
})
.catch(function () {});
}
// Install the worker on every path, including a bare redirect (which
// no longer loads main.js). Without this a search-only user — one who
// never opens the settings page — would never install the worker or
// migrate their data. register() is idempotent, so main.js registering
// again on the landing page is harmless.
if ("serviceWorker" in navigator) {
navigator.serviceWorker
.register("/sw.js", { updateViaCache: "none" })
.catch(function () {});
}
// Hand the worker the settings the old page-based version kept in
// localStorage and fold in its legacy search count (worker-side
// idempotent). The landing page does this from main.ts; this covers the
// redirect path, where main.js never loads. Best-effort: on a brand-new
// install the worker may not be active before we navigate away, but the
// cold fallback reads localStorage directly and this completes on a
// later load.
function seedWorker() {
if (!("serviceWorker" in navigator)) return;
navigator.serviceWorker.ready
.then(function (reg) {
var sw = reg.active;
if (!sw) return;
var def = localStorage.getItem("default-bang");
if (def) sw.postMessage({ type: "SET_DEFAULT_BANG", trigger: def });
try {
var cb = JSON.parse(localStorage.getItem("custom-bangs") || "{}");
if (cb && Object.keys(cb).length)
sw.postMessage({ type: "UPDATE_CUSTOM_BANGS", bangs: cb });
} catch (e) {}
var raw = localStorage.getItem("search-count");
if (raw === null) return;
var n = parseInt(raw, 10);
if (!(n > 0)) {
localStorage.removeItem("search-count");
return;
}
var ch = new MessageChannel();
ch.port1.onmessage = function () {
localStorage.removeItem("search-count");
};
sw.postMessage({ type: "MIGRATE_SEARCH_COUNT", count: n }, [
ch.port2,
]);
})
.catch(function () {});
}
// Ask the worker whether it already holds bang data, so warming
// never re-downloads something that is already cached.
function workerNeedsData() {
if (!("serviceWorker" in navigator)) return Promise.resolve(false);
return navigator.serviceWorker.ready
.then(function (reg) {
var target = reg.active || navigator.serviceWorker.controller;
if (!target) return false;
return new Promise(function (resolve) {
var channel = new MessageChannel();
var settled = false;
channel.port1.onmessage = function (event) {
settled = true;
resolve(!event.data || !event.data.ready);
};
target.postMessage({ type: "HAS_BANG_DATA" }, [channel.port2]);
setTimeout(function () {
if (!settled) resolve(false);
}, 1000);
});
})
.catch(function () {
return false;
});
}
if (!isRedirect) {
// Landing, settings, or the worker is already in charge: show the UI.
syncSettingsCookie();
whenReady(loadApp);
// On a bare landing visit, warm the worker so the next search is instant.
if (trimmed === "" && "serviceWorker" in navigator) {
window.addEventListener("load", function () {
workerNeedsData().then(function (needed) {
if (!needed) return;
fetch("/bangs.bin")
.then(function (r) { return r.arrayBuffer(); })
.then(seedServiceWorker)
.catch(function () {});
});
});
}
return;
}
// Fast redirect path: no landing bundle, and the catalog and the
// fallback resolver load *in parallel* — the resolver is ~1 KiB and
// finishes long before the catalog, so its round-trip is fully hidden
// behind the catalog download instead of running after it.
var dataPromise = fetch("/bangs.bin").then(function (r) {
return r.arrayBuffer();
});
var readyPromise = new Promise(function (resolve) {
window.addEventListener("fallbackReady", resolve, { once: true });
});
var fb = document.createElement("script");
fb.type = "module";
fb.src = "/fallback.js";
document.head.appendChild(fb);
// Migrate settings + search count to the worker on the redirect path;
// main.js (which does this on the landing page) never loads here.
// The catalog request is already in flight, so this costs nothing.
syncSettingsCookie();
seedWorker();
Promise.all([dataPromise, readyPromise])
.then(function (parts) {
var data = parts[0];
return window.resolveFallback(query, data).then(function (dest) {
seedServiceWorker(data);
return dest;
});
})
.then(function (dest) {
if (dest) window.location.replace(dest);
else whenReady(loadApp);
})
.catch(function (err) {
console.error("Fallback redirect failed:", err);
whenReady(loadApp); // don't strand the user on a blank page
});
})();
</script>
<link rel="icon" type="image/gif" href="/goose.gif" />
<link
rel="search"
type="application/opensearchdescription+xml"
title="Unduck"
href="/opensearch.xml"
/>
<link rel="manifest" href="/manifest.json" />
<style>
/* Font fallback that closely matches Inter metrics */
@font-face {
font-family: "Inter Fallback";
size-adjust: 107%;
ascent-override: 90%;
src: local("Arial");
font-display: swap;
}
:root {
font-family:
Inter,
"Inter Fallback",
system-ui,
-apple-system,
BlinkMacSystemFont,
"Segoe UI",
Roboto,
Oxygen,
Ubuntu,
Cantarell,
"Open Sans",
"Helvetica Neue",
sans-serif;
font-synthesis: none;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
/* Light mode colors */
--text-color: #1a1a1a;
--text-color-secondary: #666;
--text-color-hover: #333;
--bg-color: #fff;
--bg-color-secondary: #f5f5f5;
--bg-color-hover: #f0f0f0;
--bg-color-active: #e5e5e5;
--bg-color-danger: #e9808a;
--border-color: #ddd;
}
@media (prefers-color-scheme: dark) {
:root {
--text-color: #e0e0e0;
--text-color-secondary: #999;
--text-color-hover: #fff;
--bg-color: #121212;
--bg-color-secondary: #1e1e1e;
--bg-color-hover: #2a2a2a;
--bg-color-active: #333;
--bg-color-danger: #f15f6d;
--border-color: #444;
}
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
outline: none;
}
*:focus {
outline: 2px solid var(--text-color-secondary);
}
html,
body {
height: 100%;
width: 100%;
}
body {
line-height: 1.5;
font-weight: 400;
font-size: 16px;
color: var(--text-color);
background-color: var(--bg-color);
}
#app {
min-height: 100vh;
background-color: var(--bg-color);
}
input {
margin: 2px 0px !important;
padding: 8px 12px;
border: 1px solid var(--border-color);
border-radius: 4px;
width: 100%;
background: var(--bg-color-secondary);
color: var(--text-color);
font: inherit;
}
button {
font: inherit;
border: none;
background: none;
cursor: pointer;
color: var(--text-color-secondary);
}
button:hover {
background: var(--bg-color-hover);
}
</style>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Unduck</title>
<meta
name="description"
content="A better default search engine (with bangs!)"
/>
</head>
<body>
<div id="app"></div>
<!-- Vite entry. In production the defer-app-bundle plugin strips this tag
and hands its URL to the inline loader above, so it never loads on the
redirect path. In dev it loads normally. -->
<script type="module" src="/src/main.ts"></script>
</body>
</html>