@capybara-hq/plugin-browser 0.1.0-rc.4 → 0.1.0-rc.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/ui/panel.js CHANGED
@@ -3,6 +3,63 @@
3
3
  // daemon/protocol.ts
4
4
  var VIEWPORT = { width: 1280, height: 800 };
5
5
  var IDLE_STOP_MS = 15 * 6e4;
6
+ var VIEWER_TEXT_MAX_CHARS = 1e4;
7
+ function clipCodePoints(text, max) {
8
+ if (text.length <= max) return text;
9
+ return Array.from(text).slice(0, max).join("");
10
+ }
11
+ var VIEWER_CURSORS = [
12
+ "default",
13
+ "pointer",
14
+ "text",
15
+ "vertical-text",
16
+ "crosshair",
17
+ "move",
18
+ "grab",
19
+ "grabbing",
20
+ "not-allowed",
21
+ "no-drop",
22
+ "wait",
23
+ "progress",
24
+ "help",
25
+ "context-menu",
26
+ "cell",
27
+ "copy",
28
+ "alias",
29
+ "zoom-in",
30
+ "zoom-out",
31
+ "col-resize",
32
+ "row-resize",
33
+ "ew-resize",
34
+ "ns-resize",
35
+ "nesw-resize",
36
+ "nwse-resize",
37
+ "n-resize",
38
+ "e-resize",
39
+ "s-resize",
40
+ "w-resize",
41
+ "ne-resize",
42
+ "nw-resize",
43
+ "se-resize",
44
+ "sw-resize",
45
+ "all-scroll",
46
+ "none"
47
+ ];
48
+ function toViewerCursor(value) {
49
+ return typeof value === "string" && VIEWER_CURSORS.includes(value) ? value : "default";
50
+ }
51
+ var FRAME_TAG = 0;
52
+ var FRAME_HEADER_BYTES = 9;
53
+ function decodeFrame(m) {
54
+ if (m.byteLength < FRAME_HEADER_BYTES || m[0] !== FRAME_TAG) return null;
55
+ const v = new DataView(m.buffer, m.byteOffset, m.byteLength);
56
+ return {
57
+ seq: v.getUint32(1),
58
+ width: v.getUint16(5),
59
+ height: v.getUint16(7),
60
+ jpeg: m.subarray(FRAME_HEADER_BYTES)
61
+ };
62
+ }
6
63
  var DEFAULT_FRAME_MAX_BYTES = 256 * 1024;
7
64
  var HANDOFF_KEEPALIVE_MS = 24 * 60 * 6e4;
8
65
 
@@ -18,11 +75,17 @@
18
75
  closeTab: "Close tab",
19
76
  takeover: "Take over",
20
77
  handback: "Hand back to the agent",
21
- takeoverHint: "You are controlling the browser. Press Esc to hand it back.",
22
- statusAgent: "The agent is using the browser",
78
+ statusUser: "You are in control",
79
+ statusUserDetail: "the agent is paused; press Esc to hand back",
80
+ statusAgent: "The agent is working",
23
81
  statusUserElsewhere: "You are controlling the browser from another view",
24
- statusLoading: "Loading…",
25
- statusConnecting: "Connecting…",
82
+ statusLoading: "loading the page",
83
+ statusConnecting: "Connecting to the browser…",
84
+ tabs: "Tabs",
85
+ more: "More",
86
+ verifyTitle: "Verification needed",
87
+ verifyBody: "The site asks for a human check. Hand back when you are done; the agent continues from here.",
88
+ verifyGo: "Verify",
26
89
  blocked: "Blocked by the network setting:",
27
90
  allow: "Allow",
28
91
  dialog: "The page shows a dialog:",
@@ -52,11 +115,17 @@
52
115
  closeTab: "关闭标签页",
53
116
  takeover: "接管",
54
117
  handback: "交还智能体",
55
- takeoverHint: "你正在控制浏览器。按 Esc 交还给智能体。",
56
- statusAgent: "智能体正在使用浏览器",
118
+ statusUser: "你正在操作",
119
+ statusUserDetail: "智能体已暂停,按 Esc 交还",
120
+ statusAgent: "智能体正在操作",
57
121
  statusUserElsewhere: "你正在另一个视图中控制浏览器",
58
- statusLoading: "加载中…",
59
- statusConnecting: "正在连接…",
122
+ statusLoading: "正在加载页面",
123
+ statusConnecting: "正在连接浏览器…",
124
+ tabs: "标签页",
125
+ more: "更多",
126
+ verifyTitle: "需要你完成验证",
127
+ verifyBody: "网站要求人工验证。完成后交还,智能体从这里继续。",
128
+ verifyGo: "去验证",
60
129
  blocked: "被网络设置拦截:",
61
130
  allow: "允许",
62
131
  dialog: "页面弹出了对话框:",
@@ -81,15 +150,442 @@
81
150
  return STRINGS[locale][key];
82
151
  }
83
152
 
153
+ // ui-src/panel/input.ts
154
+ var MOVE_EVERY_MS = 33;
155
+ var PRESS_MAX_MS = 1e3;
156
+ function mountInput(opts) {
157
+ const win = opts.window;
158
+ const doc = win.document;
159
+ const { view, stage, send, interactive } = opts;
160
+ const sink = doc.createElement("textarea");
161
+ sink.id = "input-sink";
162
+ sink.setAttribute("aria-label", opts.label);
163
+ sink.setAttribute("autocomplete", "off");
164
+ sink.setAttribute("autocapitalize", "off");
165
+ sink.setAttribute("autocorrect", "off");
166
+ sink.spellcheck = false;
167
+ sink.tabIndex = -1;
168
+ Object.assign(sink.style, {
169
+ position: "absolute",
170
+ left: "0px",
171
+ top: "0px",
172
+ width: "1px",
173
+ height: "1px",
174
+ padding: "0",
175
+ border: "0",
176
+ margin: "0",
177
+ opacity: "0",
178
+ resize: "none",
179
+ overflow: "hidden",
180
+ pointerEvents: "none",
181
+ whiteSpace: "pre"
182
+ });
183
+ stage.appendChild(sink);
184
+ const press = doc.createElement("div");
185
+ press.id = "press";
186
+ press.hidden = true;
187
+ press.setAttribute("aria-hidden", "true");
188
+ Object.assign(press.style, {
189
+ position: "absolute",
190
+ width: "18px",
191
+ height: "18px",
192
+ margin: "-9px 0 0 -9px",
193
+ borderRadius: "50%",
194
+ border: "2px solid var(--cb-accent, Highlight)",
195
+ pointerEvents: "none"
196
+ });
197
+ stage.appendChild(press);
198
+ let pressTimer;
199
+ let pressReleased = false;
200
+ let cursor = "default";
201
+ let focusWanted = false;
202
+ let held = null;
203
+ const downKeys = /* @__PURE__ */ new Map();
204
+ let lastPointer = { x: 0, y: 0 };
205
+ let moveTimer;
206
+ let nextMove = null;
207
+ const inStage = (clientX, clientY) => {
208
+ const r = stage.getBoundingClientRect();
209
+ return { left: clientX - r.left, top: clientY - r.top };
210
+ };
211
+ function placeSink(clientX, clientY) {
212
+ const p = inStage(clientX, clientY);
213
+ sink.style.left = `${Math.round(p.left)}px`;
214
+ sink.style.top = `${Math.round(p.top)}px`;
215
+ }
216
+ function showPress(clientX, clientY) {
217
+ const p = inStage(clientX, clientY);
218
+ press.style.left = `${Math.round(p.left)}px`;
219
+ press.style.top = `${Math.round(p.top)}px`;
220
+ press.setAttribute("data-state", "down");
221
+ press.hidden = false;
222
+ pressReleased = false;
223
+ if (pressTimer !== void 0) clearTimeout(pressTimer);
224
+ pressTimer = setTimeout(hidePress, PRESS_MAX_MS);
225
+ }
226
+ function hidePress() {
227
+ if (pressTimer !== void 0) clearTimeout(pressTimer);
228
+ pressTimer = void 0;
229
+ press.hidden = true;
230
+ press.removeAttribute("data-state");
231
+ pressReleased = false;
232
+ }
233
+ function applyCursor() {
234
+ view.style.cursor = interactive() ? cursor : "default";
235
+ }
236
+ function queueMove(p) {
237
+ nextMove = p;
238
+ if (moveTimer !== void 0) return;
239
+ flushMove();
240
+ }
241
+ function flushMove() {
242
+ const p = nextMove;
243
+ nextMove = null;
244
+ if (p === null) {
245
+ moveTimer = void 0;
246
+ return;
247
+ }
248
+ if (interactive()) send({ t: "mouse", kind: "move", ...p });
249
+ moveTimer = setTimeout(flushMove, MOVE_EVERY_MS);
250
+ }
251
+ function dropMove() {
252
+ nextMove = null;
253
+ }
254
+ view.addEventListener("mousedown", (e) => {
255
+ if (!interactive()) return;
256
+ e.preventDefault();
257
+ const button = e.button === 2 ? "right" : "left";
258
+ held = button;
259
+ dropMove();
260
+ placeSink(e.clientX, e.clientY);
261
+ sink.focus();
262
+ showPress(e.clientX, e.clientY);
263
+ lastPointer = opts.toPage(e.clientX, e.clientY);
264
+ send({ t: "mouse", kind: "down", ...lastPointer, button });
265
+ });
266
+ const release = (e) => {
267
+ if (held === null) return;
268
+ const button = held;
269
+ held = null;
270
+ if (!interactive()) return;
271
+ dropMove();
272
+ press.setAttribute("data-state", "up");
273
+ pressReleased = true;
274
+ send({ t: "mouse", kind: "up", ...opts.toPage(e.clientX, e.clientY), button });
275
+ };
276
+ win.addEventListener("mouseup", release);
277
+ const moved = (e) => {
278
+ if (!interactive()) return;
279
+ lastPointer = opts.toPage(e.clientX, e.clientY);
280
+ queueMove(lastPointer);
281
+ };
282
+ view.addEventListener("mousemove", moved);
283
+ win.addEventListener("mousemove", (e) => {
284
+ if (held !== null && e.target !== view) moved(e);
285
+ });
286
+ view.addEventListener(
287
+ "wheel",
288
+ (e) => {
289
+ if (!interactive()) return;
290
+ e.preventDefault();
291
+ dropMove();
292
+ send({
293
+ t: "mouse",
294
+ kind: "wheel",
295
+ ...opts.toPage(e.clientX, e.clientY),
296
+ dx: e.deltaX,
297
+ dy: e.deltaY
298
+ });
299
+ },
300
+ { passive: false }
301
+ );
302
+ view.addEventListener("contextmenu", (e) => e.preventDefault());
303
+ const composing = (e) => e.isComposing || e.key === "Process" || e.keyCode === 229;
304
+ const isPasteShortcut = (e) => (e.metaKey || e.ctrlKey) && !e.altKey && e.key.toLowerCase() === "v";
305
+ function sendText(text) {
306
+ if (text === "") return;
307
+ const t2 = clipCodePoints(text, VIEWER_TEXT_MAX_CHARS);
308
+ send({ t: "key", kind: "char", key: t2, text: t2 });
309
+ }
310
+ sink.addEventListener("keydown", (e) => {
311
+ if (!interactive() || e.key === "Escape" || composing(e) || isPasteShortcut(e)) return;
312
+ e.preventDefault();
313
+ downKeys.set(e.key, e.code);
314
+ send({ t: "key", kind: "down", key: e.key, code: e.code });
315
+ });
316
+ sink.addEventListener("keyup", (e) => {
317
+ if (!interactive() || e.key === "Escape" || composing(e) || isPasteShortcut(e)) return;
318
+ e.preventDefault();
319
+ downKeys.delete(e.key);
320
+ send({ t: "key", kind: "up", key: e.key, code: e.code });
321
+ });
322
+ function releaseAll() {
323
+ if (interactive()) {
324
+ for (const [key, code] of downKeys) send({ t: "key", kind: "up", key, code });
325
+ if (held !== null) send({ t: "mouse", kind: "up", ...lastPointer, button: held });
326
+ }
327
+ downKeys.clear();
328
+ held = null;
329
+ }
330
+ win.addEventListener("blur", releaseAll);
331
+ sink.addEventListener("blur", releaseAll);
332
+ doc.addEventListener("visibilitychange", () => {
333
+ if (doc.visibilityState === "hidden") releaseAll();
334
+ });
335
+ sink.addEventListener("compositionend", (e) => {
336
+ if (interactive()) sendText(e.data ?? "");
337
+ sink.value = "";
338
+ });
339
+ sink.addEventListener("input", (e) => {
340
+ const ie = e;
341
+ if (ie.isComposing || (ie.inputType ?? "").includes("Composition")) return;
342
+ if (interactive()) sendText(sink.value);
343
+ sink.value = "";
344
+ });
345
+ sink.addEventListener("paste", (e) => {
346
+ e.preventDefault();
347
+ if (!interactive()) return;
348
+ sendText(e.clipboardData?.getData("text/plain") ?? "");
349
+ });
350
+ return {
351
+ sink,
352
+ sync() {
353
+ applyCursor();
354
+ if (!interactive()) {
355
+ held = null;
356
+ downKeys.clear();
357
+ nextMove = null;
358
+ hidePress();
359
+ if (doc.activeElement === sink) sink.blur();
360
+ return;
361
+ }
362
+ if (focusWanted) {
363
+ focusWanted = false;
364
+ sink.focus();
365
+ }
366
+ },
367
+ frame() {
368
+ if (pressReleased) hidePress();
369
+ },
370
+ cursor(value) {
371
+ cursor = toViewerCursor(value);
372
+ applyCursor();
373
+ },
374
+ focus() {
375
+ focusWanted = !interactive();
376
+ sink.focus();
377
+ }
378
+ };
379
+ }
380
+
381
+ // ui-src/panel/page.ts
382
+ var ICONS = {
383
+ back: '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m15 18-6-6 6-6"/></svg>',
384
+ forward: '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m9 18 6-6-6-6"/></svg>',
385
+ reload: '<svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M21 12a9 9 0 1 1-3-6.7L21 8"/><path d="M21 3v5h-5"/></svg>',
386
+ close: '<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" aria-hidden="true"><path d="M18 6 6 18M6 6l12 12"/></svg>',
387
+ plus: '<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" aria-hidden="true"><path d="M12 5v14M5 12h14"/></svg>',
388
+ lock: '<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" aria-hidden="true"><rect x="5" y="11" width="14" height="10" rx="2"/><path d="M8 11V7a4 4 0 0 1 8 0v4"/></svg>',
389
+ more: '<svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><circle cx="5" cy="12" r="1.6"/><circle cx="12" cy="12" r="1.6"/><circle cx="19" cy="12" r="1.6"/></svg>',
390
+ spin: '<svg class="spin" width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.4" stroke-linecap="round" aria-hidden="true"><path d="M21 12a9 9 0 1 1-9-9"/></svg>',
391
+ dot: '<svg width="12" height="12" viewBox="0 0 24 24" aria-hidden="true"><circle cx="12" cy="12" r="4" fill="currentColor"/></svg>',
392
+ hand: '<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M9 11V5a2 2 0 0 1 4 0v6"/><path d="M13 10V8a2 2 0 0 1 4 0v5a7 7 0 0 1-7 7h-.5A6.5 6.5 0 0 1 5 17.5L4 15a2 2 0 0 1 3.4-2L9 15"/></svg>',
393
+ pause: '<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" aria-hidden="true"><rect x="6" y="5" width="4" height="14" rx="1"/><rect x="14" y="5" width="4" height="14" rx="1"/></svg>'
394
+ };
395
+ var bone = (style) => `<span class="bone" style="${style}"></span>`;
396
+ var PANEL_HTML = `<!doctype html>
397
+ <html lang="en">
398
+ <head>
399
+ <meta charset="utf-8">
400
+ <meta name="viewport" content="width=device-width, initial-scale=1">
401
+ <title>Browser</title>
402
+ <style>
403
+ :root {
404
+ color-scheme: light;
405
+ --ground: color-mix(in srgb, var(--cb-ink, #1A1A18) 5%, var(--cb-bg, #FAF9F6));
406
+ --frame-shadow: 0 1px 2px rgba(26,26,24,.06), 0 4px 16px rgba(26,26,24,.06);
407
+ --bone: color-mix(in srgb, var(--cb-ink, #1A1A18) 7%, transparent);
408
+ --radius: 6px;
409
+ }
410
+ :root[data-theme="dark"] {
411
+ color-scheme: dark;
412
+ --ground: var(--cb-bg, #0A0A0A);
413
+ --frame-shadow: 0 0 0 1px rgba(255,255,255,.08);
414
+ }
415
+ * { box-sizing: border-box; }
416
+ html, body { height: 100%; }
417
+ body {
418
+ margin: 0; position: relative; overflow: hidden;
419
+ background: var(--cb-panel-2, Canvas); color: var(--cb-ink, CanvasText);
420
+ font-family: var(--cb-font, ui-monospace, "SF Mono", Menlo, monospace);
421
+ font-size: var(--cb-type-body-size, 13px); line-height: var(--cb-type-body-line, 20px);
422
+ }
423
+ /* nothing before the host's tokens and language (init) */
424
+ body:not([data-ready]) { visibility: hidden; }
425
+ [hidden] { display: none !important; }
426
+ button, input { font: inherit; color: inherit; margin: 0; }
427
+ button {
428
+ display: inline-grid; place-items: center; flex: none; height: 28px; min-width: 28px;
429
+ padding: 0; border: 0; border-radius: var(--radius); background: transparent; cursor: pointer;
430
+ white-space: nowrap;
431
+ }
432
+ button:hover:not(:disabled) { background: var(--cb-hover, rgba(26,26,24,.04)); }
433
+ button:disabled { color: var(--cb-disabled, GrayText); cursor: default; }
434
+ button:focus-visible, input:focus-visible { outline: 2px solid var(--cb-focus-ring, Highlight); outline-offset: 1px; }
435
+ .primary, .secondary, .outline { padding: 0 14px; font-weight: 600; }
436
+ .primary { background: var(--cb-accent, #CC4609); color: var(--cb-on-accent, #fff); }
437
+ .primary:hover:not(:disabled) { background: var(--cb-accent-hover, var(--cb-accent, #CC4609)); }
438
+ .secondary { font-weight: 400; box-shadow: inset 0 0 0 1px var(--cb-line, #D9D8D2); }
439
+ .outline { padding: 0 12px; color: var(--cb-accent, #CC4609); box-shadow: inset 0 0 0 1px var(--cb-accent, #CC4609); }
440
+
441
+ .layer { position: absolute; inset: 0; display: flex; flex-direction: column; }
442
+ #app[data-loading] { visibility: hidden; }
443
+ .row { flex: none; display: flex; align-items: center; padding: 0 12px; }
444
+
445
+ /* tab strip */
446
+ #tabs { height: 32px; align-items: stretch; gap: 2px; overflow-x: auto; scrollbar-width: none; }
447
+ .tab { display: flex; align-items: center; gap: 6px; flex: 0 1 auto; min-width: 64px; max-width: 200px; padding: 0 10px; color: var(--cb-muted, #5E5E5A); }
448
+ .tab[data-active="true"] { color: var(--cb-ink, #1A1A18); font-weight: 500; box-shadow: inset 0 -2px 0 var(--cb-accent, #CC4609); padding-right: 6px; }
449
+ .fav { width: 14px; height: 14px; border-radius: 3px; flex: none; background: var(--cb-line-soft, #E8E7E2); }
450
+ .tab[data-active="true"] .fav { background: var(--cb-line, #D9D8D2); }
451
+ .tab .label { flex: 1; min-width: 0; display: block; height: auto; text-align: left; overflow: hidden; text-overflow: ellipsis; font-weight: inherit; }
452
+ .tab .label:hover:not(:disabled) { background: transparent; }
453
+ .tab .label:disabled { color: inherit; }
454
+ .tab .x { width: 20px; min-width: 20px; height: 20px; border-radius: 4px; color: var(--cb-muted, #5E5E5A); }
455
+ #tabs .add { align-self: center; color: var(--cb-muted, #5E5E5A); }
456
+
457
+ /* toolbar */
458
+ #bar { height: 44px; gap: 4px; }
459
+ #field { flex: 1 1 0; min-width: 0; height: 28px; margin-left: 4px; position: relative; display: flex; align-items: center; border-radius: var(--radius); background: var(--cb-hover, rgba(26,26,24,.05)); }
460
+ #field[data-edit] { background: var(--cb-panel, #fff); box-shadow: inset 0 0 0 1px var(--cb-line, #D9D8D2); }
461
+ #lock { display: inline-flex; flex: none; margin-left: 10px; color: var(--cb-muted, #5E5E5A); }
462
+ #url { flex: 1; min-width: 0; height: 100%; padding: 0 10px 0 6px; border: 0; outline: 0; background: transparent; color: transparent; caret-color: var(--cb-ink, #1A1A18); text-overflow: ellipsis; }
463
+ #lock[hidden] + #url, #lock[hidden] ~ #urltext { padding-left: 10px; }
464
+ #url:focus { color: var(--cb-ink, #1A1A18); }
465
+ #urltext { position: absolute; inset: 0 10px 0 26px; display: flex; align-items: center; pointer-events: none; white-space: nowrap; overflow: hidden; }
466
+ #lock[hidden] ~ #urltext { left: 10px; padding-left: 0; }
467
+ #urltext span:first-child { flex: none; max-width: 100%; overflow: hidden; text-overflow: ellipsis; }
468
+ #urltext span:last-child { color: var(--cb-muted, #5E5E5A); overflow: hidden; text-overflow: ellipsis; }
469
+ #url:focus ~ #urltext { visibility: hidden; }
470
+ #takeover, #handback { margin-left: 4px; }
471
+ /* the takeover slot is always reserved: gone only when hand back takes its place */
472
+ #takeover[hidden]:not([data-gone]) { display: inline-grid !important; visibility: hidden; }
473
+ #tabsbtn { flex: 1 1 0; min-width: 0; display: flex; align-items: center; gap: 6px; padding: 0 8px; background: var(--cb-hover, rgba(26,26,24,.05)); text-align: left; }
474
+ #tabsbtn .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
475
+ #tabsbtn .count { min-width: 18px; height: 18px; padding: 0 4px; border-radius: 4px; box-shadow: inset 0 0 0 1px var(--cb-line-strong, #B5B5AE); font-size: 11px; line-height: 18px; text-align: center; color: var(--cb-muted, #5E5E5A); }
476
+ #tabsbtn, #more { display: none; }
477
+ @media (max-width: 440px) {
478
+ #tabs, #forward, #reload, #field { display: none; }
479
+ #tabsbtn { display: flex; }
480
+ #more { display: inline-grid; color: var(--cb-muted, #5E5E5A); }
481
+ }
482
+
483
+ /* status line: one fixed-height line */
484
+ #status { height: 24px; gap: 6px; margin: 0; padding: 0 16px; font-size: var(--cb-type-micro-size, 11px); line-height: var(--cb-type-micro-line, 14px); color: var(--cb-muted, #5E5E5A); white-space: nowrap; overflow: hidden; }
485
+ #status .ico { display: inline-flex; flex: none; }
486
+ #status .lead { flex: none; }
487
+ #status .detail { min-width: 0; overflow: hidden; text-overflow: ellipsis; }
488
+ #status[data-tone="agent"] .ico, #status[data-tone="agent"] .lead { color: var(--cb-accent, #CC4609); }
489
+ #status[data-tone="user"] .ico, #status[data-tone="user"] .lead { color: var(--cb-ink, #1A1A18); }
490
+ .spin { animation: spin 1s linear infinite; }
491
+ @keyframes spin { to { transform: rotate(360deg); } }
492
+
493
+ /* needs the person: a warn strip in the status line's place */
494
+ .warn { flex: none; display: flex; align-items: center; gap: 10px; padding: 8px 12px 8px 16px; background: color-mix(in srgb, var(--cb-warn, #8F6500) 8%, transparent); }
495
+ .warn > .ico { display: inline-flex; flex: none; color: var(--cb-warn, #8F6500); }
496
+ .warn .text { flex: 1; min-width: 0; display: flex; flex-direction: column; }
497
+ .warn .title { color: var(--cb-warn, #8F6500); font-weight: 500; overflow-wrap: anywhere; }
498
+ .warn .body { font-family: var(--cb-font-prose, system-ui, sans-serif); color: var(--cb-muted, #5E5E5A); overflow-wrap: anywhere; }
499
+ .warn .actions { display: flex; gap: 6px; flex: none; }
500
+
501
+ /* the picture, contained and centred on a neutral ground */
502
+ #stage { flex: 1; min-height: 0; position: relative; display: flex; align-items: center; justify-content: center; padding: 12px; overflow: hidden; background: var(--ground); }
503
+ #view { display: block; flex: none; border-radius: var(--radius); background: #fff; box-shadow: var(--frame-shadow); outline: none; }
504
+ #stage[data-control="user"] #view { box-shadow: 0 0 0 2px var(--cb-accent, #CC4609), 0 4px 16px rgba(26,26,24,.08); }
505
+ #view:focus-visible { box-shadow: 0 0 0 2px var(--cb-focus-ring, Highlight), var(--frame-shadow); }
506
+ #flash { position: absolute; width: 24px; height: 24px; margin: -12px 0 0 -12px; border: 2px solid var(--cb-accent, #CC4609); border-radius: 50%; pointer-events: none; }
507
+ .center { position: absolute; inset: 0; display: flex; flex-direction: column; gap: 6px; align-items: center; justify-content: center; padding: 16px; text-align: center; background: var(--cb-panel-2, Canvas); }
508
+ .center h2 { margin: 0; font-size: inherit; font-weight: 600; }
509
+ .center p { margin: 0; font-family: var(--cb-font-prose, system-ui, sans-serif); color: var(--cb-muted, #5E5E5A); }
510
+ .center button { margin-top: 6px; }
511
+ /* short-lived notices float over the picture's bottom edge: they never move it */
512
+ #strips { position: absolute; left: 12px; right: 12px; bottom: 12px; display: flex; flex-direction: column; gap: 6px; pointer-events: none; }
513
+ .strip { display: flex; align-items: center; gap: 8px; padding: 6px 6px 6px 12px; pointer-events: auto; border-radius: var(--cb-radius-md, 8px); background: var(--cb-raised, #fff); box-shadow: var(--cb-shadow-float, 0 8px 24px rgba(26,26,24,.08)), 0 0 0 1px var(--cb-line-soft, #E8E7E2); }
514
+ .strip p { flex: 1; min-width: 0; margin: 0; overflow-wrap: anywhere; }
515
+ .strip .label { color: var(--cb-muted, #5E5E5A); }
516
+ .strip:not(:has(button)) { padding-right: 12px; }
517
+
518
+ /* menus (narrow: tabs, more) */
519
+ .menu { position: absolute; z-index: 2; min-width: 180px; max-width: calc(100% - 24px); max-height: 60%; overflow: auto; padding: 4px; border-radius: var(--cb-radius-md, 8px); background: var(--cb-raised, #fff); box-shadow: var(--cb-shadow-float, 0 8px 24px rgba(26,26,24,.08)), 0 0 0 1px var(--cb-line-soft, #E8E7E2); }
520
+ .menu button { display: flex; width: 100%; justify-content: flex-start; gap: 8px; padding: 0 8px; text-align: left; overflow: hidden; text-overflow: ellipsis; }
521
+ .menu button[aria-current="true"] { font-weight: 600; }
522
+
523
+ /* the skeleton: the panel's own shape, quietly pulsing */
524
+ .bone { display: block; flex: none; border-radius: 2px; background: var(--bone); animation: pulse 1.6s ease-in-out infinite; }
525
+ @keyframes pulse { 50% { opacity: .5; } }
526
+ #skeleton .shot { width: 100%; max-height: 100%; aspect-ratio: 16 / 10; display: flex; flex-direction: column; gap: 12px; padding: 20px; border-radius: var(--radius); background: var(--cb-panel, #fff); box-shadow: 0 1px 2px rgba(26,26,24,.06); }
527
+ :root[data-theme="dark"] #skeleton .shot { background: var(--cb-raised, #141413); box-shadow: var(--frame-shadow); }
528
+ #skeleton .narrow { display: none; }
529
+ @media (max-width: 440px) {
530
+ #skeleton .wide { display: none; }
531
+ #skeleton .narrow { display: block; }
532
+ }
533
+ @media (prefers-reduced-motion: reduce) { .bone, .spin { animation: none; } }
534
+ </style>
535
+ </head>
536
+ <body>
537
+ <div id="skeleton" class="layer" aria-busy="true">
538
+ <div class="row wide" style="height:32px;gap:8px;padding:0 22px;display:flex">${bone("width:14px;height:14px;border-radius:3px")}${bone("width:96px;height:10px")}</div>
539
+ <div class="row" style="height:44px;gap:4px">${bone("width:28px;height:28px;border-radius:6px")}<span class="wide" style="display:contents">${bone("width:28px;height:28px;border-radius:6px")}${bone("width:28px;height:28px;border-radius:6px")}</span>${bone("flex:1;height:28px;margin-left:4px;border-radius:6px")}${bone("width:56px;height:28px;margin-left:4px;border-radius:6px")}</div>
540
+ <p id="skeleton-status" class="row" role="status" style="height:24px;margin:0;padding:0 16px;font-size:11px;line-height:14px;color:var(--cb-muted,#5E5E5A)"></p>
541
+ <div style="flex:1;min-height:0;display:flex;align-items:center;justify-content:center;padding:12px;background:var(--ground)">
542
+ <div class="shot">${bone("width:40%;height:12px")}${bone("width:75%;height:10px")}${bone("width:60%;height:10px")}${bone("flex:1;border-radius:4px")}</div>
543
+ </div>
544
+ </div>
545
+ <div id="app" class="layer" data-loading>
546
+ <div id="tabs" class="row" role="tablist"></div>
547
+ <div id="bar" class="row">
548
+ <button type="button" id="back">${ICONS.back}</button>
549
+ <button type="button" id="forward">${ICONS.forward}</button>
550
+ <button type="button" id="reload">${ICONS.reload}</button>
551
+ <button type="button" id="tabsbtn" aria-haspopup="menu"><span class="fav"></span><span class="name" id="tabsbtn-name"></span><span class="count" id="tabsbtn-count"></span></button>
552
+ <label id="field"><span id="lock" role="img" hidden>${ICONS.lock}</span><input id="url" type="text" spellcheck="false" autocomplete="off"><span id="urltext"><span id="urltext-host"></span><span id="urltext-path"></span></span></label>
553
+ <button type="button" id="more" aria-haspopup="menu">${ICONS.more}</button>
554
+ <button type="button" id="takeover" class="primary" hidden></button>
555
+ <button type="button" id="handback" class="outline" hidden></button>
556
+ </div>
557
+ <p id="status" class="row" role="status"><span class="ico" id="status-icon"></span><span class="lead" id="status-lead"></span><span class="detail" id="status-detail"></span></p>
558
+ <div id="verify" class="warn" hidden><span class="ico">${ICONS.pause}</span><div class="text"><span class="title" id="verify-title"></span><span class="body" id="verify-body"></span></div><div class="actions"><button type="button" id="verify-go" class="primary"></button></div></div>
559
+ <div id="handoff" class="warn" hidden><span class="ico">${ICONS.pause}</span><div class="text"><span class="title" id="handoff-label"></span><span class="body" id="handoff-message"></span></div><div class="actions"><button type="button" id="handoff-done" class="primary"></button><button type="button" id="handoff-cancel" class="secondary"></button></div></div>
560
+ <div id="stage">
561
+ <canvas id="view" tabindex="0"></canvas>
562
+ <div id="flash" hidden></div>
563
+ <div id="empty" class="center" hidden>
564
+ <h2 id="empty-title"></h2>
565
+ <p id="empty-body"></p>
566
+ <button type="button" id="reconnect" class="secondary"></button>
567
+ </div>
568
+ <div id="strips">
569
+ <div id="blocked" class="strip" hidden><p><span id="blocked-label" class="label"></span> <span id="blocked-host"></span></p><button type="button" id="allow" class="primary"></button></div>
570
+ <div id="dialog" class="strip" hidden><p><span id="dialog-label" class="label"></span> <span id="dialog-text"></span></p></div>
571
+ <div id="notice" class="strip" role="alert" hidden><p><span id="notice-text"></span></p></div>
572
+ </div>
573
+ </div>
574
+ </div>
575
+ <div id="menu" class="menu" role="menu" hidden></div>
576
+ <script src="panel.js"><\/script>
577
+ </body>
578
+ </html>
579
+ `;
580
+
84
581
  // ui-src/panel/panel.ts
85
582
  var PROTOCOL = "capybara-embed/1";
86
583
  var ASPECT = VIEWPORT.width / VIEWPORT.height;
87
584
  var SIDE_MAX_WIDTH = 640;
88
- var MIN_HEIGHT = 120;
89
- var MAX_HEIGHT = 4e3;
585
+ var MAX_DPR = 1.25;
586
+ var STAGE_PAD = 12;
90
587
  var HIDDEN_CLOSE_MS = 6e4;
91
588
  var COMPACT_BELOW = 600;
92
- var MOVE_INTERVAL_MS = 50;
93
589
  var RESTART_RETRY_MS = [1e3, 3e3];
94
590
  var EMPTY_POLL_MS = 1e4;
95
591
  var EMPTY_POLL_FOR_MS = 30 * 6e4;
@@ -137,7 +633,6 @@
137
633
  let wasLive = false;
138
634
  let quiet = false;
139
635
  let full = false;
140
- let postedHeight = 0;
141
636
  let retryTimer;
142
637
  let emptyTimer;
143
638
  let emptySince;
@@ -152,22 +647,36 @@
152
647
  let blockedUrl = "";
153
648
  let pendingHandoff = null;
154
649
  let sentSize = "";
155
- let lastMove = Number.NEGATIVE_INFINITY;
156
650
  let image = null;
157
- const compact = () => win.innerWidth < COMPACT_BELOW;
651
+ const compact = () => {
652
+ const sw = win.screen?.width ?? 0;
653
+ return sw > 0 && sw < COMPACT_BELOW;
654
+ };
158
655
  const text = (key) => t(locale, key);
159
656
  const interactive = () => !compact() && control === "user";
160
657
  function send(msg) {
161
658
  if (sock === null) return;
162
659
  sock.send(new TextEncoder().encode(JSON.stringify(msg)));
163
660
  }
164
- function bridge(next) {
165
- full = next;
661
+ const input = mountInput({
662
+ window: win,
663
+ view,
664
+ stage,
665
+ send,
666
+ interactive: () => interactive(),
667
+ toPage: (clientX, clientY) => {
668
+ const r = view.getBoundingClientRect();
669
+ return mapPoint(clientX - r.left, clientY - r.top, { x: 0, y: 0, w: r.width, h: r.height });
670
+ },
671
+ label: t(locale, "view")
672
+ });
673
+ function askMode(next) {
166
674
  post({ protocol: PROTOCOL, type: "panel-mode", full: next });
167
- if (!next) {
168
- postedHeight = 0;
169
- fitHeight();
170
- }
675
+ }
676
+ function hostMode(next) {
677
+ if (next === full) return;
678
+ full = next;
679
+ onBoxChange();
171
680
  }
172
681
  function setLabels() {
173
682
  const set = (id, key) => {
@@ -175,6 +684,10 @@
175
684
  };
176
685
  set("takeover", "takeover");
177
686
  set("handback", "handback");
687
+ set("verify-title", "verifyTitle");
688
+ set("verify-body", "verifyBody");
689
+ set("verify-go", "verifyGo");
690
+ set("skeleton-status", "statusConnecting");
178
691
  set("allow", "allow");
179
692
  set("reconnect", "reconnect");
180
693
  set("blocked-label", "blocked");
@@ -182,16 +695,17 @@
182
695
  set("handoff-label", "handoff");
183
696
  set("handoff-done", "handoffDone");
184
697
  set("handoff-cancel", "handoffCancel");
698
+ input.sink.setAttribute("aria-label", text("view"));
185
699
  $("lock").setAttribute("aria-label", text("secure"));
186
700
  $("lock").setAttribute("title", text("secure"));
187
701
  for (const [id, key] of [
188
702
  ["back", "back"],
189
703
  ["forward", "forward"],
190
- ["reload", "reload"]
704
+ ["reload", "reload"],
705
+ ["more", "more"]
191
706
  ]) {
192
707
  $(id).setAttribute("aria-label", text(key));
193
708
  $(id).setAttribute("title", text(key));
194
- $(id).textContent = id === "back" ? "<" : id === "forward" ? ">" : "↻";
195
709
  }
196
710
  url.setAttribute("aria-label", text("address"));
197
711
  view.setAttribute("aria-label", text("view"));
@@ -201,26 +715,50 @@
201
715
  function renderStatus() {
202
716
  const el = $("status");
203
717
  el.setAttribute("data-control", control);
204
- let key;
205
- if (mode === "empty" || mode === "offline" || mode === "error") {
206
- el.textContent = "";
207
- return;
718
+ let tone = "quiet";
719
+ let icon = "";
720
+ let lead = null;
721
+ let detail = null;
722
+ if (mode === "connecting") lead = "statusConnecting";
723
+ else if (mode === "live" && control === "user") {
724
+ if (interactive()) {
725
+ tone = "user";
726
+ icon = ICONS.hand;
727
+ lead = "statusUser";
728
+ detail = "statusUserDetail";
729
+ } else lead = "statusUserElsewhere";
730
+ } else if (mode === "live") {
731
+ tone = "agent";
732
+ const loading = lastState?.loading === true;
733
+ icon = loading ? ICONS.spin : ICONS.dot;
734
+ lead = "statusAgent";
735
+ if (loading) detail = "statusLoading";
208
736
  }
209
- if (mode === "connecting") key = "statusConnecting";
210
- else if (control === "user") key = compact() ? "statusUserElsewhere" : "takeoverHint";
211
- else if (lastState?.loading === true) key = "statusLoading";
212
- else key = "statusAgent";
213
- el.textContent = text(key);
737
+ el.setAttribute("data-tone", tone);
738
+ $("status-icon").innerHTML = icon;
739
+ $("status-lead").textContent = lead === null ? "" : text(lead);
740
+ $("status-detail").textContent = detail === null ? "" : ` · ${text(detail)}`;
214
741
  }
215
742
  function renderChrome() {
216
743
  const watchOnly = compact();
744
+ const readOnly = !interactive();
217
745
  const isLive = mode === "live";
218
746
  $("takeover").hidden = watchOnly || !isLive || control === "user";
219
747
  $("handback").hidden = watchOnly || !isLive || control !== "user";
748
+ $("takeover").toggleAttribute("data-gone", watchOnly || isLive && control === "user");
749
+ const verifying = isLive && control === "agent" && tabs.find((x) => x.active)?.blocker?.kind === "verification" && !watchOnly;
750
+ $("verify").hidden = !verifying;
751
+ $("takeover").className = verifying ? "secondary" : "primary";
752
+ $("handoff").hidden = pendingHandoff === null || verifying;
753
+ $("status").hidden = verifying || pendingHandoff !== null;
754
+ $("stage").setAttribute("data-control", isLive ? control : "agent");
755
+ $("field").toggleAttribute("data-edit", interactive());
220
756
  for (const id of ["back", "forward", "reload"])
221
- $(id).disabled = watchOnly;
222
- url.readOnly = watchOnly;
223
- view.style.cursor = interactive() ? "default" : "pointer";
757
+ $(id).disabled = readOnly;
758
+ $("more").disabled = readOnly;
759
+ $("tabsbtn").disabled = readOnly;
760
+ url.readOnly = readOnly;
761
+ input.sync();
224
762
  $("lock").hidden = !isLive || lastState?.secure !== true;
225
763
  const showEmpty = mode === "empty" || mode === "offline" || mode === "error";
226
764
  $("empty").hidden = !showEmpty;
@@ -238,27 +776,40 @@
238
776
  $("blocked").hidden = blockedHost === "";
239
777
  $("blocked-host").textContent = blockedHost;
240
778
  renderStatus();
779
+ renderLoading();
780
+ }
781
+ function renderLoading() {
782
+ const loading = mode === "connecting" || mode === "live" && image === null;
783
+ $("app").toggleAttribute("data-loading", loading);
784
+ $("skeleton").hidden = !loading;
785
+ if (!loading) onBoxChange();
241
786
  }
242
787
  function renderTabs() {
243
788
  const box = $("tabs");
244
789
  box.textContent = "";
245
- const watchOnly = compact();
790
+ const watchOnly = !interactive();
246
791
  for (const tab of tabs) {
247
792
  const el = doc.createElement("div");
248
793
  el.className = "tab";
249
794
  el.setAttribute("role", "tab");
250
795
  el.setAttribute("data-active", String(tab.active));
796
+ el.setAttribute("aria-selected", String(tab.active));
797
+ const fav = doc.createElement("span");
798
+ fav.className = "fav";
799
+ el.appendChild(fav);
251
800
  const label = doc.createElement("button");
252
801
  label.type = "button";
253
802
  label.className = "label";
254
- label.textContent = tab.title === "" ? tab.url : tab.title;
803
+ label.textContent = tabName(tab);
804
+ label.title = tabName(tab);
255
805
  label.disabled = watchOnly;
256
806
  label.addEventListener("click", () => send({ t: "tab", action: "switch", tabId: tab.id }));
257
807
  el.appendChild(label);
258
- if (!watchOnly) {
808
+ if (!watchOnly && tab.active && control === "user") {
259
809
  const close = doc.createElement("button");
260
810
  close.type = "button";
261
- close.textContent = "×";
811
+ close.className = "x";
812
+ close.innerHTML = ICONS.close;
262
813
  close.setAttribute("aria-label", text("closeTab"));
263
814
  close.addEventListener("click", () => send({ t: "tab", action: "close", tabId: tab.id }));
264
815
  el.appendChild(close);
@@ -268,11 +819,65 @@
268
819
  if (!watchOnly) {
269
820
  const add = doc.createElement("button");
270
821
  add.type = "button";
271
- add.textContent = "+";
822
+ add.className = "add";
823
+ add.innerHTML = ICONS.plus;
272
824
  add.setAttribute("aria-label", text("newTab"));
273
825
  add.addEventListener("click", () => send({ t: "tab", action: "open" }));
274
826
  box.appendChild(add);
275
827
  }
828
+ const active = tabs.find((x) => x.active);
829
+ $("tabsbtn-name").textContent = active === void 0 ? "" : hostOf(active.url) || tabName(active);
830
+ $("tabsbtn-count").textContent = String(tabs.length);
831
+ $("tabsbtn").setAttribute("aria-label", `${text("tabs")}: ${tabs.length}`);
832
+ }
833
+ const tabName = (tab) => tab.title === "" ? tab.url : tab.title;
834
+ function hostOf(u) {
835
+ try {
836
+ return new URL(u).host;
837
+ } catch {
838
+ return "";
839
+ }
840
+ }
841
+ function renderUrl(u) {
842
+ let host = u;
843
+ let rest = "";
844
+ try {
845
+ const p = new URL(u);
846
+ if (p.protocol === "http:" || p.protocol === "https:") {
847
+ host = p.host;
848
+ rest = `${p.pathname === "/" ? "" : p.pathname}${p.search}${p.hash}`;
849
+ }
850
+ } catch {
851
+ }
852
+ $("urltext-host").textContent = host;
853
+ $("urltext-path").textContent = rest;
854
+ }
855
+ function openMenu(anchor, items) {
856
+ const menu = $("menu");
857
+ menu.textContent = "";
858
+ for (const it of items) {
859
+ const b = doc.createElement("button");
860
+ b.type = "button";
861
+ b.setAttribute("role", "menuitem");
862
+ b.textContent = it.label;
863
+ if (it.current) b.setAttribute("aria-current", "true");
864
+ b.addEventListener("click", () => {
865
+ closeMenu();
866
+ it.run();
867
+ });
868
+ menu.appendChild(b);
869
+ }
870
+ const r = anchor.getBoundingClientRect();
871
+ menu.style.top = `${Math.round(r.bottom + 4)}px`;
872
+ menu.style.right = `${Math.max(12, Math.round(win.innerWidth - r.right))}px`;
873
+ menu.hidden = false;
874
+ anchor.setAttribute("aria-expanded", "true");
875
+ menu.firstElementChild?.focus();
876
+ }
877
+ function closeMenu() {
878
+ $("menu").hidden = true;
879
+ $("tabsbtn").removeAttribute("aria-expanded");
880
+ $("more").removeAttribute("aria-expanded");
276
881
  }
277
882
  function applyState(s) {
278
883
  lastState = s;
@@ -282,9 +887,11 @@
282
887
  tabs = s.tabs;
283
888
  const was = control;
284
889
  control = s.control;
285
- if (full && was === "user" && control === "agent") bridge(false);
890
+ if (full && was === "user" && control === "agent") askMode(false);
891
+ if (was === "user" && control === "agent" && pendingNav?.sentOn === sock) pendingNav = null;
286
892
  const active = tabs.find((x) => x.active);
287
893
  if (doc.activeElement !== url) url.value = active?.url ?? "";
894
+ renderUrl(active?.url ?? "");
288
895
  renderTabs();
289
896
  renderChrome();
290
897
  if (sock !== null) {
@@ -320,6 +927,7 @@
320
927
  }
321
928
  if (p.sentOn !== sock && sock !== null) {
322
929
  p.sentOn = sock;
930
+ if (control !== "user" && !compact()) takeover();
323
931
  send({ t: "nav", url: p.url });
324
932
  }
325
933
  }
@@ -351,27 +959,18 @@
351
959
  }
352
960
  function layout() {
353
961
  const st = stage.getBoundingClientRect();
354
- const box = letterbox(Math.max(0, Math.floor(st.width)), Math.max(0, Math.floor(st.height)));
962
+ const box = letterbox(
963
+ Math.max(0, Math.floor(st.width) - 2 * STAGE_PAD),
964
+ Math.max(0, Math.floor(st.height) - 2 * STAGE_PAD)
965
+ );
355
966
  view.style.width = `${box.w}px`;
356
967
  view.style.height = `${box.h}px`;
357
968
  const dpr = win.devicePixelRatio || 1;
358
969
  const cap = control === "user" || full ? VIEWPORT.width : SIDE_MAX_WIDTH;
359
- const w = Math.max(1, Math.min(cap, Math.round(box.w * dpr)));
970
+ const w = Math.max(1, Math.min(cap, Math.round(box.w * Math.min(dpr, MAX_DPR))));
360
971
  return { w, h: Math.max(1, Math.round(w / ASPECT)) };
361
972
  }
362
- function fitHeight() {
363
- if (full) return;
364
- const st = stage.getBoundingClientRect();
365
- const width = Math.max(0, Math.floor(st.width));
366
- if (width === 0) return;
367
- const chrome = Math.max(0, win.innerHeight - Math.max(0, st.height));
368
- const height = Math.max(MIN_HEIGHT, Math.min(MAX_HEIGHT, Math.ceil(chrome + width / ASPECT)));
369
- if (height === postedHeight) return;
370
- postedHeight = height;
371
- post({ protocol: PROTOCOL, type: "resize", height });
372
- }
373
973
  function onBoxChange() {
374
- fitHeight();
375
974
  if (sock === null || mode !== "live") return;
376
975
  const { w, h } = layout();
377
976
  if (sentSize !== `${w}x${h}`) sizeMessage("resize");
@@ -394,11 +993,11 @@
394
993
  }
395
994
  async function onFrame(m) {
396
995
  try {
397
- const bin = win.atob(m.jpeg);
398
- const bytes = new Uint8Array(bin.length);
399
- for (let i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i);
400
- image = await decode(bytes);
996
+ const first = image === null;
997
+ image = await decode(m.jpeg);
998
+ if (first) renderLoading();
401
999
  draw();
1000
+ input.frame();
402
1001
  } catch {
403
1002
  }
404
1003
  send({ t: "frame-ack", seq: m.seq });
@@ -440,7 +1039,10 @@
440
1039
  const mine = gen;
441
1040
  quiet = lookOnly && mode === "empty";
442
1041
  if (quiet) armEmptyPoll();
443
- else show("connecting");
1042
+ else {
1043
+ image = null;
1044
+ show("connecting");
1045
+ }
444
1046
  let ticketUrl;
445
1047
  try {
446
1048
  const res = await doFetch("stream-ticket", {
@@ -472,23 +1074,35 @@
472
1074
  };
473
1075
  }
474
1076
  async function onMessage(data) {
475
- let raw;
476
- if (typeof data === "string") raw = data;
477
- else if (data instanceof ArrayBuffer) raw = new TextDecoder().decode(data);
478
- else if (ArrayBuffer.isView(data)) raw = new TextDecoder().decode(data);
1077
+ let bytes;
1078
+ if (typeof data === "string") bytes = new TextEncoder().encode(data);
1079
+ else if (data instanceof ArrayBuffer) bytes = new Uint8Array(data);
1080
+ else if (ArrayBuffer.isView(data))
1081
+ bytes = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
479
1082
  else if (typeof data.arrayBuffer === "function")
480
- raw = new TextDecoder().decode(await data.arrayBuffer());
1083
+ bytes = new Uint8Array(await data.arrayBuffer());
481
1084
  else return;
1085
+ retried401 = false;
1086
+ attempts = 0;
1087
+ restartRetries = 0;
1088
+ const frame = decodeFrame(bytes);
1089
+ if (frame !== null) {
1090
+ void onFrame(frame);
1091
+ return;
1092
+ }
482
1093
  let msg;
483
1094
  try {
484
- msg = JSON.parse(raw);
1095
+ msg = JSON.parse(new TextDecoder().decode(bytes));
485
1096
  } catch {
486
1097
  return;
487
1098
  }
488
- retried401 = false;
489
- attempts = 0;
490
- restartRetries = 0;
491
1099
  switch (msg.t) {
1100
+ case "ping":
1101
+ send({ t: "pong", id: msg.id });
1102
+ break;
1103
+ case "cursor":
1104
+ input.cursor(msg.cursor);
1105
+ break;
492
1106
  case "state":
493
1107
  mode = "live";
494
1108
  wasLive = true;
@@ -496,9 +1110,6 @@
496
1110
  emptySince = void 0;
497
1111
  applyState(msg);
498
1112
  break;
499
- case "frame":
500
- void onFrame(msg);
501
- break;
502
1113
  case "blocked":
503
1114
  blockedHost = msg.host;
504
1115
  blockedUrl = msg.url;
@@ -542,13 +1153,14 @@
542
1153
  }
543
1154
  function takeover() {
544
1155
  send({ t: "takeover" });
545
- bridge(true);
546
- view.focus();
1156
+ askMode(true);
1157
+ input.focus();
547
1158
  renderChrome();
548
1159
  }
549
1160
  function handback() {
1161
+ pendingNav = null;
550
1162
  send({ t: "handback" });
551
- bridge(false);
1163
+ askMode(false);
552
1164
  renderChrome();
553
1165
  if (pendingHandoff !== null) void answerHandoff(true);
554
1166
  }
@@ -578,6 +1190,7 @@
578
1190
  blockedHost = "";
579
1191
  blockedUrl = "";
580
1192
  renderChrome();
1193
+ if (!interactive()) return;
581
1194
  if (target === "") {
582
1195
  send({ t: "nav", action: "reload" });
583
1196
  return;
@@ -611,27 +1224,31 @@
611
1224
  return;
612
1225
  }
613
1226
  }
614
- function onPointer(kind, e) {
615
- if (compact()) return;
616
- if (kind === "move") {
617
- if (!interactive() || e.timeStamp - lastMove < MOVE_INTERVAL_MS) return;
618
- lastMove = e.timeStamp;
619
- }
620
- const r = view.getBoundingClientRect();
621
- const p = mapPoint(e.clientX - r.left, e.clientY - r.top, {
622
- x: 0,
623
- y: 0,
624
- w: r.width,
625
- h: r.height
626
- });
627
- send({
628
- t: "mouse",
629
- kind,
630
- ...p,
631
- ...kind === "move" ? {} : { button: e.button === 2 ? "right" : "left" }
632
- });
633
- }
634
1227
  $("takeover").addEventListener("click", takeover);
1228
+ $("verify-go").addEventListener("click", takeover);
1229
+ $("tabsbtn").addEventListener("click", (e) => {
1230
+ if (!interactive()) return;
1231
+ openMenu(e.currentTarget, [
1232
+ ...tabs.map((tab) => ({
1233
+ label: tabName(tab),
1234
+ current: tab.active,
1235
+ run: () => send({ t: "tab", action: "switch", tabId: tab.id })
1236
+ })),
1237
+ { label: text("newTab"), run: () => send({ t: "tab", action: "open" }) }
1238
+ ]);
1239
+ });
1240
+ $("more").addEventListener("click", (e) => {
1241
+ openMenu(e.currentTarget, [
1242
+ { label: text("forward"), run: () => send({ t: "nav", action: "forward" }) },
1243
+ { label: text("reload"), run: () => send({ t: "nav", action: "reload" }) }
1244
+ ]);
1245
+ });
1246
+ doc.addEventListener("mousedown", (e) => {
1247
+ const m = $("menu");
1248
+ if (m.hidden || m.contains(e.target)) return;
1249
+ if (e.target?.closest?.("#tabsbtn, #more")) return;
1250
+ closeMenu();
1251
+ });
635
1252
  $("handback").addEventListener("click", handback);
636
1253
  $("handoff-done").addEventListener("click", () => void answerHandoff(true));
637
1254
  $("handoff-cancel").addEventListener("click", () => void answerHandoff(false));
@@ -645,45 +1262,17 @@
645
1262
  $("forward").addEventListener("click", () => send({ t: "nav", action: "forward" }));
646
1263
  $("reload").addEventListener("click", () => send({ t: "nav", action: "reload" }));
647
1264
  url.addEventListener("keydown", (e) => {
648
- if (e.key !== "Enter" || compact() || url.value.trim() === "") return;
1265
+ if (e.key !== "Enter" || !interactive() || url.value.trim() === "") return;
649
1266
  send({ t: "nav", url: normalizeUrl(url.value) });
650
- view.focus();
651
- });
652
- view.addEventListener("mousedown", (e) => onPointer("down", e));
653
- view.addEventListener("mouseup", (e) => onPointer("up", e));
654
- view.addEventListener("mousemove", (e) => onPointer("move", e));
655
- view.addEventListener("contextmenu", (e) => e.preventDefault());
656
- view.addEventListener(
657
- "wheel",
658
- (e) => {
659
- if (compact()) return;
660
- e.preventDefault();
661
- const r = view.getBoundingClientRect();
662
- const p = mapPoint(e.clientX - r.left, e.clientY - r.top, {
663
- x: 0,
664
- y: 0,
665
- w: r.width,
666
- h: r.height
667
- });
668
- send({ t: "mouse", kind: "wheel", ...p, dx: e.deltaX, dy: e.deltaY });
669
- },
670
- { passive: false }
671
- );
672
- view.addEventListener("keydown", (e) => {
673
- if (!interactive() || e.key === "Escape" || e.isComposing) return;
674
- e.preventDefault();
675
- send({ t: "key", kind: "down", key: e.key, code: e.code });
676
- });
677
- view.addEventListener("keyup", (e) => {
678
- if (!interactive() || e.key === "Escape" || e.isComposing) return;
679
- e.preventDefault();
680
- send({ t: "key", kind: "up", key: e.key, code: e.code });
681
- });
682
- view.addEventListener("compositionend", (e) => {
683
- if (interactive() && e.data !== "") send({ t: "key", kind: "char", key: e.data, text: e.data });
1267
+ input.focus();
684
1268
  });
685
1269
  doc.addEventListener("keydown", (e) => {
686
- if (e.key === "Escape" && interactive()) handback();
1270
+ if (e.key !== "Escape") return;
1271
+ if (!$("menu").hidden) {
1272
+ closeMenu();
1273
+ return;
1274
+ }
1275
+ if (interactive()) handback();
687
1276
  });
688
1277
  win.addEventListener("resize", () => {
689
1278
  onBoxChange();
@@ -725,7 +1314,6 @@
725
1314
  }
726
1315
  setLabels();
727
1316
  renderChrome();
728
- fitHeight();
729
1317
  win.addEventListener("message", (ev) => {
730
1318
  if (ev.source !== win.parent) return;
731
1319
  const m = ev.data;
@@ -736,8 +1324,7 @@
736
1324
  applyTheme(m.tokens, m.theme);
737
1325
  setLabels();
738
1326
  renderChrome();
739
- postedHeight = 0;
740
- fitHeight();
1327
+ doc.body.setAttribute("data-ready", "");
741
1328
  if (!started) {
742
1329
  started = true;
743
1330
  void connect();
@@ -746,6 +1333,8 @@
746
1333
  token = m.token;
747
1334
  } else if (m.type === "theme") {
748
1335
  applyTheme(m.tokens, m.theme);
1336
+ } else if (m.type === "panel-mode" && typeof m.full === "boolean") {
1337
+ hostMode(m.full);
749
1338
  }
750
1339
  });
751
1340
  }