dsh-browser-cdp 0.0.1 → 0.16.23
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/LICENSE +27 -0
- package/README.md +285 -9
- package/THIRD_PARTY_NOTICES.md +62 -0
- package/bin/cdp-cast-worker.mjs +7012 -0
- package/bin/cdp-chrome-wrapper.sh +53 -0
- package/bin/cdp-render-worker.mjs +610 -0
- package/bin/record-normalize.d.mts +32 -0
- package/bin/record-normalize.mjs +156 -0
- package/cordis.patch.yml +17 -0
- package/dsh-plugin.json +148 -0
- package/lib/client.js +5985 -0
- package/lib/index.js +8923 -0
- package/lib/launcher-CQyI8-By.js +3 -0
- package/lib/launcher-DBaxfIsF.js +269 -0
- package/locale/de.json +6 -0
- package/locale/en.json +6 -0
- package/locale/es.json +6 -0
- package/locale/fr.json +6 -0
- package/locale/it.json +6 -0
- package/locale/ja.json +6 -0
- package/locale/ko.json +6 -0
- package/locale/ru.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +94 -14
- package/runtime/PATCHES.md +30 -0
- package/runtime/ego-browser/dist/out/index.js +7206 -0
- package/runtime/ego-browser/screenshot-clip.d.mts +22 -0
- package/runtime/ego-browser/screenshot-clip.mjs +45 -0
- package/runtime/ego-linux/bin/ego-browser.mjs +399 -0
- package/runtime/ego-linux/src/agent-identity.mjs +65 -0
- package/runtime/ego-linux/src/chrome.mjs +867 -0
- package/runtime/ego-linux/src/cursor.mjs +1292 -0
- package/runtime/ego-linux/src/desktop.mjs +73 -0
- package/runtime/ego-linux/src/paths.mjs +35 -0
- package/runtime/ego-linux/src/session.mjs +37 -0
- package/runtime/ego-linux/src/shim.mjs +145 -0
- package/runtime/ego-linux/src/snapshot.mjs +424 -0
- package/runtime/ego-linux/src/spaces-server.mjs +355 -0
- package/runtime/ego-linux/src/spaces-ui.mjs +456 -0
- package/runtime/ego-linux/src/tabs.mjs +109 -0
- package/runtime/ego-linux/src/task-spaces.mjs +671 -0
- package/runtime/ego-linux/src/transport.mjs +321 -0
- package/runtime/ego-linux/src/window-fit.mjs +107 -0
- package/runtime/skills/ego-browser/SKILL.md +209 -0
- package/runtime/skills/ego-browser/agents/openai.yaml +7 -0
- package/runtime/skills/ego-browser/assets/composer-icon.png +0 -0
- package/runtime/skills/ego-browser/assets/logo.png +0 -0
- package/runtime/skills/ego-browser/learnings/google/browser-tools/autocomplete.js +11 -0
- package/runtime/skills/ego-browser/learnings/google/manifest.json +40 -0
- package/runtime/skills/ego-browser/learnings/google/notes/overview.md +23 -0
- package/runtime/skills/ego-browser/learnings/google/tools/search-extract.js +35 -0
- package/runtime/skills/ego-browser/learnings/x-com/browser-tools/extract-post.js +9 -0
- package/runtime/skills/ego-browser/learnings/x-com/manifest.json +51 -0
- package/runtime/skills/ego-browser/learnings/x-com/notes/overview.md +18 -0
- package/runtime/skills/ego-browser/learnings/x-com/notes/timeline.md +19 -0
- package/runtime/skills/ego-browser/learnings/x-com/tools/search-users.js +33 -0
- package/runtime/skills/ego-browser/learnings/x-com/tools/timeline.js +32 -0
- package/runtime/skills/ego-browser/references/install.md +131 -0
- package/runtime/skills/ego-browser/references/task-spaces.md +122 -0
- package/runtime/skills/ego-browser/references/video.md +17 -0
- package/runtime/skills/ego-browser/scripts/install.sh +309 -0
- package/index.js +0 -11
|
@@ -0,0 +1,1292 @@
|
|
|
1
|
+
import { createSessionResolver } from "./session.mjs";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The agent's cursor — the visible "something else is driving this page" mark.
|
|
5
|
+
*
|
|
6
|
+
* The native macOS app draws this itself, over the web view; on Linux the page
|
|
7
|
+
* is the only surface the shim controls, so the cursor is a DOM overlay
|
|
8
|
+
* injected into whichever page the harness is currently acting on. It follows
|
|
9
|
+
* every pointer position the harness sends, presses and springs back when a
|
|
10
|
+
* button goes down and up, ripples where it clicks, sweeps the lines it reads,
|
|
11
|
+
* and carries the agent's current task state as a label.
|
|
12
|
+
*
|
|
13
|
+
* Two properties keep it from interfering with the automation it illustrates:
|
|
14
|
+
*
|
|
15
|
+
* - the host element is `pointer-events: none`, so `document.elementFromPoint`
|
|
16
|
+
* never returns it. That matters beyond cosmetics: the harness's wheel and
|
|
17
|
+
* drag fallbacks hit-test with elementFromPoint, and an overlay that
|
|
18
|
+
* answered those probes would swallow input meant for the page.
|
|
19
|
+
* - every render is fire-and-forget and swallows its own errors, so a page
|
|
20
|
+
* that refuses the injection, or navigates mid-flight, can never fail an
|
|
21
|
+
* action. The cursor is allowed to be missing; it is not allowed to break
|
|
22
|
+
* anything.
|
|
23
|
+
*
|
|
24
|
+
* The overlay is a Runtime.evaluate injection rather than a content script, so
|
|
25
|
+
* it works on pages with a strict CSP and needs no extension.
|
|
26
|
+
*
|
|
27
|
+
* Env: EGO_LINUX_CURSOR=0 turns it off (it is drawn into screenshots, which is
|
|
28
|
+
* usually wanted and occasionally not); EGO_LINUX_CURSOR_NAME renames it from
|
|
29
|
+
* the default "DeepSeek".
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
const HOST_ID = "ego-agent-cursor-overlay";
|
|
33
|
+
const ACCENT = "#d97757";
|
|
34
|
+
// Reading is where an agent spends most of its time, and the state a watcher is
|
|
35
|
+
// likeliest to mistake for an idle window. Giving it its own colour means one
|
|
36
|
+
// glance separates looking at the page from changing it — terracotta acts, blue
|
|
37
|
+
// observes. Every mark the overlay draws while reading switches to this, and
|
|
38
|
+
// switches back the moment real input lands.
|
|
39
|
+
const READ_ACCENT = "#4a9eff";
|
|
40
|
+
|
|
41
|
+
/** How long the pressed look is held, however briefly the button was down. */
|
|
42
|
+
const MIN_PRESS_MS = 130;
|
|
43
|
+
|
|
44
|
+
/** Keystrokes stop arriving well before the agent is done thinking; hold on. */
|
|
45
|
+
const TYPING_IDLE_MS = 700;
|
|
46
|
+
|
|
47
|
+
export function createCursorApi(cdp, { listTabs }) {
|
|
48
|
+
const enabled = process.env.EGO_LINUX_CURSOR !== "0";
|
|
49
|
+
const name = process.env.EGO_LINUX_CURSOR_NAME || "DeepSeek";
|
|
50
|
+
const sessionForActiveTab = createSessionResolver(cdp, {
|
|
51
|
+
listTabs,
|
|
52
|
+
op: "cursor",
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
const state = {
|
|
56
|
+
x: 0,
|
|
57
|
+
y: 0,
|
|
58
|
+
label: "",
|
|
59
|
+
visible: true,
|
|
60
|
+
placed: false,
|
|
61
|
+
pressed: false,
|
|
62
|
+
typing: false,
|
|
63
|
+
note: "",
|
|
64
|
+
highlight: null,
|
|
65
|
+
read: null,
|
|
66
|
+
};
|
|
67
|
+
// Where the cursor rests on a page it has only read, never touched.
|
|
68
|
+
const RESTING_X = 28;
|
|
69
|
+
const RESTING_Y = 28;
|
|
70
|
+
let previousLabel = "";
|
|
71
|
+
let dirty = false;
|
|
72
|
+
let pendingPulse = false;
|
|
73
|
+
let inFlight = false;
|
|
74
|
+
let pressedAt = 0;
|
|
75
|
+
let releaseTimer = null;
|
|
76
|
+
let typingTimer = null;
|
|
77
|
+
let labelX = null;
|
|
78
|
+
let labelY = null;
|
|
79
|
+
let highlightId = 0;
|
|
80
|
+
let readId = 0;
|
|
81
|
+
|
|
82
|
+
const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Put the overlay back after the page it lived in was replaced.
|
|
86
|
+
*
|
|
87
|
+
* The cursor is a DOM node injected into the current document, so a
|
|
88
|
+
* navigation destroys it — and nothing re-injects. Only a render does that,
|
|
89
|
+
* and renders are driven by snapshots, pointer events and keystrokes, none of
|
|
90
|
+
* which a goto() performs. So an agent that navigated and then worked through
|
|
91
|
+
* page.evaluate or a site tool — neither of which snapshots — drove the page
|
|
92
|
+
* with no cursor on screen at all, which reads as an idle window.
|
|
93
|
+
*
|
|
94
|
+
* The old coordinates described elements in a document that no longer exists,
|
|
95
|
+
* so re-drawing at them would point confidently at nothing. The cursor goes
|
|
96
|
+
* back to its resting corner instead and lets the badge carry what the agent
|
|
97
|
+
* is doing; the label is already marked stale by the move, so it presents
|
|
98
|
+
* itself as history rather than as the current action.
|
|
99
|
+
*/
|
|
100
|
+
function pageReplaced() {
|
|
101
|
+
if (!enabled) return;
|
|
102
|
+
endRead();
|
|
103
|
+
state.x = RESTING_X;
|
|
104
|
+
state.y = RESTING_Y;
|
|
105
|
+
state.placed = true;
|
|
106
|
+
schedule();
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Events only reach the shim for sessions it has claimed, and only for domains
|
|
110
|
+
// enabled on them — see claimSession/onShimEvent in transport.mjs. Registered
|
|
111
|
+
// once here; flush() does the per-session claim as targets come and go.
|
|
112
|
+
cdp.onShimEvent?.("Page.loadEventFired", () => pageReplaced());
|
|
113
|
+
|
|
114
|
+
/** Sessions already claimed and told to report loads. */
|
|
115
|
+
const watchedSessions = new Set();
|
|
116
|
+
|
|
117
|
+
async function watchLoads(sessionId) {
|
|
118
|
+
if (!sessionId || watchedSessions.has(sessionId)) return;
|
|
119
|
+
watchedSessions.add(sessionId);
|
|
120
|
+
cdp.claimSession?.(sessionId);
|
|
121
|
+
// Without Page.enable the load event never fires on this session. Failure is
|
|
122
|
+
// cosmetic, exactly like every other render step here.
|
|
123
|
+
await cdp.call("Page.enable", {}, sessionId).catch(() => {});
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* A read sweep narrates the snapshot that started it, so any real input makes
|
|
128
|
+
* it stale. Dropping the read from the payload is how the page is told to stop
|
|
129
|
+
* — the sweep owns the cursor only while the id it began with keeps arriving.
|
|
130
|
+
*/
|
|
131
|
+
function endRead() {
|
|
132
|
+
state.read = null;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function placeAt(x, y) {
|
|
136
|
+
if (!Number.isFinite(x) || !Number.isFinite(y)) return;
|
|
137
|
+
state.x = x;
|
|
138
|
+
state.y = y;
|
|
139
|
+
state.placed = true;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Coalesce renders: a drag dispatches mouse moves far faster than a CDP round
|
|
144
|
+
* trip completes, so only the latest position is ever sent, and a pulse that
|
|
145
|
+
* lands mid-flight is carried over to the next render rather than dropped.
|
|
146
|
+
*/
|
|
147
|
+
function schedule() {
|
|
148
|
+
dirty = true;
|
|
149
|
+
if (inFlight) return;
|
|
150
|
+
void flush();
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
async function flush() {
|
|
154
|
+
if (!dirty || inFlight) return;
|
|
155
|
+
inFlight = true;
|
|
156
|
+
try {
|
|
157
|
+
while (dirty) {
|
|
158
|
+
dirty = false;
|
|
159
|
+
const payload = {
|
|
160
|
+
x: state.x,
|
|
161
|
+
y: state.y,
|
|
162
|
+
// The label always goes out, so the badge never empties mid-session —
|
|
163
|
+
// a blank badge tells a watcher less than a slightly old one. Whether
|
|
164
|
+
// it still describes where the cursor now is travels alongside it, and
|
|
165
|
+
// the overlay dims a stale label rather than passing it off as current.
|
|
166
|
+
label: state.label,
|
|
167
|
+
labelStale: !(state.x === labelX && state.y === labelY),
|
|
168
|
+
name,
|
|
169
|
+
accent: ACCENT,
|
|
170
|
+
readAccent: READ_ACCENT,
|
|
171
|
+
hostId: HOST_ID,
|
|
172
|
+
// Typing counts as something to show even before the cursor has been
|
|
173
|
+
// placed, because fill() never places it.
|
|
174
|
+
visible: state.visible && (state.placed || state.typing),
|
|
175
|
+
placed: state.placed,
|
|
176
|
+
pressed: state.pressed,
|
|
177
|
+
typing: state.typing,
|
|
178
|
+
note: state.note,
|
|
179
|
+
highlight: state.highlight,
|
|
180
|
+
read: state.read,
|
|
181
|
+
pulse: pendingPulse,
|
|
182
|
+
};
|
|
183
|
+
pendingPulse = false;
|
|
184
|
+
const sessionId = await sessionForActiveTab();
|
|
185
|
+
await watchLoads(sessionId);
|
|
186
|
+
await cdp.call(
|
|
187
|
+
"Runtime.evaluate",
|
|
188
|
+
{
|
|
189
|
+
expression: `(${renderOverlay.toString()})(${JSON.stringify(payload)})`,
|
|
190
|
+
returnByValue: false,
|
|
191
|
+
awaitPromise: false,
|
|
192
|
+
userGesture: false,
|
|
193
|
+
},
|
|
194
|
+
sessionId,
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
} catch {
|
|
198
|
+
// Cosmetic only: a closed tab, a page mid-navigation or a target that
|
|
199
|
+
// rejected the evaluate must never surface as an automation failure.
|
|
200
|
+
} finally {
|
|
201
|
+
inFlight = false;
|
|
202
|
+
if (dirty) void flush();
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
return {
|
|
207
|
+
/**
|
|
208
|
+
* Arm the load watcher ahead of a navigation the overlay has to survive.
|
|
209
|
+
*
|
|
210
|
+
* flush() claims the session as a side effect of rendering, so a process
|
|
211
|
+
* whose very first act is a goto has claimed nothing yet and misses its own
|
|
212
|
+
* first load — leaving the window blank for exactly the shape of task that
|
|
213
|
+
* navigates and then works through page.evaluate or a site tool. Called on
|
|
214
|
+
* Page.navigate, which the harness sends before the document is replaced.
|
|
215
|
+
*/
|
|
216
|
+
async watchPage() {
|
|
217
|
+
if (!enabled) return { done: false, reason: "cursor disabled" };
|
|
218
|
+
try {
|
|
219
|
+
await watchLoads(await sessionForActiveTab());
|
|
220
|
+
} catch {
|
|
221
|
+
// No tab yet, or a target that refused the attach: cosmetic, like every
|
|
222
|
+
// other step here.
|
|
223
|
+
}
|
|
224
|
+
return { done: true };
|
|
225
|
+
},
|
|
226
|
+
|
|
227
|
+
/** Back ego.animationHighlightMouseToPosition(x, y). */
|
|
228
|
+
moveTo(x, y) {
|
|
229
|
+
if (!enabled) return { done: false, reason: "cursor disabled" };
|
|
230
|
+
if (!Number.isFinite(x) || !Number.isFinite(y)) return { done: false };
|
|
231
|
+
// A redundant move is normally worth nothing, but a read sweep has walked
|
|
232
|
+
// the cursor away from where the harness last put it — so moving back to
|
|
233
|
+
// that same point is a real move, and the one that ends the sweep.
|
|
234
|
+
if (state.placed && state.x === x && state.y === y && !state.read) {
|
|
235
|
+
return { done: true };
|
|
236
|
+
}
|
|
237
|
+
endRead();
|
|
238
|
+
state.x = x;
|
|
239
|
+
state.y = y;
|
|
240
|
+
state.placed = true;
|
|
241
|
+
// The label is no longer blanked here: it stays up, marked stale, until a
|
|
242
|
+
// real action replaces it. Emptying it on every move is what made the
|
|
243
|
+
// badge flicker on a page the agent was only walking across.
|
|
244
|
+
schedule();
|
|
245
|
+
return { done: true };
|
|
246
|
+
},
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* The agent is reading the page rather than touching it.
|
|
250
|
+
*
|
|
251
|
+
* Reading is most of what an agent does — open a page, snapshot it, decide —
|
|
252
|
+
* and none of it dispatches a pointer event, so until now the overlay simply
|
|
253
|
+
* never appeared: watching a session work looked like nothing was happening.
|
|
254
|
+
* Placing the cursor on the first read gives the badge (which already
|
|
255
|
+
* breathes) something to attach to, so reading reads as activity.
|
|
256
|
+
*
|
|
257
|
+
* The resting spot is only used when no click has placed the cursor yet; a
|
|
258
|
+
* cursor that has been somewhere stays there, because jumping it to a corner
|
|
259
|
+
* on every snapshot would lose where the agent actually was.
|
|
260
|
+
*
|
|
261
|
+
* The badge alone said the agent was reading but never *what*, which is the
|
|
262
|
+
* one thing a person watching wants to know — so each read also carries an
|
|
263
|
+
* id, and the page runs a sweep for it: the cursor travels the lines that
|
|
264
|
+
* are actually on screen, marking each as it passes. The sweep is driven
|
|
265
|
+
* from inside the page, because a round trip per line would cost more than
|
|
266
|
+
* the snapshot it illustrates.
|
|
267
|
+
*/
|
|
268
|
+
reading(label = "reading") {
|
|
269
|
+
if (!enabled) return { done: false, reason: "cursor disabled" };
|
|
270
|
+
if (!state.placed) {
|
|
271
|
+
state.x = RESTING_X;
|
|
272
|
+
state.y = RESTING_Y;
|
|
273
|
+
state.placed = true;
|
|
274
|
+
}
|
|
275
|
+
previousLabel = state.label;
|
|
276
|
+
if (state.label !== label) state.label = label;
|
|
277
|
+
readId += 1;
|
|
278
|
+
state.read = { id: readId };
|
|
279
|
+
schedule();
|
|
280
|
+
return { done: true };
|
|
281
|
+
},
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* The label persists until a different activity replaces it.
|
|
285
|
+
*
|
|
286
|
+
* A snapshot completes in milliseconds, so clearing the label when the read
|
|
287
|
+
* returned made "reading" flash for less than a frame — the badge just read
|
|
288
|
+
* "DeepSeek". Leaving it up means the window always says what the agent last
|
|
289
|
+
* did, which is what makes an idle-looking window legible. moveTo and
|
|
290
|
+
* pulseAt take it down again when real input arrives.
|
|
291
|
+
*/
|
|
292
|
+
clearReadingLabel() {
|
|
293
|
+
if (state.label !== "reading") return;
|
|
294
|
+
state.label = previousLabel === "reading" ? "" : previousLabel || "";
|
|
295
|
+
schedule();
|
|
296
|
+
},
|
|
297
|
+
|
|
298
|
+
/** Back ego.setAgentTaskState(label) — the text shown next to the cursor. */
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Back ego.setAgentTaskState(label).
|
|
302
|
+
*
|
|
303
|
+
* The harness sets this from an action's own `label` option, immediately
|
|
304
|
+
* after moving the cursor there — so it describes *that* action, at *that*
|
|
305
|
+
* point. It is therefore remembered against the point it was set for, and
|
|
306
|
+
* stops applying once the cursor moves on. Without that it would be set
|
|
307
|
+
* once and then narrate every later action wrongly, forever.
|
|
308
|
+
*/
|
|
309
|
+
setTaskState(taskState) {
|
|
310
|
+
if (!enabled) return { done: false, reason: "cursor disabled" };
|
|
311
|
+
const label = labelOf(taskState);
|
|
312
|
+
if (label === state.label && labelX === state.x && labelY === state.y) {
|
|
313
|
+
return { done: true };
|
|
314
|
+
}
|
|
315
|
+
state.label = label;
|
|
316
|
+
labelX = state.x;
|
|
317
|
+
labelY = state.y;
|
|
318
|
+
schedule();
|
|
319
|
+
return { done: true };
|
|
320
|
+
},
|
|
321
|
+
|
|
322
|
+
/** A button went down here: hold the cursor pressed, and ripple. */
|
|
323
|
+
press(x, y) {
|
|
324
|
+
if (!enabled) return;
|
|
325
|
+
endRead();
|
|
326
|
+
placeAt(x, y);
|
|
327
|
+
clearTimeout(releaseTimer);
|
|
328
|
+
state.pressed = true;
|
|
329
|
+
pressedAt = Date.now();
|
|
330
|
+
pendingPulse = true;
|
|
331
|
+
schedule();
|
|
332
|
+
},
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* The button came back up. A click's press and release are 25ms apart, which
|
|
336
|
+
* is too short to read as anything, so the pressed look is held for a floor
|
|
337
|
+
* of MIN_PRESS_MS. A drag releases long after that floor and springs back at
|
|
338
|
+
* once.
|
|
339
|
+
*/
|
|
340
|
+
release(x, y) {
|
|
341
|
+
if (!enabled) return;
|
|
342
|
+
endRead();
|
|
343
|
+
placeAt(x, y);
|
|
344
|
+
if (!state.pressed) {
|
|
345
|
+
schedule();
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
const remaining = MIN_PRESS_MS - (Date.now() - pressedAt);
|
|
349
|
+
if (remaining <= 0) {
|
|
350
|
+
state.pressed = false;
|
|
351
|
+
schedule();
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
schedule(); // the new position now; the spring back on its own schedule
|
|
355
|
+
clearTimeout(releaseTimer);
|
|
356
|
+
releaseTimer = setTimeout(() => {
|
|
357
|
+
state.pressed = false;
|
|
358
|
+
schedule();
|
|
359
|
+
}, remaining);
|
|
360
|
+
// A heredoc is a short-lived process; never hold it open for a flourish.
|
|
361
|
+
releaseTimer.unref?.();
|
|
362
|
+
},
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* A key went to the page. Typing has no coordinates, so this is a state
|
|
366
|
+
* with a tail rather than an event: it stays on until the keystrokes stop.
|
|
367
|
+
* Called per key event, but the render coalescer collapses a burst into a
|
|
368
|
+
* couple of draws, and re-drawing is what lets the ring follow focus from
|
|
369
|
+
* one field to the next.
|
|
370
|
+
*/
|
|
371
|
+
typed() {
|
|
372
|
+
if (!enabled) return;
|
|
373
|
+
endRead();
|
|
374
|
+
clearTimeout(typingTimer);
|
|
375
|
+
typingTimer = setTimeout(() => {
|
|
376
|
+
state.typing = false;
|
|
377
|
+
schedule();
|
|
378
|
+
}, TYPING_IDLE_MS);
|
|
379
|
+
typingTimer.unref?.();
|
|
380
|
+
state.typing = true;
|
|
381
|
+
schedule();
|
|
382
|
+
},
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* Draw a marker over some text, the way you would to explain it.
|
|
386
|
+
*
|
|
387
|
+
* `target` is a CSS selector, or the text itself — a string is tried as a
|
|
388
|
+
* selector first and searched for as text if that finds nothing, so both
|
|
389
|
+
* `highlight("#total")` and `highlight("free shipping")` do the obvious
|
|
390
|
+
* thing. `{selector}` or `{text}` forces one.
|
|
391
|
+
*
|
|
392
|
+
* The bands are an overlay, never a real selection: selecting text for the
|
|
393
|
+
* look of it would fight the agent's own work on the page.
|
|
394
|
+
*
|
|
395
|
+
* @param {string|{selector?: string, text?: string}} target
|
|
396
|
+
* @param {{note?: string}} [options] Text to show next to the cursor while drawing.
|
|
397
|
+
* @returns {Promise<{done: boolean, lines: number}>}
|
|
398
|
+
*/
|
|
399
|
+
async highlight(target, options = {}) {
|
|
400
|
+
if (!enabled) return { done: false, lines: 0, reason: "cursor disabled" };
|
|
401
|
+
const request =
|
|
402
|
+
typeof target === "string"
|
|
403
|
+
? { selector: target, text: target }
|
|
404
|
+
: { selector: target?.selector || "", text: target?.text || "" };
|
|
405
|
+
|
|
406
|
+
let found;
|
|
407
|
+
try {
|
|
408
|
+
const sessionId = await sessionForActiveTab();
|
|
409
|
+
const { result } = await cdp.call(
|
|
410
|
+
"Runtime.evaluate",
|
|
411
|
+
{
|
|
412
|
+
expression: `(${resolveHighlight.toString()})(${JSON.stringify(request)})`,
|
|
413
|
+
returnByValue: true,
|
|
414
|
+
awaitPromise: false,
|
|
415
|
+
},
|
|
416
|
+
sessionId,
|
|
417
|
+
);
|
|
418
|
+
found = result?.value;
|
|
419
|
+
} catch {
|
|
420
|
+
return { done: false, lines: 0 };
|
|
421
|
+
}
|
|
422
|
+
if (!found || !found.rects?.length) return { done: false, lines: 0 };
|
|
423
|
+
|
|
424
|
+
// An explicit marker replaces the automatic sweep: both drive the cursor,
|
|
425
|
+
// and the agent's own explanation is the one worth watching.
|
|
426
|
+
endRead();
|
|
427
|
+
state.note = String(options.note ?? "").slice(0, 120);
|
|
428
|
+
highlightId += 1;
|
|
429
|
+
state.highlight = { id: highlightId, rects: found.rects, upTo: -1 };
|
|
430
|
+
|
|
431
|
+
// Walk the cursor along each line as its band draws, so the marker looks
|
|
432
|
+
// drawn rather than pasted. The caller awaits this: it is an explanation,
|
|
433
|
+
// and an explanation you cannot see happen is just a screenshot.
|
|
434
|
+
for (let index = 0; index < found.rects.length; index += 1) {
|
|
435
|
+
const rect = found.rects[index];
|
|
436
|
+
const baseline = rect.y + rect.height - found.scrollY;
|
|
437
|
+
state.x = rect.x - found.scrollX;
|
|
438
|
+
state.y = baseline;
|
|
439
|
+
state.placed = true;
|
|
440
|
+
state.highlight.upTo = index;
|
|
441
|
+
schedule();
|
|
442
|
+
await wait(Math.round(rect.ms * 0.2));
|
|
443
|
+
state.x = rect.x + rect.width - found.scrollX;
|
|
444
|
+
state.y = baseline;
|
|
445
|
+
schedule();
|
|
446
|
+
await wait(rect.ms);
|
|
447
|
+
}
|
|
448
|
+
return { done: true, lines: found.rects.length };
|
|
449
|
+
},
|
|
450
|
+
|
|
451
|
+
/** Wipe the marker off again. */
|
|
452
|
+
clearHighlight() {
|
|
453
|
+
if (!enabled) return { done: false, reason: "cursor disabled" };
|
|
454
|
+
state.highlight = null;
|
|
455
|
+
state.note = "";
|
|
456
|
+
schedule();
|
|
457
|
+
return { done: true };
|
|
458
|
+
},
|
|
459
|
+
|
|
460
|
+
/** Hidden while the space belongs to the user (handOffTaskSpace). */
|
|
461
|
+
hide() {
|
|
462
|
+
if (!enabled) return;
|
|
463
|
+
if (!state.visible) return;
|
|
464
|
+
state.visible = false;
|
|
465
|
+
schedule();
|
|
466
|
+
},
|
|
467
|
+
|
|
468
|
+
/** Shown again when the agent resumes (takeOverTaskSpace). */
|
|
469
|
+
show() {
|
|
470
|
+
if (!enabled) return;
|
|
471
|
+
if (state.visible) return;
|
|
472
|
+
state.visible = true;
|
|
473
|
+
schedule();
|
|
474
|
+
},
|
|
475
|
+
};
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
/**
|
|
479
|
+
* Find what to draw the marker over, and measure it line by line.
|
|
480
|
+
*
|
|
481
|
+
* Serialized into the page like the renderer. A Range is what gives one box per
|
|
482
|
+
* *line* rather than one for the whole paragraph — which is the difference
|
|
483
|
+
* between a pen stroke and a coloured rectangle.
|
|
484
|
+
*/
|
|
485
|
+
function resolveHighlight(request) {
|
|
486
|
+
let range = null;
|
|
487
|
+
|
|
488
|
+
if (request.selector) {
|
|
489
|
+
let element = null;
|
|
490
|
+
try {
|
|
491
|
+
element = document.querySelector(request.selector);
|
|
492
|
+
} catch {
|
|
493
|
+
element = null; // a text query is rarely also valid CSS
|
|
494
|
+
}
|
|
495
|
+
if (element) {
|
|
496
|
+
range = document.createRange();
|
|
497
|
+
range.selectNodeContents(element);
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
if (!range && request.text) {
|
|
502
|
+
const needle = request.text.trim().toLowerCase();
|
|
503
|
+
const walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT);
|
|
504
|
+
while (walker.nextNode()) {
|
|
505
|
+
const node = walker.currentNode;
|
|
506
|
+
const parent = node.parentElement;
|
|
507
|
+
if (!parent || !parent.getClientRects().length) continue;
|
|
508
|
+
const at = (node.nodeValue || "").toLowerCase().indexOf(needle);
|
|
509
|
+
if (at === -1) continue;
|
|
510
|
+
range = document.createRange();
|
|
511
|
+
range.setStart(node, at);
|
|
512
|
+
range.setEnd(node, at + needle.length);
|
|
513
|
+
break;
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
if (!range) return { rects: [], scrollX: window.scrollX, scrollY: window.scrollY };
|
|
518
|
+
|
|
519
|
+
// A marker nobody can see explains nothing, so bring it on screen first.
|
|
520
|
+
const first = range.getBoundingClientRect();
|
|
521
|
+
if (first.bottom < 0 || first.top > window.innerHeight) {
|
|
522
|
+
const anchor = range.startContainer.parentElement;
|
|
523
|
+
anchor?.scrollIntoView({ block: "center" });
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
const rects = [];
|
|
527
|
+
for (const rect of range.getClientRects()) {
|
|
528
|
+
if (rect.width < 2 || rect.height < 2) continue;
|
|
529
|
+
rects.push({
|
|
530
|
+
x: rect.left + window.scrollX,
|
|
531
|
+
y: rect.top + window.scrollY,
|
|
532
|
+
width: rect.width,
|
|
533
|
+
height: rect.height,
|
|
534
|
+
// Long lines take longer to draw, the way a real stroke would.
|
|
535
|
+
ms: Math.min(700, Math.max(160, Math.round(rect.width * 1.7))),
|
|
536
|
+
});
|
|
537
|
+
}
|
|
538
|
+
return { rects, scrollX: window.scrollX, scrollY: window.scrollY };
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
/**
|
|
542
|
+
* Read the overlay back out of a page: where the cursor is, what it is doing,
|
|
543
|
+
* and how long ago it last moved. The Spaces overview runs this in its own
|
|
544
|
+
* process — the page is the only state the two share, since each heredoc is a
|
|
545
|
+
* separate Node process with its own CDP connection.
|
|
546
|
+
*
|
|
547
|
+
* Returns null when no agent has acted in that page.
|
|
548
|
+
*/
|
|
549
|
+
export const CURSOR_PROBE_EXPRESSION = `(${probeOverlay.toString()})(${JSON.stringify(HOST_ID)})`;
|
|
550
|
+
|
|
551
|
+
function probeOverlay(hostId) {
|
|
552
|
+
const host = document.getElementById(hostId);
|
|
553
|
+
const state = host && host.__egoState;
|
|
554
|
+
if (!state) return null;
|
|
555
|
+
return {
|
|
556
|
+
// Where the cursor is *now*: it is anchored to the page, so a scroll since
|
|
557
|
+
// the last action has moved it on screen. Reporting the stale viewport
|
|
558
|
+
// position would crop an active card to the wrong place.
|
|
559
|
+
x: state.pageX - window.scrollX,
|
|
560
|
+
y: state.pageY - window.scrollY,
|
|
561
|
+
label: state.label || "",
|
|
562
|
+
name: state.name || "",
|
|
563
|
+
ageMs: Date.now() - state.at,
|
|
564
|
+
// Newest last, ages rather than timestamps: the reader is in another
|
|
565
|
+
// process and has no business trusting this page's clock.
|
|
566
|
+
trail: (host.__egoLog || []).map((entry) => ({
|
|
567
|
+
text: entry.text,
|
|
568
|
+
ageMs: Date.now() - entry.at,
|
|
569
|
+
})),
|
|
570
|
+
// A screenshot clip is in page coordinates; hand back what converts it.
|
|
571
|
+
scrollX: window.scrollX,
|
|
572
|
+
scrollY: window.scrollY,
|
|
573
|
+
viewportWidth: window.innerWidth,
|
|
574
|
+
viewportHeight: window.innerHeight,
|
|
575
|
+
};
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
/**
|
|
579
|
+
* The harness passes a label string, but the native surface is documented only
|
|
580
|
+
* as "task state" — accept the object form rather than printing [object Object].
|
|
581
|
+
*/
|
|
582
|
+
function labelOf(taskState) {
|
|
583
|
+
if (taskState === null || taskState === undefined) return "";
|
|
584
|
+
if (typeof taskState === "string") return taskState.slice(0, 120);
|
|
585
|
+
if (typeof taskState === "object") {
|
|
586
|
+
const text = taskState.label ?? taskState.state ?? taskState.text;
|
|
587
|
+
return typeof text === "string" ? text.slice(0, 120) : "";
|
|
588
|
+
}
|
|
589
|
+
return String(taskState).slice(0, 120);
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
/**
|
|
593
|
+
* The overlay itself, serialized with Function.prototype.toString() and run
|
|
594
|
+
* inside the page on every update. It must stay self-contained: no imports, no
|
|
595
|
+
* closure over anything in this module, and idempotent — it is re-entered for
|
|
596
|
+
* every move, and re-creates itself after a navigation blew the old one away.
|
|
597
|
+
*/
|
|
598
|
+
function renderOverlay(payload) {
|
|
599
|
+
const parent = document.body || document.documentElement;
|
|
600
|
+
if (!parent) return;
|
|
601
|
+
|
|
602
|
+
let host = document.getElementById(payload.hostId);
|
|
603
|
+
if (!payload.visible) {
|
|
604
|
+
// Handing the space back is a departure, not a disappearance, so the cursor
|
|
605
|
+
// fades instead of blinking out. The state the Spaces overview polls goes
|
|
606
|
+
// immediately though: the agent has let go, whatever the pixels still say.
|
|
607
|
+
if (host && !host.__egoLeaving) {
|
|
608
|
+
host.__egoLeaving = true;
|
|
609
|
+
host.__egoState = null;
|
|
610
|
+
if (host.__egoSweep) host.__egoSweep.done = true;
|
|
611
|
+
host.style.opacity = "0";
|
|
612
|
+
setTimeout(() => host.remove(), 220);
|
|
613
|
+
}
|
|
614
|
+
return;
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
// A host on its way out is not one to reuse: its opacity is already bound for
|
|
618
|
+
// zero, and a taken-over space would inherit the fade meant for the handoff.
|
|
619
|
+
if (host && host.__egoLeaving) {
|
|
620
|
+
host.remove();
|
|
621
|
+
host = null;
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
if (!host) {
|
|
625
|
+
host = document.createElement("div");
|
|
626
|
+
host.id = payload.hostId;
|
|
627
|
+
host.setAttribute("aria-hidden", "true");
|
|
628
|
+
// Inline, so page rules cannot win against it, and `pointer-events: none`
|
|
629
|
+
// last so `all: initial` cannot reset it back to auto. Every child inherits
|
|
630
|
+
// that, which is what keeps elementFromPoint blind to the overlay.
|
|
631
|
+
host.style.cssText =
|
|
632
|
+
"all:initial;position:fixed;left:0;top:0;width:0;height:0;" +
|
|
633
|
+
"z-index:2147483647;pointer-events:none;" +
|
|
634
|
+
// Arrives and leaves under its own power. The agent taking a page over is
|
|
635
|
+
// an event worth seeing, and a cursor that pops into existence reads as a
|
|
636
|
+
// rendering glitch rather than as something showing up.
|
|
637
|
+
"opacity:0;transition:opacity 200ms ease;";
|
|
638
|
+
const shadow = host.attachShadow({ mode: "closed" });
|
|
639
|
+
shadow.innerHTML =
|
|
640
|
+
"<style>" +
|
|
641
|
+
// The cursor is anchored to the page, not the screen: it marks the thing
|
|
642
|
+
// the agent is working on, so it has to travel with that thing when the
|
|
643
|
+
// page scrolls. #layer carries the scroll offset with no transition of
|
|
644
|
+
// its own, leaving #pointer's transition free to animate agent movement —
|
|
645
|
+
// one element carrying both would make scrolling look rubbery.
|
|
646
|
+
"#layer{position:absolute;left:0;top:0}" +
|
|
647
|
+
// Marker bands, one per line of text, drawn in page coordinates so they
|
|
648
|
+
// stay on their words when the page scrolls. Each wipes in from its own
|
|
649
|
+
// left edge — a pen stroke, not a rectangle appearing.
|
|
650
|
+
"#bands{position:absolute;left:0;top:0}" +
|
|
651
|
+
".band{position:absolute;left:0;top:0;width:0;border-radius:3px;" +
|
|
652
|
+
"background:linear-gradient(180deg,rgba(217,119,87,.34),rgba(217,119,87,.22));" +
|
|
653
|
+
"box-shadow:0 0 0 1px rgba(217,119,87,.22);" +
|
|
654
|
+
"transition:width 260ms cubic-bezier(.33,.9,.5,1)}" +
|
|
655
|
+
// A read band is the same stroke, lighter and temporary: reading is
|
|
656
|
+
// constant, so its marks fade behind the cursor instead of piling up the
|
|
657
|
+
// way a deliberate highlight does.
|
|
658
|
+
".band.read{background:linear-gradient(180deg,rgba(74,158,255,.32),rgba(74,158,255,.16));" +
|
|
659
|
+
"box-shadow:none;transition-property:width,opacity;transition-timing-function:linear}" +
|
|
660
|
+
// While reading, every mark the overlay owns turns blue: the arrow, the
|
|
661
|
+
// ring, the badge's dot. The rules are scoped to #layer.reading so the
|
|
662
|
+
// whole scheme reverts in one class toggle when input arrives.
|
|
663
|
+
"#layer.reading #arrow path,#layer.reading #hand path{fill:" +
|
|
664
|
+
payload.readAccent +
|
|
665
|
+
"}" +
|
|
666
|
+
"#layer.reading #ring{border-color:" +
|
|
667
|
+
payload.readAccent +
|
|
668
|
+
";box-shadow:0 0 0 4px rgba(74,158,255,.18)}" +
|
|
669
|
+
"#layer.reading #dot{background:" +
|
|
670
|
+
payload.readAccent +
|
|
671
|
+
"}" +
|
|
672
|
+
// A label that no longer describes where the cursor is stays legible but
|
|
673
|
+
// stops competing with one that does — and its dot stops breathing,
|
|
674
|
+
// because breathing is what marks the badge as live.
|
|
675
|
+
"#badge.stale{opacity:.5}" +
|
|
676
|
+
"#badge.stale #dot{animation:none;opacity:.35}" +
|
|
677
|
+
"#pointer{position:absolute;left:0;top:0;" +
|
|
678
|
+
"transition:transform 200ms cubic-bezier(.22,.61,.36,1);will-change:transform}" +
|
|
679
|
+
"#ring{position:absolute;left:0;top:0;border:2px solid " +
|
|
680
|
+
payload.accent +
|
|
681
|
+
";border-radius:7px;box-shadow:0 0 0 4px rgba(217,119,87,.18);opacity:0;" +
|
|
682
|
+
"transition:opacity 160ms ease,transform 200ms cubic-bezier(.22,.61,.36,1)," +
|
|
683
|
+
"width 200ms ease,height 200ms ease}" +
|
|
684
|
+
"#ring.on{opacity:1}" +
|
|
685
|
+
"#arrow{position:absolute;left:-1px;top:-1px;display:block;" +
|
|
686
|
+
// Scaled about its own tip, so pressing squashes the arrow without
|
|
687
|
+
// moving the point it is aiming at. Down is fast and flat; the spring
|
|
688
|
+
// back overshoots, which is what makes it read as a button release.
|
|
689
|
+
"transform-origin:2px 2px;" +
|
|
690
|
+
"transition:transform 190ms cubic-bezier(.34,1.56,.64,1);" +
|
|
691
|
+
"filter:drop-shadow(0 2px 5px rgba(0,0,0,.35))}" +
|
|
692
|
+
"#pointer.press #arrow{transform:scale(.76);" +
|
|
693
|
+
"transition:transform 70ms cubic-bezier(.4,0,1,1)}" +
|
|
694
|
+
"#pulse{position:absolute;left:-19px;top:-19px;width:38px;height:38px;" +
|
|
695
|
+
"border-radius:50%;border:2px solid " +
|
|
696
|
+
payload.accent +
|
|
697
|
+
";opacity:0}" +
|
|
698
|
+
"#pulse.on{animation:ego-pulse 500ms ease-out}" +
|
|
699
|
+
"@keyframes ego-pulse{from{transform:scale(.2);opacity:.9}" +
|
|
700
|
+
"to{transform:scale(1);opacity:0}}" +
|
|
701
|
+
// Positioned by transform alone, in the page coordinates #layer works in.
|
|
702
|
+
"#badge{position:absolute;left:0;top:0;display:flex;align-items:center;" +
|
|
703
|
+
"gap:7px;max-width:300px;padding:5px 11px 5px 9px;border-radius:999px;" +
|
|
704
|
+
"background:rgba(24,24,27,.72);color:#fff;box-sizing:border-box;" +
|
|
705
|
+
// Frosted rather than flat: the badge sits over arbitrary pages, and one
|
|
706
|
+
// that lets its background through belongs to the page it is labelling
|
|
707
|
+
// instead of hovering over it as a second opaque card.
|
|
708
|
+
"-webkit-backdrop-filter:blur(14px) saturate(1.5);" +
|
|
709
|
+
"backdrop-filter:blur(14px) saturate(1.5);" +
|
|
710
|
+
"border:1px solid rgba(255,255,255,.09);letter-spacing:.01em;" +
|
|
711
|
+
"font:500 12px/1.35 ui-sans-serif,system-ui,-apple-system,'Segoe UI',sans-serif;" +
|
|
712
|
+
"box-shadow:0 8px 24px rgba(0,0,0,.28),0 1px 2px rgba(0,0,0,.2);" +
|
|
713
|
+
"transition:transform 200ms ease}" +
|
|
714
|
+
// Docked, the badge is holding still against a moving page: every scroll
|
|
715
|
+
// event rewrites its offset, and a transition would chase each one.
|
|
716
|
+
"#badge.docked{transition:none}" +
|
|
717
|
+
// Which way the cursor went, when it is no longer on screen to point at.
|
|
718
|
+
"#hint{flex:none;opacity:.7;font-size:11px}" +
|
|
719
|
+
"#hint:empty{display:none}" +
|
|
720
|
+
"#text{overflow:hidden;text-overflow:ellipsis;white-space:nowrap;" +
|
|
721
|
+
"animation:ego-fade 220ms ease}" +
|
|
722
|
+
"@keyframes ego-fade{from{opacity:.3}to{opacity:1}}" +
|
|
723
|
+
"#dot{flex:none;width:7px;height:7px;border-radius:50%;background:" +
|
|
724
|
+
payload.accent +
|
|
725
|
+
";animation:ego-breathe 1.7s ease-in-out infinite}" +
|
|
726
|
+
"@keyframes ego-breathe{0%,100%{opacity:1}50%{opacity:.35}}" +
|
|
727
|
+
// The reading glow. A sibling of #layer, not a child: #layer carries the
|
|
728
|
+
// scroll offset as a transform, and a transformed ancestor would make
|
|
729
|
+
// position:fixed resolve against it instead of the viewport — the glow
|
|
730
|
+
// would then scroll away from the edges it is meant to trace.
|
|
731
|
+
"#glow{position:fixed;left:0;top:0;right:0;bottom:0;opacity:0;" +
|
|
732
|
+
"transition:opacity 260ms ease;" +
|
|
733
|
+
"box-shadow:inset 0 0 0 2px rgba(74,158,255,.22)," +
|
|
734
|
+
"inset 0 0 52px rgba(74,158,255,.13)}" +
|
|
735
|
+
// Breathing at a slower beat than the badge dot, so the two read as one
|
|
736
|
+
// system rather than as two things blinking out of step.
|
|
737
|
+
"#glow.on{opacity:1;animation:ego-read-glow 2.4s ease-in-out infinite}" +
|
|
738
|
+
"@keyframes ego-read-glow{0%,100%{opacity:1}50%{opacity:.5}}" +
|
|
739
|
+
// The shape says what the cursor is over, the way a real one does: an
|
|
740
|
+
// arrow on the page, a hand on anything clickable, a beam over text.
|
|
741
|
+
"svg.shape{position:absolute;left:-1px;top:-1px;display:none}" +
|
|
742
|
+
"svg.shape.on{display:block}" +
|
|
743
|
+
"</style>" +
|
|
744
|
+
'<div id="layer">' +
|
|
745
|
+
'<div id="bands"></div>' +
|
|
746
|
+
'<div id="ring"></div>' +
|
|
747
|
+
'<div id="pointer">' +
|
|
748
|
+
'<div id="pulse"></div>' +
|
|
749
|
+
'<svg id="arrow" class="shape on" width="20" height="26" viewBox="-1.4 -1.4 17 24">' +
|
|
750
|
+
'<path d="M0 0 L0 18.6 L4.6 14.3 L7.7 21.1 L11.1 19.5 L8 12.9 L13.9 12.5 Z" ' +
|
|
751
|
+
'fill="' +
|
|
752
|
+
payload.accent +
|
|
753
|
+
'" stroke="#fff" stroke-width="1.5" stroke-linejoin="round"/></svg>' +
|
|
754
|
+
'<svg id="hand" class="shape" width="23" height="26" viewBox="-1.4 -1.4 20 24">' +
|
|
755
|
+
'<path d="M4.6 11.4 L4.6 2.4 A1.8 1.8 0 0 1 8.2 2.4 L8.2 9.4 L8.2 6.6 ' +
|
|
756
|
+
"A1.7 1.7 0 0 1 11.6 6.6 L11.6 9.4 L11.6 7.6 A1.7 1.7 0 0 1 15 7.6 L15 9.6 " +
|
|
757
|
+
"A1.7 1.7 0 0 1 18.4 9.9 L18.4 15.4 A5.6 5.6 0 0 1 12.8 21 L10.4 21 " +
|
|
758
|
+
'A5.4 5.4 0 0 1 5.6 18.1 L2.2 13.4 A1.7 1.7 0 0 1 4.6 11.4 Z" ' +
|
|
759
|
+
'fill="' +
|
|
760
|
+
payload.accent +
|
|
761
|
+
'" stroke="#fff" stroke-width="1.5" stroke-linejoin="round"/></svg>' +
|
|
762
|
+
'<svg id="beam" class="shape" width="14" height="26" viewBox="-1.4 -1.4 12 24">' +
|
|
763
|
+
'<path d="M1.6 1 H7.4 M4.5 1 V19.4 M1.6 19.4 H7.4" ' +
|
|
764
|
+
'stroke="#fff" stroke-width="4.4" stroke-linecap="round" fill="none"/>' +
|
|
765
|
+
'<path d="M1.6 1 H7.4 M4.5 1 V19.4 M1.6 19.4 H7.4" stroke="' +
|
|
766
|
+
payload.accent +
|
|
767
|
+
'" stroke-width="2" stroke-linecap="round" fill="none"/></svg>' +
|
|
768
|
+
"</div>" +
|
|
769
|
+
// A sibling of #pointer rather than a child of it. The badge has to stay
|
|
770
|
+
// readable exactly when the cursor has scrolled out of view, and a child
|
|
771
|
+
// of something parked far off screen is at the mercy of whether that
|
|
772
|
+
// layer gets drawn at all. Still inside #layer, though: everything the
|
|
773
|
+
// overlay draws shares one coordinate space, and a screenshot treats the
|
|
774
|
+
// whole subtree alike only while that stays true.
|
|
775
|
+
'<div id="badge"><span id="dot"></span><span id="hint"></span>' +
|
|
776
|
+
'<span id="text"></span></div>' +
|
|
777
|
+
"</div>" +
|
|
778
|
+
'<div id="glow"></div>';
|
|
779
|
+
host.__egoShadow = shadow;
|
|
780
|
+
|
|
781
|
+
// The page scrolls under the cursor between actions, and no CDP round trip
|
|
782
|
+
// announces that. Compensating in the page keeps it glued to its element.
|
|
783
|
+
const sync = () => {
|
|
784
|
+
const layer = shadow.getElementById("layer");
|
|
785
|
+
if (layer) {
|
|
786
|
+
layer.style.transform =
|
|
787
|
+
"translate3d(" + -window.scrollX + "px," + -window.scrollY + "px,0)";
|
|
788
|
+
}
|
|
789
|
+
// Staying glued to its element means riding that element off the screen,
|
|
790
|
+
// and a long scroll leaves the window showing nothing at all. The cursor
|
|
791
|
+
// keeps its place; the badge is what has to remain readable.
|
|
792
|
+
const at = host.__egoState;
|
|
793
|
+
if (at) placeBadge(at.pageX - window.scrollX, at.pageY - window.scrollY);
|
|
794
|
+
};
|
|
795
|
+
// hide() removes the host but leaves the realm standing, so a hide/show
|
|
796
|
+
// cycle would stack a listener per show, each closing over a dead root.
|
|
797
|
+
if (window.__egoCursorSync) {
|
|
798
|
+
window.removeEventListener("scroll", window.__egoCursorSync);
|
|
799
|
+
window.removeEventListener("resize", window.__egoCursorSync);
|
|
800
|
+
}
|
|
801
|
+
window.addEventListener("scroll", sync, { passive: true });
|
|
802
|
+
// A resize reflows the page under an overlay whose marks are held in page
|
|
803
|
+
// coordinates, and re-docks nothing on its own: the badge keeps measuring
|
|
804
|
+
// itself against the viewport it was placed in, so the whole overlay reads
|
|
805
|
+
// as slid away from what it points at. Retiling the window — switching to a
|
|
806
|
+
// dynamic or split layout — is the everyday way to hit it, and scrolling was
|
|
807
|
+
// the only event that used to put it right again.
|
|
808
|
+
window.addEventListener("resize", sync, { passive: true });
|
|
809
|
+
window.__egoCursorSync = sync;
|
|
810
|
+
host.__egoSync = sync;
|
|
811
|
+
parent.appendChild(host);
|
|
812
|
+
void host.offsetWidth; // let opacity:0 land, or there is nothing to fade from
|
|
813
|
+
host.style.opacity = "1";
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
const shadow = host.__egoShadow;
|
|
817
|
+
if (!shadow) return;
|
|
818
|
+
|
|
819
|
+
// Looked up before the first sync rather than where they are first drawn:
|
|
820
|
+
// sync() places the badge too, and it runs on the line below.
|
|
821
|
+
const badge = shadow.getElementById("badge");
|
|
822
|
+
const hint = shadow.getElementById("hint");
|
|
823
|
+
|
|
824
|
+
host.__egoSync?.();
|
|
825
|
+
|
|
826
|
+
// The harness speaks in viewport coordinates, but the cursor lives in page
|
|
827
|
+
// ones. Convert only when the viewport point actually changed: re-deriving it
|
|
828
|
+
// on a label-only render would walk the cursor across the page on every
|
|
829
|
+
// scroll, since the same viewport point is a different page point by then.
|
|
830
|
+
const previous = host.__egoState;
|
|
831
|
+
const moved = !previous || previous.x !== payload.x || previous.y !== payload.y;
|
|
832
|
+
let pageX = moved ? payload.x + window.scrollX : previous.pageX;
|
|
833
|
+
let pageY = moved ? payload.y + window.scrollY : previous.pageY;
|
|
834
|
+
|
|
835
|
+
// fill() focuses its target and inserts text without dispatching a single
|
|
836
|
+
// pointer event, so an agent can fill a whole form having never placed the
|
|
837
|
+
// cursor. Park it on the field it is typing into rather than at the origin.
|
|
838
|
+
if (!payload.placed) {
|
|
839
|
+
const target = focusedRect();
|
|
840
|
+
if (!target) return;
|
|
841
|
+
pageX = target.right + window.scrollX - 8;
|
|
842
|
+
pageY = target.bottom + window.scrollY - 6;
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
// Where it sits on screen right now, which is not payload.x/y once the page
|
|
846
|
+
// has scrolled under it.
|
|
847
|
+
const viewX = pageX - window.scrollX;
|
|
848
|
+
const viewY = pageY - window.scrollY;
|
|
849
|
+
|
|
850
|
+
// A read sweep drives the cursor from inside the page, line by line, so a
|
|
851
|
+
// render belonging to the same read must not drag it back to where the sweep
|
|
852
|
+
// began. Anything else — a click, a keystroke, the next read — ends it.
|
|
853
|
+
const pointer = shadow.getElementById("pointer");
|
|
854
|
+
const running = host.__egoSweep;
|
|
855
|
+
const readId = payload.read ? payload.read.id : 0;
|
|
856
|
+
const sweeping = Boolean(running) && running.id === readId && !running.done;
|
|
857
|
+
if (running && !running.done && !sweeping) {
|
|
858
|
+
// Cut off mid-line. Hand the pointer back with the timing it had before the
|
|
859
|
+
// sweep borrowed it, or the next real move animates like a read.
|
|
860
|
+
running.done = true;
|
|
861
|
+
pointer.style.transitionDuration = "";
|
|
862
|
+
pointer.style.transitionTimingFunction = "";
|
|
863
|
+
}
|
|
864
|
+
if (!sweeping) {
|
|
865
|
+
// One fixed duration for every move made a nudge between two fields crawl
|
|
866
|
+
// and a jump across the page look like a teleport. Scaling it to the
|
|
867
|
+
// distance is what a hand on a mouse does: near is quick, far takes a beat.
|
|
868
|
+
const far = previous ? Math.hypot(pageX - previous.pageX, pageY - previous.pageY) : 0;
|
|
869
|
+
pointer.style.transitionDuration =
|
|
870
|
+
Math.round(Math.min(420, Math.max(90, far * 0.45))) + "ms";
|
|
871
|
+
pointer.style.transform = "translate3d(" + pageX + "px," + pageY + "px,0)";
|
|
872
|
+
}
|
|
873
|
+
pointer.classList.toggle("press", Boolean(payload.pressed));
|
|
874
|
+
|
|
875
|
+
// What the cursor is over decides both its shape and, when the agent has not
|
|
876
|
+
// said what it is doing, what the badge reads. elementFromPoint is safe to
|
|
877
|
+
// ask here precisely because the overlay is pointer-events:none.
|
|
878
|
+
const under = document.elementFromPoint(viewX, viewY);
|
|
879
|
+
const subject = under
|
|
880
|
+
? under.closest("a,button,input,select,textarea,label,summary,[role]") ||
|
|
881
|
+
// Falling back to whatever is under the cursor is useful for a paragraph
|
|
882
|
+
// or an image. It is not for <html> or <body>: their textContent is the
|
|
883
|
+
// entire document, stylesheet text and all, so the badge would read
|
|
884
|
+
// "DeepSeek · Example Domainbody{background:#…". Resting the cursor in a
|
|
885
|
+
// corner after a navigation lands on exactly those, so describing them
|
|
886
|
+
// has to mean describing nothing — the stale label is the better answer.
|
|
887
|
+
(under.tagName === "HTML" || under.tagName === "BODY" ? null : under)
|
|
888
|
+
: null;
|
|
889
|
+
showShape(shadow, payload.typing ? "beam" : shapeFor(under));
|
|
890
|
+
|
|
891
|
+
// A label that still matches where the cursor is outranks everything. Once it
|
|
892
|
+
// goes stale it yields to a fresh description of what is actually under the
|
|
893
|
+
// cursor, and only comes back when there is nothing else to say — dimmed, to
|
|
894
|
+
// admit it is history. That ordering is what keeps the badge both permanent
|
|
895
|
+
// and honest: it never empties, and it never narrates the wrong thing.
|
|
896
|
+
const current = payload.note || (payload.labelStale ? "" : payload.label);
|
|
897
|
+
const nearby = describe(subject);
|
|
898
|
+
const showingStale =
|
|
899
|
+
!payload.typing && !current && !nearby && Boolean(payload.label);
|
|
900
|
+
const detail = payload.typing ? "typing…" : current || nearby || payload.label;
|
|
901
|
+
setBadgeText(detail ? payload.name + " · " + detail : payload.name);
|
|
902
|
+
|
|
903
|
+
// Reading owns the blue scheme; anything else hands it straight back. The
|
|
904
|
+
// read is what the harness cleared on the last real input, so this follows
|
|
905
|
+
// the same signal the sweep does rather than tracking a second one.
|
|
906
|
+
const readingNow = Boolean(payload.read) && !payload.typing;
|
|
907
|
+
shadow.getElementById("layer").classList.toggle("reading", readingNow);
|
|
908
|
+
// A host injected by an older build has no #glow, and the shim reuses whatever
|
|
909
|
+
// host it finds rather than rebuilding it. Reaching straight through would
|
|
910
|
+
// throw a TypeError that flush() silently swallows, leaving the cursor frozen
|
|
911
|
+
// on every page that was already open when the update landed. Missing glow
|
|
912
|
+
// just means no glow until that page next navigates.
|
|
913
|
+
const glow = shadow.getElementById("glow");
|
|
914
|
+
if (glow) glow.classList.toggle("on", readingNow);
|
|
915
|
+
// A reading label is always current — the sweep rewrites it per line — so the
|
|
916
|
+
// stale dimming only applies when the badge actually fell back to old text.
|
|
917
|
+
shadow
|
|
918
|
+
.getElementById("badge")
|
|
919
|
+
.classList.toggle("stale", showingStale && !readingNow);
|
|
920
|
+
|
|
921
|
+
placeBadge(viewX, viewY);
|
|
922
|
+
|
|
923
|
+
// A short trail of what happened, kept in the page for the same reason the
|
|
924
|
+
// cursor's position is: the Spaces panel runs in another process and this is
|
|
925
|
+
// the only state the two share. Recorded on transitions, so a burst of
|
|
926
|
+
// keystrokes is one entry rather than sixty.
|
|
927
|
+
const log = host.__egoLog || (host.__egoLog = []);
|
|
928
|
+
const remember = (text) => {
|
|
929
|
+
if (!text) return;
|
|
930
|
+
if (log.length && log[log.length - 1].text === text) return;
|
|
931
|
+
log.push({ text, at: Date.now() });
|
|
932
|
+
if (log.length > 6) log.shift();
|
|
933
|
+
};
|
|
934
|
+
if (payload.pulse) remember("clicked " + (describe(subject) || "the page"));
|
|
935
|
+
if (payload.typing && !previous?.typing) {
|
|
936
|
+
remember("typed into " + (describe(document.activeElement) || "a field"));
|
|
937
|
+
}
|
|
938
|
+
if (payload.highlight && payload.highlight.id !== previous?.highlightId) {
|
|
939
|
+
remember("highlighted " + (payload.note || "a passage"));
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
// Typing has no pointer to follow, so the field being typed into is what the
|
|
943
|
+
// overlay marks instead — otherwise text simply appears with nothing to say
|
|
944
|
+
// where it came from.
|
|
945
|
+
const ring = shadow.getElementById("ring");
|
|
946
|
+
const focused = payload.typing ? focusedRect() : null;
|
|
947
|
+
ring.classList.toggle("on", Boolean(focused));
|
|
948
|
+
if (focused) {
|
|
949
|
+
ring.style.width = focused.width + "px";
|
|
950
|
+
ring.style.height = focused.height + "px";
|
|
951
|
+
ring.style.transform =
|
|
952
|
+
"translate3d(" +
|
|
953
|
+
(focused.left + window.scrollX) +
|
|
954
|
+
"px," +
|
|
955
|
+
(focused.top + window.scrollY) +
|
|
956
|
+
"px,0)";
|
|
957
|
+
}
|
|
958
|
+
|
|
959
|
+
// Marker bands. Each is created at zero width and given its real width on the
|
|
960
|
+
// same frame, so the CSS transition draws it on rather than snapping it in.
|
|
961
|
+
// They are created only as the sweep reaches them, which is what makes the
|
|
962
|
+
// cursor look like it is doing the drawing.
|
|
963
|
+
const bands = shadow.getElementById("bands");
|
|
964
|
+
const marks = payload.highlight ? payload.highlight.rects : [];
|
|
965
|
+
if (!marks.length) {
|
|
966
|
+
// A sweep in flight puts its own bands in here; only a render that ended it
|
|
967
|
+
// is entitled to wipe them.
|
|
968
|
+
if (!sweeping) {
|
|
969
|
+
bands.replaceChildren();
|
|
970
|
+
bands.__egoId = 0;
|
|
971
|
+
}
|
|
972
|
+
} else {
|
|
973
|
+
// A new highlight replaces the old one. Without the generation check the
|
|
974
|
+
// previous stroke's bands would be reused for it, and the second highlight
|
|
975
|
+
// of a session would silently draw nothing.
|
|
976
|
+
if (bands.__egoId !== payload.highlight.id) {
|
|
977
|
+
bands.replaceChildren();
|
|
978
|
+
bands.__egoId = payload.highlight.id;
|
|
979
|
+
}
|
|
980
|
+
for (let index = 0; index < marks.length; index += 1) {
|
|
981
|
+
if (index > payload.highlight.upTo || bands.children[index]) continue;
|
|
982
|
+
const rect = marks[index];
|
|
983
|
+
const band = document.createElement("div");
|
|
984
|
+
band.className = "band";
|
|
985
|
+
band.style.height = rect.height + "px";
|
|
986
|
+
band.style.transform =
|
|
987
|
+
"translate3d(" + rect.x + "px," + rect.y + "px,0)";
|
|
988
|
+
band.style.transitionDuration = rect.ms + "ms";
|
|
989
|
+
bands.appendChild(band);
|
|
990
|
+
void band.offsetWidth; // let the zero width land before the real one
|
|
991
|
+
band.style.width = rect.width + "px";
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
|
|
995
|
+
if (payload.pulse) {
|
|
996
|
+
const pulse = shadow.getElementById("pulse");
|
|
997
|
+
pulse.classList.remove("on");
|
|
998
|
+
void pulse.offsetWidth; // restart the animation rather than ignore a re-click
|
|
999
|
+
pulse.classList.add("on");
|
|
1000
|
+
}
|
|
1001
|
+
|
|
1002
|
+
// Left for anything reading the page from outside — the Spaces overview uses
|
|
1003
|
+
// it to tell a space being worked in from one sitting idle.
|
|
1004
|
+
host.__egoState = {
|
|
1005
|
+
x: payload.x,
|
|
1006
|
+
y: payload.y,
|
|
1007
|
+
pageX,
|
|
1008
|
+
pageY,
|
|
1009
|
+
label: detail,
|
|
1010
|
+
name: payload.name,
|
|
1011
|
+
typing: Boolean(payload.typing),
|
|
1012
|
+
highlightId: payload.highlight ? payload.highlight.id : 0,
|
|
1013
|
+
at: Date.now(),
|
|
1014
|
+
};
|
|
1015
|
+
|
|
1016
|
+
// Last, so everything the sweep reaches for — the log, the badge, the state
|
|
1017
|
+
// it keeps fresh — already exists by the time it runs.
|
|
1018
|
+
if (readId && (!running || running.id !== readId)) startReadSweep(readId);
|
|
1019
|
+
|
|
1020
|
+
/**
|
|
1021
|
+
* Reading, made watchable.
|
|
1022
|
+
*
|
|
1023
|
+
* Snapshotting is most of what an agent does and it dispatches no input at
|
|
1024
|
+
* all, so a window where the agent was reading showed a cursor parked in a
|
|
1025
|
+
* corner under a badge that said "reading" — true, but never *what*. The
|
|
1026
|
+
* sweep walks the cursor along the lines that are actually on screen, marking
|
|
1027
|
+
* each as it passes and naming it in the badge, so someone watching can
|
|
1028
|
+
* follow what the agent took in.
|
|
1029
|
+
*
|
|
1030
|
+
* It runs entirely inside the page: a round trip per line would cost more
|
|
1031
|
+
* than the snapshot it illustrates, and the heredoc that triggered it has
|
|
1032
|
+
* usually exited before the last line is drawn. That also means it must give
|
|
1033
|
+
* up on its own — the sweep object is the only handle anything has on it.
|
|
1034
|
+
*/
|
|
1035
|
+
function startReadSweep(id) {
|
|
1036
|
+
const sweep = { id, done: false };
|
|
1037
|
+
host.__egoSweep = sweep;
|
|
1038
|
+
const lines = readableLines();
|
|
1039
|
+
if (!lines.length) {
|
|
1040
|
+
sweep.done = true;
|
|
1041
|
+
return;
|
|
1042
|
+
}
|
|
1043
|
+
remember("read " + snip(lines[0].text));
|
|
1044
|
+
|
|
1045
|
+
let index = 0;
|
|
1046
|
+
let spent = 0;
|
|
1047
|
+
|
|
1048
|
+
const step = () => {
|
|
1049
|
+
if (sweep.done) return;
|
|
1050
|
+
// A budget rather than a line count: reading is constant, and a sweep
|
|
1051
|
+
// still going when the agent acts again would only ever be interrupted.
|
|
1052
|
+
if (index >= lines.length || spent > 2400) {
|
|
1053
|
+
sweep.done = true;
|
|
1054
|
+
pointer.style.transitionDuration = "";
|
|
1055
|
+
pointer.style.transitionTimingFunction = "";
|
|
1056
|
+
return;
|
|
1057
|
+
}
|
|
1058
|
+
const line = lines[index];
|
|
1059
|
+
index += 1;
|
|
1060
|
+
const scanMs = Math.min(360, Math.max(130, Math.round(line.width * 0.8)));
|
|
1061
|
+
spent += scanMs + 110;
|
|
1062
|
+
const baseline = line.y + line.height - 2;
|
|
1063
|
+
|
|
1064
|
+
// Onto the start of the line first, then along it: the two legs are what
|
|
1065
|
+
// make it read as following the words rather than sliding to a spot.
|
|
1066
|
+
pointer.style.transitionTimingFunction = "ease-out";
|
|
1067
|
+
pointer.style.transitionDuration = "100ms";
|
|
1068
|
+
pointer.style.transform =
|
|
1069
|
+
"translate3d(" + line.x + "px," + baseline + "px,0)";
|
|
1070
|
+
setBadgeText(payload.name + " · reading “" + snip(line.text) + "”");
|
|
1071
|
+
placeBadge(line.x - window.scrollX, baseline - window.scrollY);
|
|
1072
|
+
|
|
1073
|
+
setTimeout(() => {
|
|
1074
|
+
if (sweep.done) return;
|
|
1075
|
+
pointer.style.transitionTimingFunction = "linear";
|
|
1076
|
+
pointer.style.transitionDuration = scanMs + "ms";
|
|
1077
|
+
pointer.style.transform =
|
|
1078
|
+
"translate3d(" + (line.x + line.width) + "px," + baseline + "px,0)";
|
|
1079
|
+
|
|
1080
|
+
const band = document.createElement("div");
|
|
1081
|
+
band.className = "band read";
|
|
1082
|
+
band.style.height = line.height + "px";
|
|
1083
|
+
band.style.transform = "translate3d(" + line.x + "px," + line.y + "px,0)";
|
|
1084
|
+
band.style.transitionDuration = scanMs + "ms";
|
|
1085
|
+
bands.appendChild(band);
|
|
1086
|
+
void band.offsetWidth; // let the zero width land before the real one
|
|
1087
|
+
band.style.width = line.width + "px";
|
|
1088
|
+
setTimeout(() => {
|
|
1089
|
+
band.style.opacity = "0";
|
|
1090
|
+
setTimeout(() => band.remove(), scanMs + 60);
|
|
1091
|
+
}, scanMs + 320);
|
|
1092
|
+
|
|
1093
|
+
// Keep the state the Spaces overview polls moving: a card whose agent is
|
|
1094
|
+
// reading should not look like a card whose agent walked away.
|
|
1095
|
+
host.__egoState.pageX = line.x + line.width;
|
|
1096
|
+
host.__egoState.pageY = baseline;
|
|
1097
|
+
host.__egoState.label = "reading " + snip(line.text);
|
|
1098
|
+
host.__egoState.at = Date.now();
|
|
1099
|
+
|
|
1100
|
+
setTimeout(step, scanMs + 10);
|
|
1101
|
+
}, 110);
|
|
1102
|
+
};
|
|
1103
|
+
|
|
1104
|
+
step();
|
|
1105
|
+
}
|
|
1106
|
+
|
|
1107
|
+
/**
|
|
1108
|
+
* The lines of text actually on screen, in reading order.
|
|
1109
|
+
*
|
|
1110
|
+
* Line boxes rather than elements: a Range hands back one rect per line, which
|
|
1111
|
+
* is what lets the cursor travel a paragraph the way a reader does. Only the
|
|
1112
|
+
* first few rects of any one node, so a long paragraph cannot spend the whole
|
|
1113
|
+
* sweep while the rest of the page goes unread.
|
|
1114
|
+
*/
|
|
1115
|
+
function readableLines() {
|
|
1116
|
+
const root = document.body;
|
|
1117
|
+
if (!root) return [];
|
|
1118
|
+
const found = [];
|
|
1119
|
+
// Measuring is what costs, and an agent scrolled halfway down a long page
|
|
1120
|
+
// has thousands of text nodes above it that can never be swept. Dropping a
|
|
1121
|
+
// container that is entirely off screen drops everything inside it in one
|
|
1122
|
+
// test — but only when it has a box of its own to judge by, since an empty
|
|
1123
|
+
// rect (display:contents, a zero-height wrapper) says nothing about the
|
|
1124
|
+
// children. The count is the backstop for what pruning cannot reach.
|
|
1125
|
+
let measured = 0;
|
|
1126
|
+
const walker = document.createTreeWalker(
|
|
1127
|
+
root,
|
|
1128
|
+
NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT,
|
|
1129
|
+
{
|
|
1130
|
+
acceptNode(node) {
|
|
1131
|
+
if (node.nodeType === 3) return NodeFilter.FILTER_ACCEPT;
|
|
1132
|
+
const box = node.getBoundingClientRect();
|
|
1133
|
+
const offScreen = box.bottom < 4 || box.top > window.innerHeight - 4;
|
|
1134
|
+
if (box.width > 0 && box.height > 0 && offScreen) {
|
|
1135
|
+
return NodeFilter.FILTER_REJECT;
|
|
1136
|
+
}
|
|
1137
|
+
return NodeFilter.FILTER_SKIP; // an element is not a line; its text is
|
|
1138
|
+
},
|
|
1139
|
+
},
|
|
1140
|
+
);
|
|
1141
|
+
while (found.length < 12 && measured < 400 && walker.nextNode()) {
|
|
1142
|
+
const node = walker.currentNode;
|
|
1143
|
+
const text = (node.nodeValue || "").replace(/\s+/g, " ").trim();
|
|
1144
|
+
if (text.length < 12) continue;
|
|
1145
|
+
const parent = node.parentElement;
|
|
1146
|
+
if (!parent || parent.closest("script,style,noscript,svg")) continue;
|
|
1147
|
+
measured += 1;
|
|
1148
|
+
const range = document.createRange();
|
|
1149
|
+
range.selectNodeContents(node);
|
|
1150
|
+
let taken = 0;
|
|
1151
|
+
for (const rect of range.getClientRects()) {
|
|
1152
|
+
if (taken >= 3 || found.length >= 12) break;
|
|
1153
|
+
if (rect.width < 80 || rect.height < 9 || rect.height > 96) continue;
|
|
1154
|
+
if (rect.bottom < 4 || rect.top > window.innerHeight - 4) continue;
|
|
1155
|
+
taken += 1;
|
|
1156
|
+
found.push({
|
|
1157
|
+
x: rect.left + window.scrollX,
|
|
1158
|
+
y: rect.top + window.scrollY,
|
|
1159
|
+
width: rect.width,
|
|
1160
|
+
height: rect.height,
|
|
1161
|
+
text,
|
|
1162
|
+
});
|
|
1163
|
+
}
|
|
1164
|
+
}
|
|
1165
|
+
// Document order is close to reading order but not the same thing under
|
|
1166
|
+
// absolute positioning, and a cursor that jumps back up the page reads as a
|
|
1167
|
+
// glitch rather than as reading.
|
|
1168
|
+
return found.sort((a, b) => a.y - b.y || a.x - b.x);
|
|
1169
|
+
}
|
|
1170
|
+
|
|
1171
|
+
/** Short enough for a badge that is drawn into every screenshot. */
|
|
1172
|
+
function snip(text) {
|
|
1173
|
+
const value = String(text || "").replace(/\s+/g, " ").trim();
|
|
1174
|
+
return value.length > 44 ? value.slice(0, 43) + "…" : value;
|
|
1175
|
+
}
|
|
1176
|
+
|
|
1177
|
+
/**
|
|
1178
|
+
* The label, swapped without a jump cut.
|
|
1179
|
+
*
|
|
1180
|
+
* Written synchronously — anything reading the badge sees the new text on the
|
|
1181
|
+
* next tick, and only the fade is deferred. The animation is restarted by hand
|
|
1182
|
+
* because a swap arriving mid-fade would otherwise inherit whatever opacity
|
|
1183
|
+
* the last one had reached and never brighten.
|
|
1184
|
+
*/
|
|
1185
|
+
function setBadgeText(next) {
|
|
1186
|
+
const text = shadow.getElementById("text");
|
|
1187
|
+
if (text.textContent === next) return;
|
|
1188
|
+
text.textContent = next;
|
|
1189
|
+
text.style.animation = "none";
|
|
1190
|
+
void text.offsetWidth;
|
|
1191
|
+
text.style.animation = "";
|
|
1192
|
+
}
|
|
1193
|
+
|
|
1194
|
+
/**
|
|
1195
|
+
* Where the label goes, in viewport coordinates: what matters is where the
|
|
1196
|
+
* cursor currently sits on screen, not where on the page it is marking.
|
|
1197
|
+
*
|
|
1198
|
+
* Near an edge the badge flips back over the cursor, so the label is never the
|
|
1199
|
+
* thing that gets clipped. Past the edge — the cursor scrolled out of view
|
|
1200
|
+
* entirely — it stops following and docks, because a label that leaves with
|
|
1201
|
+
* the cursor tells a watcher nothing about what is still happening.
|
|
1202
|
+
*/
|
|
1203
|
+
function placeBadge(atX, atY) {
|
|
1204
|
+
const outY = atY < 4 ? "↑" : atY > window.innerHeight - 4 ? "↓" : "";
|
|
1205
|
+
const outX = atX < 4 ? "←" : atX > window.innerWidth - 4 ? "→" : "";
|
|
1206
|
+
// Vertical first: pages scroll that way, so it is the direction that
|
|
1207
|
+
// actually carries the cursor off screen.
|
|
1208
|
+
const away = outY || outX;
|
|
1209
|
+
hint.textContent = away;
|
|
1210
|
+
|
|
1211
|
+
// #layer already carries -scroll, so a viewport target has to be written
|
|
1212
|
+
// back into page coordinates to survive it.
|
|
1213
|
+
const toPage = (x, y) =>
|
|
1214
|
+
"translate(" +
|
|
1215
|
+
Math.round(x + window.scrollX) +
|
|
1216
|
+
"px," +
|
|
1217
|
+
Math.round(y + window.scrollY) +
|
|
1218
|
+
"px)";
|
|
1219
|
+
|
|
1220
|
+
if (!away) {
|
|
1221
|
+
// Trailing the cursor at the offset it used to sit at as a child of it,
|
|
1222
|
+
// then flipped back over it near an edge so the label is never the thing
|
|
1223
|
+
// that gets clipped.
|
|
1224
|
+
badge.classList.remove("docked");
|
|
1225
|
+
badge.style.transform =
|
|
1226
|
+
toPage(atX + 20, atY + 22) +
|
|
1227
|
+
(atX + 340 > window.innerWidth ? " translateX(calc(-100% - 40px))" : "") +
|
|
1228
|
+
(atY + 70 > window.innerHeight ? " translateY(-60px)" : "");
|
|
1229
|
+
return;
|
|
1230
|
+
}
|
|
1231
|
+
|
|
1232
|
+
// Parked against the edge the cursor left by, and held there while the page
|
|
1233
|
+
// keeps moving under it.
|
|
1234
|
+
const width = badge.offsetWidth || 240;
|
|
1235
|
+
const dockX = Math.min(Math.max(atX, 12), Math.max(12, window.innerWidth - width - 12));
|
|
1236
|
+
const dockY = Math.min(Math.max(atY, 12), Math.max(12, window.innerHeight - 40));
|
|
1237
|
+
badge.classList.add("docked");
|
|
1238
|
+
badge.style.transform = toPage(dockX, dockY);
|
|
1239
|
+
}
|
|
1240
|
+
|
|
1241
|
+
/** The page's own cursor for this element is the honest source of shape. */
|
|
1242
|
+
function shapeFor(element) {
|
|
1243
|
+
if (!element) return "arrow";
|
|
1244
|
+
const style = getComputedStyle(element).cursor;
|
|
1245
|
+
if (style === "pointer") return "hand";
|
|
1246
|
+
if (style === "text" || style === "vertical-text") return "beam";
|
|
1247
|
+
return "arrow";
|
|
1248
|
+
}
|
|
1249
|
+
|
|
1250
|
+
function showShape(root, id) {
|
|
1251
|
+
for (const shape of root.querySelectorAll("svg.shape")) {
|
|
1252
|
+
shape.classList.toggle("on", shape.id === id);
|
|
1253
|
+
}
|
|
1254
|
+
}
|
|
1255
|
+
|
|
1256
|
+
/**
|
|
1257
|
+
* A short name for the thing under the cursor. Deliberately never reads an
|
|
1258
|
+
* input's value: the agent types passwords into these, and the badge is drawn
|
|
1259
|
+
* into every screenshot.
|
|
1260
|
+
*/
|
|
1261
|
+
function describe(element) {
|
|
1262
|
+
if (!element) return "";
|
|
1263
|
+
const candidates = [
|
|
1264
|
+
element.getAttribute("aria-label"),
|
|
1265
|
+
// A form control's own <label> beats its placeholder: "typed into Your
|
|
1266
|
+
// name" reads like a sentence, "typed into type a name" does not.
|
|
1267
|
+
element.labels?.[0]?.textContent,
|
|
1268
|
+
element.getAttribute("alt"),
|
|
1269
|
+
element.getAttribute("placeholder"),
|
|
1270
|
+
element.getAttribute("title"),
|
|
1271
|
+
element.tagName === "INPUT" || element.tagName === "TEXTAREA"
|
|
1272
|
+
? ""
|
|
1273
|
+
: element.textContent,
|
|
1274
|
+
];
|
|
1275
|
+
for (const candidate of candidates) {
|
|
1276
|
+
const text = String(candidate || "")
|
|
1277
|
+
.replace(/\s+/g, " ")
|
|
1278
|
+
.trim();
|
|
1279
|
+
if (text) return text.length > 40 ? text.slice(0, 39) + "…" : text;
|
|
1280
|
+
}
|
|
1281
|
+
return "";
|
|
1282
|
+
}
|
|
1283
|
+
|
|
1284
|
+
function focusedRect() {
|
|
1285
|
+
const active = document.activeElement;
|
|
1286
|
+
if (!active || active === document.body || active === document.documentElement) {
|
|
1287
|
+
return null;
|
|
1288
|
+
}
|
|
1289
|
+
const rect = active.getBoundingClientRect();
|
|
1290
|
+
return rect.width > 0 && rect.height > 0 ? rect : null;
|
|
1291
|
+
}
|
|
1292
|
+
}
|