townmouse 0.0.0-stage → 0.4.0
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/CHANGELOG.md +196 -0
- package/DESIGN.md +153 -0
- package/LICENSE +661 -0
- package/README.md +600 -2
- package/SECURITY.md +60 -0
- package/TOWNMOUSE.md +91 -0
- package/assets/brand/mascot/README.md +23 -0
- package/assets/brand/mascot/townmouse-aristocrat.png +0 -0
- package/assets/brand/mascot/townmouse-icon.svg +1 -0
- package/assets/brand/mascot/townmouse-monocled.png +0 -0
- package/bin/townmouse +68 -0
- package/docs/PACKAGING.md +371 -0
- package/docs/browser-workspace.md +237 -0
- package/docs/panes.md +442 -0
- package/native/TownmouseApp.swift +108 -0
- package/npm-shrinkwrap.json +1282 -0
- package/package.json +93 -4
- package/public/apple-touch-icon.png +0 -0
- package/public/brand/floydslabs-credit.png +0 -0
- package/public/brand/floydslabs-credit.webp +0 -0
- package/public/brand/townmouse-aristocrat.webp +0 -0
- package/public/brand/townmouse-monocled.webp +0 -0
- package/public/browser-picture.mjs +159 -0
- package/public/clipboard-text.mjs +5 -0
- package/public/favicon-16x16.png +0 -0
- package/public/favicon-32x32.png +0 -0
- package/public/favicon.ico +0 -0
- package/public/favicon.svg +1 -0
- package/public/features/app-version.mjs +35 -0
- package/public/features/autosave.mjs +80 -0
- package/public/features/clipboard.mjs +69 -0
- package/public/features/command-palette.mjs +181 -0
- package/public/features/desktop-palette.mjs +19 -0
- package/public/features/export.mjs +37 -0
- package/public/features/file-drop.mjs +77 -0
- package/public/features/floating-palette.mjs +113 -0
- package/public/features/fontsize.mjs +72 -0
- package/public/features/haptic-keys.mjs +42 -0
- package/public/features/history-undo.mjs +102 -0
- package/public/features/keepalive.mjs +36 -0
- package/public/features/keybar-config.mjs +70 -0
- package/public/features/keyboard-shortcuts.mjs +74 -0
- package/public/features/links.mjs +139 -0
- package/public/features/multi-window-sync.mjs +76 -0
- package/public/features/osc-bridge.mjs +64 -0
- package/public/features/phone-landscape-focus.mjs +157 -0
- package/public/features/recent-commands.mjs +100 -0
- package/public/features/search.mjs +134 -0
- package/public/features/session-lock.mjs +96 -0
- package/public/features/session-manager.mjs +251 -0
- package/public/features/settings-sync.mjs +77 -0
- package/public/features/shortcut-help.mjs +85 -0
- package/public/features/smart-completions.mjs +101 -0
- package/public/features/smart-suggest.mjs +124 -0
- package/public/features/snippets.mjs +85 -0
- package/public/features/split-view.mjs +102 -0
- package/public/features/status-metrics.mjs +137 -0
- package/public/features/stylus-support.mjs +124 -0
- package/public/features/swipe-history.mjs +141 -0
- package/public/features/swipe-sessions.mjs +85 -0
- package/public/features/tab-reorder.mjs +70 -0
- package/public/features/theme-system.mjs +51 -0
- package/public/features/toast-status.mjs +118 -0
- package/public/features/two-finger-menu.mjs +87 -0
- package/public/features/voice-input.mjs +244 -0
- package/public/features/wake-fullscreen.mjs +67 -0
- package/public/features/window-title.mjs +47 -0
- package/public/features/zoom.mjs +80 -0
- package/public/icon-128.png +0 -0
- package/public/icon-180.png +0 -0
- package/public/icon-192.png +0 -0
- package/public/icon-48.png +0 -0
- package/public/icon-512-maskable.png +0 -0
- package/public/icon-512.png +0 -0
- package/public/icon.svg +1 -0
- package/public/index.html +2304 -0
- package/public/input-chunks.mjs +22 -0
- package/public/input-guard.mjs +218 -0
- package/public/manifest.json +56 -0
- package/public/panes.css +1433 -0
- package/public/panes.html +211 -0
- package/public/panes.mjs +3183 -0
- package/public/service-worker.js +67 -0
- package/public/workspace.css +1621 -0
- package/public/workspace.mjs +1715 -0
- package/scripts/install-service.sh +303 -0
- package/scripts/node-runtime.sh +138 -0
- package/scripts/panes +568 -0
- package/scripts/prepare-native.sh +35 -0
- package/scripts/retire-legacy-service.sh +76 -0
- package/scripts/run-hooks +69 -0
- package/scripts/townmouse.sh +152 -0
- package/scripts/uninstall-service.sh +65 -0
- package/src/agent-guide.js +329 -0
- package/src/attachments.js +128 -0
- package/src/browser-dom.js +548 -0
- package/src/browser-workspace.js +1733 -0
- package/src/chrome-resolve.js +80 -0
- package/src/owner-page.js +43 -0
- package/src/pty-permissions.js +99 -0
- package/src/regex-worker.js +13 -0
- package/src/replay-text.js +189 -0
- package/src/request-boundary.js +58 -0
- package/src/safe-regex.js +87 -0
- package/src/server.js +1582 -0
- package/src/shell-resolve.js +42 -0
- package/src/static-deps.js +61 -0
- package/src/workspace-panes.js +1431 -0
|
@@ -0,0 +1,548 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Page-side helpers for the agent browser.
|
|
4
|
+
//
|
|
5
|
+
// Every function in this file that is prefixed `page...` is serialized by
|
|
6
|
+
// Puppeteer and therefore must stay self-contained: no closures over module
|
|
7
|
+
// variables, no `require`. Everything it needs travels in its argument object.
|
|
8
|
+
//
|
|
9
|
+
// Element identity is stamped into the page as `data-townmouse-ref`. That
|
|
10
|
+
// marker survives re-renders of unrelated parts of the page, works inside open
|
|
11
|
+
// shadow roots, and can be found again from any frame. It deliberately does not
|
|
12
|
+
// encode position, so a target does not silently point at whatever moved under
|
|
13
|
+
// old coordinates.
|
|
14
|
+
|
|
15
|
+
const REF_ATTRIBUTE = 'data-townmouse-ref';
|
|
16
|
+
|
|
17
|
+
function pageCatalog(options) {
|
|
18
|
+
const opts = options || {};
|
|
19
|
+
const MAX = Math.max(1, Math.min(500, Number(opts.max) || 200));
|
|
20
|
+
const INCLUDE_HIDDEN = Boolean(opts.includeHidden);
|
|
21
|
+
const MARK = 'data-townmouse-ref';
|
|
22
|
+
const start = Math.max(0, Number(opts.start) || 0);
|
|
23
|
+
|
|
24
|
+
const INTERACTIVE = new Set(['A', 'BUTTON', 'INPUT', 'SELECT', 'TEXTAREA', 'SUMMARY', 'OPTION', 'LABEL']);
|
|
25
|
+
const TEXTUAL = /^H[1-6]$/;
|
|
26
|
+
|
|
27
|
+
function walk(root, visit) {
|
|
28
|
+
if (!root) return;
|
|
29
|
+
const stack = [root];
|
|
30
|
+
const seen = new Set();
|
|
31
|
+
while (stack.length) {
|
|
32
|
+
const node = stack.pop();
|
|
33
|
+
if (!node || seen.has(node)) continue;
|
|
34
|
+
seen.add(node);
|
|
35
|
+
if (node.nodeType === 1) visit(node);
|
|
36
|
+
const children = node.children ? Array.from(node.children) : [];
|
|
37
|
+
// A shadow host's children are not rendered; the shadow tree is.
|
|
38
|
+
const shadow = node.nodeType === 1 ? node.shadowRoot : null;
|
|
39
|
+
const next = shadow ? [shadow] : children;
|
|
40
|
+
for (let index = next.length - 1; index >= 0; index--) stack.push(next[index]);
|
|
41
|
+
if (shadow && children.length) {
|
|
42
|
+
for (let index = children.length - 1; index >= 0; index--) stack.push(children[index]);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function labelFor(el) {
|
|
48
|
+
const aria = el.getAttribute && el.getAttribute('aria-label');
|
|
49
|
+
if (aria && aria.trim()) return aria.trim();
|
|
50
|
+
const labelledby = el.getAttribute && el.getAttribute('aria-labelledby');
|
|
51
|
+
if (labelledby) {
|
|
52
|
+
let scope = null;
|
|
53
|
+
try { scope = el.getRootNode(); } catch (_) { scope = null; }
|
|
54
|
+
const parts = String(labelledby).split(/\s+/).map((id) => {
|
|
55
|
+
if (scope && typeof scope.getElementById === 'function') return scope.getElementById(id);
|
|
56
|
+
const doc = el.ownerDocument;
|
|
57
|
+
return doc ? doc.getElementById(id) : null;
|
|
58
|
+
}).filter(Boolean);
|
|
59
|
+
const text = parts.map((part) => part.textContent || '').join(' ').replace(/\s+/g, ' ').trim();
|
|
60
|
+
if (text) return text;
|
|
61
|
+
}
|
|
62
|
+
if (el.labels && el.labels.length) {
|
|
63
|
+
const text = Array.from(el.labels).map((label) => label.textContent || '').join(' ').replace(/\s+/g, ' ').trim();
|
|
64
|
+
if (text) return text;
|
|
65
|
+
}
|
|
66
|
+
const placeholder = el.getAttribute && el.getAttribute('placeholder');
|
|
67
|
+
if (placeholder && placeholder.trim()) return placeholder.trim();
|
|
68
|
+
const alt = el.getAttribute && el.getAttribute('alt');
|
|
69
|
+
if (alt && alt.trim()) return alt.trim();
|
|
70
|
+
const title = el.getAttribute && el.getAttribute('title');
|
|
71
|
+
if (title && title.trim()) return title.trim();
|
|
72
|
+
if (el.tagName === 'INPUT' && el.type === 'submit' && el.value) return String(el.value);
|
|
73
|
+
const text = (el.innerText || el.textContent || '').replace(/\s+/g, ' ').trim();
|
|
74
|
+
return text.slice(0, 160);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function roleFor(el) {
|
|
78
|
+
const explicit = el.getAttribute && el.getAttribute('role');
|
|
79
|
+
if (explicit && explicit.trim()) return explicit.trim().split(/\s+/)[0];
|
|
80
|
+
const tag = el.tagName;
|
|
81
|
+
if (tag === 'A') return el.hasAttribute('href') ? 'link' : 'generic';
|
|
82
|
+
if (tag === 'BUTTON') return 'button';
|
|
83
|
+
if (tag === 'SELECT') return el.multiple ? 'listbox' : 'combobox';
|
|
84
|
+
if (tag === 'TEXTAREA') return 'textbox';
|
|
85
|
+
if (tag === 'IFRAME') return 'iframe';
|
|
86
|
+
if (tag === 'IMG') return 'image';
|
|
87
|
+
if (tag === 'SUMMARY') return 'button';
|
|
88
|
+
if (tag === 'OPTION') return 'option';
|
|
89
|
+
if (tag === 'LABEL') return 'label';
|
|
90
|
+
if (TEXTUAL.test(tag)) return 'heading';
|
|
91
|
+
if (tag === 'INPUT') {
|
|
92
|
+
const type = String(el.type || 'text').toLowerCase();
|
|
93
|
+
if (type === 'submit' || type === 'reset' || type === 'button' || type === 'image') return 'button';
|
|
94
|
+
if (type === 'checkbox' || type === 'radio') return type;
|
|
95
|
+
if (type === 'range') return 'slider';
|
|
96
|
+
if (type === 'file') return 'file';
|
|
97
|
+
if (type === 'color') return 'color';
|
|
98
|
+
if (['date', 'time', 'datetime-local', 'month', 'week'].includes(type)) return type;
|
|
99
|
+
return 'textbox';
|
|
100
|
+
}
|
|
101
|
+
if (el.isContentEditable) return 'textbox';
|
|
102
|
+
return 'generic';
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function interesting(el) {
|
|
106
|
+
const tag = el.tagName;
|
|
107
|
+
if (INTERACTIVE.has(tag)) return true;
|
|
108
|
+
if (tag === 'IFRAME' || tag === 'IMG') return true;
|
|
109
|
+
if (TEXTUAL.test(tag)) return true;
|
|
110
|
+
if (el.isContentEditable) return true;
|
|
111
|
+
if (el.hasAttribute('role') || el.hasAttribute('tabindex')) return true;
|
|
112
|
+
if (el.hasAttribute('onclick') || el.hasAttribute('aria-label') || el.hasAttribute('contenteditable')) return true;
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function boxFor(el) {
|
|
117
|
+
let rect;
|
|
118
|
+
try { rect = el.getBoundingClientRect(); } catch (_) { return null; }
|
|
119
|
+
return {
|
|
120
|
+
x: Math.round(rect.left * 10) / 10,
|
|
121
|
+
y: Math.round(rect.top * 10) / 10,
|
|
122
|
+
width: Math.round(rect.width * 10) / 10,
|
|
123
|
+
height: Math.round(rect.height * 10) / 10
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function visibleFor(el) {
|
|
128
|
+
let style;
|
|
129
|
+
try { style = getComputedStyle(el); } catch (_) { return false; }
|
|
130
|
+
if (!style || style.visibility === 'hidden' || style.display === 'none') return false;
|
|
131
|
+
if (Number(style.opacity || 1) === 0) return false;
|
|
132
|
+
const rect = el.getBoundingClientRect();
|
|
133
|
+
return rect.width > 0 && rect.height > 0;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const elements = [];
|
|
137
|
+
let counter = start;
|
|
138
|
+
walk(document, (el) => {
|
|
139
|
+
if (elements.length >= MAX) return;
|
|
140
|
+
if (!interesting(el)) return;
|
|
141
|
+
const visible = visibleFor(el);
|
|
142
|
+
if (!visible && !INCLUDE_HIDDEN) return;
|
|
143
|
+
const box = boxFor(el);
|
|
144
|
+
if (!box) return;
|
|
145
|
+
// Reuse an existing marker so a target keeps its identity across repeated
|
|
146
|
+
// catalogs. A fresh number is only minted for an element that never had one.
|
|
147
|
+
let existing = null;
|
|
148
|
+
try { existing = el.getAttribute(MARK); } catch (_) { existing = null; }
|
|
149
|
+
const ref = existing || `e${counter++}`;
|
|
150
|
+
if (!existing) {
|
|
151
|
+
try { el.setAttribute(MARK, ref); } catch (_) { /* detached or read-only host */ }
|
|
152
|
+
}
|
|
153
|
+
// A catalog is a read path: it is served to view-only grants. A password
|
|
154
|
+
// field's value must never leave the page through it; the image shows
|
|
155
|
+
// bullets and /content omits form values for the same reason. The input's
|
|
156
|
+
// type is reported so the server can hold the same line for every other
|
|
157
|
+
// path a control's value takes.
|
|
158
|
+
const isPassword = el.tagName === 'INPUT' && String(el.type || '').toLowerCase() === 'password';
|
|
159
|
+
const descriptor = {
|
|
160
|
+
ref,
|
|
161
|
+
role: roleFor(el),
|
|
162
|
+
tag: el.tagName.toLowerCase(),
|
|
163
|
+
type: el.tagName === 'INPUT' ? String(el.type || '').toLowerCase() : undefined,
|
|
164
|
+
name: labelFor(el),
|
|
165
|
+
box,
|
|
166
|
+
visible,
|
|
167
|
+
disabled: Boolean(el.disabled) || el.getAttribute('aria-disabled') === 'true',
|
|
168
|
+
checked: typeof el.checked === 'boolean' ? el.checked : null,
|
|
169
|
+
selected: el.tagName === 'OPTION' ? Boolean(el.selected) : null,
|
|
170
|
+
expanded: el.getAttribute('aria-expanded') === 'true'
|
|
171
|
+
? true
|
|
172
|
+
: (el.getAttribute('aria-expanded') === 'false' ? false : null),
|
|
173
|
+
required: Boolean(el.required) || el.getAttribute('aria-required') === 'true',
|
|
174
|
+
readonly: Boolean(el.readOnly) || el.getAttribute('aria-readonly') === 'true',
|
|
175
|
+
value: isPassword
|
|
176
|
+
? null
|
|
177
|
+
: (el.tagName === 'INPUT' || el.tagName === 'TEXTAREA' || el.tagName === 'SELECT')
|
|
178
|
+
? String(el.value == null ? '' : el.value).slice(0, 400)
|
|
179
|
+
: null,
|
|
180
|
+
redacted: isPassword || undefined
|
|
181
|
+
};
|
|
182
|
+
if (el.tagName === 'IFRAME') {
|
|
183
|
+
descriptor.role = 'iframe';
|
|
184
|
+
descriptor.name = el.getAttribute('title') || el.getAttribute('name') || el.src || 'iframe';
|
|
185
|
+
}
|
|
186
|
+
elements.push(descriptor);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
return {
|
|
190
|
+
elements,
|
|
191
|
+
nextRef: counter,
|
|
192
|
+
scrollX: Math.round(window.scrollX || 0),
|
|
193
|
+
scrollY: Math.round(window.scrollY || 0),
|
|
194
|
+
docWidth: Math.round(document.documentElement ? document.documentElement.scrollWidth : 0),
|
|
195
|
+
docHeight: Math.round(document.documentElement ? document.documentElement.scrollHeight : 0)
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// Same traversal, no stamping: a geometry snapshot used to decide whether a
|
|
200
|
+
// coordinate recorded against an observation still means anything.
|
|
201
|
+
function pageSignature(options) {
|
|
202
|
+
const opts = options || {};
|
|
203
|
+
const MAX = Math.max(1, Math.min(600, Number(opts.max) || 300));
|
|
204
|
+
const parts = [];
|
|
205
|
+
let count = 0;
|
|
206
|
+
const stack = [document];
|
|
207
|
+
const seen = new Set();
|
|
208
|
+
while (stack.length && count < MAX) {
|
|
209
|
+
const node = stack.pop();
|
|
210
|
+
if (!node || seen.has(node)) continue;
|
|
211
|
+
seen.add(node);
|
|
212
|
+
if (node.nodeType === 1) {
|
|
213
|
+
let rect = null;
|
|
214
|
+
try { rect = node.getBoundingClientRect(); } catch (_) { rect = null; }
|
|
215
|
+
let style = null;
|
|
216
|
+
try { style = getComputedStyle(node); } catch (_) { style = null; }
|
|
217
|
+
if (rect && style && style.display !== 'none' && style.visibility !== 'hidden' && rect.width > 0 && rect.height > 0) {
|
|
218
|
+
parts.push(`${node.tagName}:${Math.round(rect.left)},${Math.round(rect.top)},${Math.round(rect.width)},${Math.round(rect.height)}`);
|
|
219
|
+
count++;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
const children = node.children ? Array.from(node.children) : [];
|
|
223
|
+
const shadow = node.nodeType === 1 && node.shadowRoot ? node.shadowRoot : null;
|
|
224
|
+
if (shadow) stack.push(shadow);
|
|
225
|
+
for (let index = children.length - 1; index >= 0; index--) stack.push(children[index]);
|
|
226
|
+
}
|
|
227
|
+
return {
|
|
228
|
+
signature: parts.join('|'),
|
|
229
|
+
scrollX: Math.round(window.scrollX || 0),
|
|
230
|
+
scrollY: Math.round(window.scrollY || 0),
|
|
231
|
+
url: location.href,
|
|
232
|
+
title: document.title || ''
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// Resolve a stamped marker to a real element, piercing open shadow roots.
|
|
237
|
+
function pageResolve(marker) {
|
|
238
|
+
const MARK = 'data-townmouse-ref';
|
|
239
|
+
const stack = [document];
|
|
240
|
+
const seen = new Set();
|
|
241
|
+
while (stack.length) {
|
|
242
|
+
const node = stack.pop();
|
|
243
|
+
if (!node || seen.has(node)) continue;
|
|
244
|
+
seen.add(node);
|
|
245
|
+
if (node.nodeType === 1 && node.getAttribute && node.getAttribute(MARK) === marker) return node;
|
|
246
|
+
// Descend below the document node too: `document` is not an Element, but it
|
|
247
|
+
// still has children.
|
|
248
|
+
if (node.shadowRoot) stack.push(node.shadowRoot);
|
|
249
|
+
const children = node.children ? Array.from(node.children) : [];
|
|
250
|
+
for (let index = children.length - 1; index >= 0; index--) stack.push(children[index]);
|
|
251
|
+
}
|
|
252
|
+
return null;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function pageFocusInfo() {
|
|
256
|
+
const el = document.activeElement;
|
|
257
|
+
if (!el) return null;
|
|
258
|
+
const info = {
|
|
259
|
+
role: (el.getAttribute && el.getAttribute('role')) || el.tagName.toLowerCase(),
|
|
260
|
+
name: (el.getAttribute && (el.getAttribute('aria-label') || el.getAttribute('placeholder')))
|
|
261
|
+
|| (el.labels && el.labels[0] ? el.labels[0].textContent : '')
|
|
262
|
+
|| '',
|
|
263
|
+
tag: el.tagName.toLowerCase()
|
|
264
|
+
};
|
|
265
|
+
if (el.tagName === 'INPUT' || el.tagName === 'TEXTAREA' || el.tagName === 'SELECT') {
|
|
266
|
+
info.value = String(el.value == null ? '' : el.value).slice(0, 400);
|
|
267
|
+
info.type = String(el.type || '').toLowerCase();
|
|
268
|
+
}
|
|
269
|
+
return info;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
function pageScroll(options) {
|
|
273
|
+
const opts = options || {};
|
|
274
|
+
const MARK = 'data-townmouse-ref';
|
|
275
|
+
const dx = Number(opts.dx) || 0;
|
|
276
|
+
const dy = Number(opts.dy) || 0;
|
|
277
|
+
|
|
278
|
+
// Inlined on purpose: this function is serialized alone into the page, so it
|
|
279
|
+
// cannot call a module-level helper.
|
|
280
|
+
function resolve(marker) {
|
|
281
|
+
const stack = [document];
|
|
282
|
+
const seen = new Set();
|
|
283
|
+
while (stack.length) {
|
|
284
|
+
const node = stack.pop();
|
|
285
|
+
if (!node || seen.has(node)) continue;
|
|
286
|
+
seen.add(node);
|
|
287
|
+
if (node.nodeType === 1 && node.getAttribute && node.getAttribute(MARK) === marker) return node;
|
|
288
|
+
if (node.shadowRoot) stack.push(node.shadowRoot);
|
|
289
|
+
const children = node.children ? Array.from(node.children) : [];
|
|
290
|
+
for (let index = children.length - 1; index >= 0; index--) stack.push(children[index]);
|
|
291
|
+
}
|
|
292
|
+
return null;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
function deepestScrollable(start) {
|
|
296
|
+
let node = start;
|
|
297
|
+
while (node && node !== document.body && node !== document.documentElement) {
|
|
298
|
+
let style = null;
|
|
299
|
+
try { style = getComputedStyle(node); } catch (_) { style = null; }
|
|
300
|
+
if (style) {
|
|
301
|
+
const scrollableY = /(auto|scroll|overlay)/.test(style.overflowY) && node.scrollHeight > node.clientHeight + 1;
|
|
302
|
+
const scrollableX = /(auto|scroll|overlay)/.test(style.overflowX) && node.scrollWidth > node.clientWidth + 1;
|
|
303
|
+
if (scrollableY || scrollableX) return node;
|
|
304
|
+
}
|
|
305
|
+
node = node.parentElement || (node.getRootNode && node.getRootNode().host) || null;
|
|
306
|
+
}
|
|
307
|
+
return null;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
let target = null;
|
|
311
|
+
if (opts.marker) {
|
|
312
|
+
target = resolve(opts.marker);
|
|
313
|
+
if (!target) return { found: false, reason: 'missing' };
|
|
314
|
+
} else if (opts.selector) {
|
|
315
|
+
try { target = document.querySelector(opts.selector); } catch (_) { target = null; }
|
|
316
|
+
if (!target) return { found: false, reason: 'missing' };
|
|
317
|
+
} else {
|
|
318
|
+
target = document.scrollingElement || document.documentElement;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
const scroller = (opts.marker || opts.selector)
|
|
322
|
+
? (deepestScrollable(target) || document.scrollingElement || document.documentElement)
|
|
323
|
+
: (document.scrollingElement || document.documentElement);
|
|
324
|
+
|
|
325
|
+
const before = { x: scroller.scrollLeft, y: scroller.scrollTop };
|
|
326
|
+
scroller.scrollBy({ left: dx, top: dy, behavior: 'instant' });
|
|
327
|
+
const after = { x: scroller.scrollLeft, y: scroller.scrollTop };
|
|
328
|
+
return {
|
|
329
|
+
found: true,
|
|
330
|
+
scroller: scroller === (document.scrollingElement || document.documentElement)
|
|
331
|
+
? 'page'
|
|
332
|
+
: (scroller.tagName.toLowerCase() + (scroller.id ? '#' + scroller.id : '')),
|
|
333
|
+
before,
|
|
334
|
+
after,
|
|
335
|
+
moved: before.x !== after.x || before.y !== after.y,
|
|
336
|
+
scrollX: Math.round(window.scrollX || 0),
|
|
337
|
+
scrollY: Math.round(window.scrollY || 0)
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
function pageSnapshotText(options) {
|
|
342
|
+
const opts = options || {};
|
|
343
|
+
const limit = Math.max(1000, Math.min(2000000, Number(opts.limit) || 2000000));
|
|
344
|
+
const text = (document.body ? document.body.innerText : '') || '';
|
|
345
|
+
const total = text.length;
|
|
346
|
+
const truncated = total > limit;
|
|
347
|
+
return {
|
|
348
|
+
text: truncated ? text.slice(0, limit) : text,
|
|
349
|
+
totalLength: total,
|
|
350
|
+
truncated,
|
|
351
|
+
title: document.title || '',
|
|
352
|
+
url: location.href
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
// ── Node-side drivers ───────────────────────────────────────────────────────
|
|
357
|
+
|
|
358
|
+
const crypto = require('crypto');
|
|
359
|
+
|
|
360
|
+
function hashSignature(value) {
|
|
361
|
+
return crypto.createHash('sha1').update(String(value || '')).digest('hex').slice(0, 16);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Catalog the page across every frame, including cross-origin iframes.
|
|
366
|
+
* Returns element descriptors with a globally unique `ref` and the frame that
|
|
367
|
+
* owns each one.
|
|
368
|
+
*
|
|
369
|
+
* @param {import('puppeteer').Page} page
|
|
370
|
+
*/
|
|
371
|
+
async function catalog(page, options = {}) {
|
|
372
|
+
const frames = page.frames();
|
|
373
|
+
const max = Math.max(1, Math.min(500, Number(options.max) || 200));
|
|
374
|
+
const includeHidden = Boolean(options.includeHidden);
|
|
375
|
+
const elements = [];
|
|
376
|
+
let next = Math.max(0, Number(options.start) || 0);
|
|
377
|
+
let frameIndex = 0;
|
|
378
|
+
for (const frame of frames) {
|
|
379
|
+
if (elements.length >= max) break;
|
|
380
|
+
const remaining = max - elements.length;
|
|
381
|
+
let result;
|
|
382
|
+
try {
|
|
383
|
+
result = await frame.evaluate(pageCatalog, { max: remaining, includeHidden, start: next });
|
|
384
|
+
} catch (_) {
|
|
385
|
+
// A frame can detach mid-walk (navigation, removed iframe). Skip it; the
|
|
386
|
+
// rest of the page is still catalogued.
|
|
387
|
+
frameIndex++;
|
|
388
|
+
continue;
|
|
389
|
+
}
|
|
390
|
+
next = Number(result.nextRef) || next;
|
|
391
|
+
for (const element of result.elements) {
|
|
392
|
+
elements.push({
|
|
393
|
+
...element,
|
|
394
|
+
frame: frameIndex,
|
|
395
|
+
frameUrl: frame.url(),
|
|
396
|
+
inFrame: frameIndex > 0
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
frameIndex++;
|
|
400
|
+
}
|
|
401
|
+
return { elements, nextRef: next, frameCount: frames.length };
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* Resolve a ref to a live element handle. Ref numbers are only ever resolved by
|
|
406
|
+
* the marker stamped into the DOM, never by index or coordinate.
|
|
407
|
+
*/
|
|
408
|
+
async function resolveRef(page, ref) {
|
|
409
|
+
if (!ref || typeof ref !== 'string') return null;
|
|
410
|
+
for (const frame of page.frames()) {
|
|
411
|
+
let handle;
|
|
412
|
+
try { handle = await frame.evaluateHandle(pageResolve, ref); } catch (_) { continue; }
|
|
413
|
+
if (!handle) continue;
|
|
414
|
+
const element = handle.asElement();
|
|
415
|
+
if (element) return element;
|
|
416
|
+
await handle.dispose().catch(() => {});
|
|
417
|
+
}
|
|
418
|
+
return null;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
// Cheap position read for a picture taken for a viewer on request. The full layout
|
|
422
|
+
// signature is only needed when deciding whether a target is stale.
|
|
423
|
+
async function scrollPosition(page) {
|
|
424
|
+
try {
|
|
425
|
+
const result = await page.mainFrame().evaluate(() => ({
|
|
426
|
+
scrollX: Math.round(window.scrollX || 0),
|
|
427
|
+
scrollY: Math.round(window.scrollY || 0)
|
|
428
|
+
}));
|
|
429
|
+
return { scrollX: result.scrollX, scrollY: result.scrollY };
|
|
430
|
+
} catch (_) {
|
|
431
|
+
return { scrollX: 0, scrollY: 0 };
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
async function signature(page) {
|
|
436
|
+
// Every frame contributes, because a click inside an iframe is invalidated
|
|
437
|
+
// by that iframe scrolling or rearranging even when the outer page is still.
|
|
438
|
+
const parts = [];
|
|
439
|
+
let main = null;
|
|
440
|
+
for (const frame of page.frames()) {
|
|
441
|
+
let result;
|
|
442
|
+
try { result = await frame.evaluate(pageSignature, {}); } catch (_) { continue; }
|
|
443
|
+
if (!main) main = result; // page.frames() lists the main frame first
|
|
444
|
+
parts.push(`${frame.url()}|${result.scrollX},${result.scrollY}|${result.signature}`);
|
|
445
|
+
}
|
|
446
|
+
if (!main) return { hash: '', scrollX: 0, scrollY: 0, url: '', title: '' };
|
|
447
|
+
return {
|
|
448
|
+
hash: hashSignature(parts.join('||')),
|
|
449
|
+
scrollX: main.scrollX,
|
|
450
|
+
scrollY: main.scrollY,
|
|
451
|
+
url: main.url,
|
|
452
|
+
title: main.title
|
|
453
|
+
};
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
async function focusInfo(page) {
|
|
457
|
+
try { return await page.mainFrame().evaluate(pageFocusInfo, {}); }
|
|
458
|
+
catch (_) { return null; }
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
async function scroll(page, options) {
|
|
462
|
+
// Scroll the frame that actually owns the target, so a list inside an iframe
|
|
463
|
+
// scrolls without moving the outer page.
|
|
464
|
+
for (const frame of page.frames()) {
|
|
465
|
+
let exists = false;
|
|
466
|
+
try {
|
|
467
|
+
exists = await frame.evaluate(
|
|
468
|
+
(wanted) => {
|
|
469
|
+
if (wanted.marker) {
|
|
470
|
+
const MARK = 'data-townmouse-ref';
|
|
471
|
+
const stack = [document];
|
|
472
|
+
const seen = new Set();
|
|
473
|
+
while (stack.length) {
|
|
474
|
+
const node = stack.pop();
|
|
475
|
+
if (!node || seen.has(node)) continue;
|
|
476
|
+
seen.add(node);
|
|
477
|
+
if (node.nodeType === 1 && node.getAttribute && node.getAttribute(MARK) === wanted.marker) return true;
|
|
478
|
+
if (node.shadowRoot) stack.push(node.shadowRoot);
|
|
479
|
+
const children = node.children ? Array.from(node.children) : [];
|
|
480
|
+
for (let index = children.length - 1; index >= 0; index--) stack.push(children[index]);
|
|
481
|
+
}
|
|
482
|
+
return false;
|
|
483
|
+
}
|
|
484
|
+
try { return Boolean(document.querySelector(wanted.selector)); } catch (_) { return false; }
|
|
485
|
+
},
|
|
486
|
+
{ marker: options.marker || null, selector: options.selector || null }
|
|
487
|
+
);
|
|
488
|
+
} catch (_) {
|
|
489
|
+
exists = false;
|
|
490
|
+
}
|
|
491
|
+
if (!exists) continue;
|
|
492
|
+
try {
|
|
493
|
+
return await frame.evaluate(pageScroll, {
|
|
494
|
+
marker: options.marker,
|
|
495
|
+
selector: options.selector,
|
|
496
|
+
dx: options.dx,
|
|
497
|
+
dy: options.dy
|
|
498
|
+
});
|
|
499
|
+
} catch (error) {
|
|
500
|
+
return { found: false, reason: 'failed', error: error.message };
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
return { found: false, reason: 'missing' };
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
/**
|
|
507
|
+
* Read the visible text of a page without silently truncating it. The caller
|
|
508
|
+
* gets an explicit flag when the text was cut.
|
|
509
|
+
*/
|
|
510
|
+
async function content(page, limit = 200000) {
|
|
511
|
+
const frames = [];
|
|
512
|
+
for (const frame of page.frames()) {
|
|
513
|
+
try {
|
|
514
|
+
const part = await frame.evaluate(pageSnapshotText, { limit });
|
|
515
|
+
frames.push({ url: frame.url(), text: part.text, totalLength: part.totalLength, truncated: part.truncated });
|
|
516
|
+
} catch (_) { /* frame detached mid-read */ }
|
|
517
|
+
}
|
|
518
|
+
const text = frames.map((part) => part.text).filter(Boolean).join('\n\n');
|
|
519
|
+
const totalLength = frames.reduce((sum, part) => sum + part.totalLength, 0);
|
|
520
|
+
let title = '';
|
|
521
|
+
try { title = await page.title(); } catch (_) { title = ''; }
|
|
522
|
+
return {
|
|
523
|
+
text: text.slice(0, limit),
|
|
524
|
+
totalLength,
|
|
525
|
+
truncated: frames.some((part) => part.truncated) || text.length > limit,
|
|
526
|
+
frameCount: frames.length,
|
|
527
|
+
frames: frames.map((part) => ({ url: part.url, length: part.totalLength, truncated: part.truncated })),
|
|
528
|
+
title,
|
|
529
|
+
url: page.url()
|
|
530
|
+
};
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
module.exports = {
|
|
534
|
+
REF_ATTRIBUTE,
|
|
535
|
+
catalog,
|
|
536
|
+
resolveRef,
|
|
537
|
+
signature,
|
|
538
|
+
scrollPosition,
|
|
539
|
+
focusInfo,
|
|
540
|
+
scroll,
|
|
541
|
+
content,
|
|
542
|
+
pageCatalog,
|
|
543
|
+
pageSignature,
|
|
544
|
+
pageResolve,
|
|
545
|
+
pageScroll,
|
|
546
|
+
pageSnapshotText,
|
|
547
|
+
pageFocusInfo
|
|
548
|
+
};
|