lobster-cli 0.1.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.
Files changed (45) hide show
  1. package/README.md +389 -0
  2. package/dist/agent/core.js +1013 -0
  3. package/dist/agent/core.js.map +1 -0
  4. package/dist/agent/index.js +1027 -0
  5. package/dist/agent/index.js.map +1 -0
  6. package/dist/brain/index.js +60 -0
  7. package/dist/brain/index.js.map +1 -0
  8. package/dist/browser/dom/index.js +1096 -0
  9. package/dist/browser/dom/index.js.map +1 -0
  10. package/dist/browser/index.js +2034 -0
  11. package/dist/browser/index.js.map +1 -0
  12. package/dist/browser/manager.js +86 -0
  13. package/dist/browser/manager.js.map +1 -0
  14. package/dist/browser/page-adapter.js +1345 -0
  15. package/dist/browser/page-adapter.js.map +1 -0
  16. package/dist/cascade/index.js +138 -0
  17. package/dist/cascade/index.js.map +1 -0
  18. package/dist/config/index.js +110 -0
  19. package/dist/config/index.js.map +1 -0
  20. package/dist/config/schema.js +66 -0
  21. package/dist/config/schema.js.map +1 -0
  22. package/dist/discover/index.js +545 -0
  23. package/dist/discover/index.js.map +1 -0
  24. package/dist/index.js +5529 -0
  25. package/dist/index.js.map +1 -0
  26. package/dist/lib.js +4206 -0
  27. package/dist/lib.js.map +1 -0
  28. package/dist/llm/client.js +379 -0
  29. package/dist/llm/client.js.map +1 -0
  30. package/dist/llm/index.js +397 -0
  31. package/dist/llm/index.js.map +1 -0
  32. package/dist/llm/openai-client.js +214 -0
  33. package/dist/llm/openai-client.js.map +1 -0
  34. package/dist/output/index.js +93 -0
  35. package/dist/output/index.js.map +1 -0
  36. package/dist/pipeline/index.js +802 -0
  37. package/dist/pipeline/index.js.map +1 -0
  38. package/dist/router/decision.js +80 -0
  39. package/dist/router/decision.js.map +1 -0
  40. package/dist/router/index.js +3443 -0
  41. package/dist/router/index.js.map +1 -0
  42. package/dist/types/index.js +23 -0
  43. package/dist/types/index.js.map +1 -0
  44. package/logo.svg +11 -0
  45. package/package.json +65 -0
@@ -0,0 +1,1345 @@
1
+ // src/browser/dom/flat-tree.ts
2
+ var FLAT_TREE_SCRIPT = `
3
+ (() => {
4
+ const INTERACTIVE_TAGS = new Set([
5
+ 'a', 'button', 'input', 'select', 'textarea', 'details', 'summary',
6
+ 'label', 'option', 'fieldset', 'legend',
7
+ ]);
8
+
9
+ const INTERACTIVE_ROLES = new Set([
10
+ 'button', 'link', 'textbox', 'checkbox', 'radio', 'combobox',
11
+ 'listbox', 'menu', 'menuitem', 'tab', 'switch', 'slider',
12
+ 'searchbox', 'spinbutton', 'option', 'menuitemcheckbox', 'menuitemradio',
13
+ ]);
14
+
15
+ const ATTR_WHITELIST = [
16
+ 'type', 'role', 'aria-label', 'aria-expanded', 'aria-selected',
17
+ 'aria-checked', 'aria-disabled', 'placeholder', 'title', 'href',
18
+ 'value', 'name', 'alt', 'src',
19
+ ];
20
+
21
+ let highlightIndex = 0;
22
+ const nodes = {};
23
+ const selectorMap = {};
24
+
25
+ function isVisible(el) {
26
+ if (el.offsetWidth === 0 && el.offsetHeight === 0) return false;
27
+ const style = getComputedStyle(el);
28
+ if (style.display === 'none' || style.visibility === 'hidden' || style.opacity === '0') return false;
29
+ return true;
30
+ }
31
+
32
+ function isInteractive(el) {
33
+ const tag = el.tagName.toLowerCase();
34
+ if (INTERACTIVE_TAGS.has(tag)) return true;
35
+ const role = el.getAttribute('role');
36
+ if (role && INTERACTIVE_ROLES.has(role)) return true;
37
+ if (el.getAttribute('contenteditable') === 'true') return true;
38
+ if (el.getAttribute('tabindex') !== null && parseInt(el.getAttribute('tabindex')) >= 0) return true;
39
+ if (el.onclick || el.getAttribute('onclick')) return true;
40
+ return false;
41
+ }
42
+
43
+ function getAttributes(el) {
44
+ const attrs = {};
45
+ for (const attr of ATTR_WHITELIST) {
46
+ const val = el.getAttribute(attr);
47
+ if (val !== null && val !== '') attrs[attr] = val;
48
+ }
49
+ return attrs;
50
+ }
51
+
52
+ function getScrollable(el) {
53
+ const style = getComputedStyle(el);
54
+ const overflowY = style.overflowY;
55
+ const overflowX = style.overflowX;
56
+ const isScrollableY = (overflowY === 'auto' || overflowY === 'scroll') && el.scrollHeight > el.clientHeight;
57
+ const isScrollableX = (overflowX === 'auto' || overflowX === 'scroll') && el.scrollWidth > el.clientWidth;
58
+ if (!isScrollableY && !isScrollableX) return null;
59
+ return {
60
+ left: el.scrollLeft,
61
+ top: el.scrollTop,
62
+ right: el.scrollWidth - el.clientWidth - el.scrollLeft,
63
+ bottom: el.scrollHeight - el.clientHeight - el.scrollTop,
64
+ };
65
+ }
66
+
67
+ function walk(el, parentId) {
68
+ if (!el || el.nodeType === 8) return; // skip comments
69
+
70
+ if (el.nodeType === 3) { // text node
71
+ const text = el.textContent.trim();
72
+ if (!text) return;
73
+ const id = 'text_' + Math.random().toString(36).slice(2, 8);
74
+ nodes[id] = { id, tagName: '#text', text, parentId };
75
+ if (parentId && nodes[parentId]) {
76
+ nodes[parentId].children = nodes[parentId].children || [];
77
+ nodes[parentId].children.push(id);
78
+ }
79
+ return;
80
+ }
81
+
82
+ if (el.nodeType !== 1) return; // only elements
83
+
84
+ const tag = el.tagName.toLowerCase();
85
+ if (['script', 'style', 'noscript', 'svg', 'path'].includes(tag)) return;
86
+ if (!isVisible(el)) return;
87
+
88
+ const id = tag + '_' + Math.random().toString(36).slice(2, 8);
89
+ const interactive = isInteractive(el);
90
+ const node = {
91
+ id,
92
+ tagName: tag,
93
+ attributes: getAttributes(el),
94
+ parentId,
95
+ children: [],
96
+ isInteractive: interactive,
97
+ };
98
+
99
+ if (interactive) {
100
+ node.highlightIndex = highlightIndex;
101
+ selectorMap[highlightIndex] = id;
102
+ highlightIndex++;
103
+ }
104
+
105
+ const scrollable = getScrollable(el);
106
+ if (scrollable) node.scrollable = scrollable;
107
+
108
+ const text = [];
109
+ for (const child of el.childNodes) {
110
+ if (child.nodeType === 3 && child.textContent.trim()) {
111
+ text.push(child.textContent.trim());
112
+ }
113
+ }
114
+ if (text.length > 0) node.text = text.join(' ').slice(0, 200);
115
+
116
+ nodes[id] = node;
117
+
118
+ if (parentId && nodes[parentId]) {
119
+ nodes[parentId].children.push(id);
120
+ }
121
+
122
+ for (const child of el.children) {
123
+ walk(child, id);
124
+ }
125
+ }
126
+
127
+ const rootId = 'root';
128
+ nodes[rootId] = { id: rootId, tagName: 'body', children: [], attributes: {} };
129
+ for (const child of document.body.children) {
130
+ walk(child, rootId);
131
+ }
132
+
133
+ return { rootId, map: nodes, selectorMap };
134
+ })()
135
+ `;
136
+
137
+ // src/browser/dom/snapshot.ts
138
+ var SNAPSHOT_SCRIPT = `
139
+ (() => {
140
+ let idx = 0;
141
+ const __prevHashes = (window.__lobster_prev_hashes) ? new Set(window.__lobster_prev_hashes) : null;
142
+ const __currentHashes = [];
143
+
144
+ const SKIP_TAGS = new Set([
145
+ 'script','style','noscript','svg','path','meta','link','head',
146
+ 'template','slot','colgroup','col',
147
+ ]);
148
+
149
+ const INTERACTIVE_TAGS = new Set([
150
+ 'a','button','input','select','textarea','details','summary','label',
151
+ ]);
152
+
153
+ const INTERACTIVE_ROLES = new Set([
154
+ 'button','link','textbox','checkbox','radio','combobox','listbox',
155
+ 'menu','menuitem','tab','switch','slider','searchbox','spinbutton',
156
+ 'option','menuitemcheckbox','menuitemradio','treeitem',
157
+ ]);
158
+
159
+ const ATTR_WHITELIST = [
160
+ 'type','role','aria-label','aria-expanded','aria-selected','aria-checked',
161
+ 'aria-disabled','aria-haspopup','aria-pressed','placeholder','title',
162
+ 'href','value','name','alt','src','action','method','for',
163
+ 'data-testid','data-id','contenteditable','tabindex',
164
+ ];
165
+
166
+ const AD_PATTERNS = /ad[-_]?banner|ad[-_]?container|google[-_]?ad|doubleclick|adsbygoogle|sponsored|^ad$/i;
167
+
168
+ // \u2500\u2500 Stage 1: Visibility check \u2500\u2500
169
+ function isVisible(el) {
170
+ if (el.offsetWidth === 0 && el.offsetHeight === 0 && el.tagName !== 'INPUT') return false;
171
+ const s = getComputedStyle(el);
172
+ if (s.display === 'none') return false;
173
+ if (s.visibility === 'hidden' || s.visibility === 'collapse') return false;
174
+ if (s.opacity === '0') return false;
175
+ if (s.clipPath === 'inset(100%)') return false;
176
+ // Check for offscreen positioning
177
+ const rect = el.getBoundingClientRect();
178
+ if (rect.right < 0 || rect.bottom < 0) return false;
179
+ return true;
180
+ }
181
+
182
+ // \u2500\u2500 Stage 2: Interactive detection \u2500\u2500
183
+ function isInteractive(el) {
184
+ const tag = el.tagName.toLowerCase();
185
+ if (INTERACTIVE_TAGS.has(tag)) {
186
+ // Skip disabled elements
187
+ if (el.disabled) return false;
188
+ // Skip hidden inputs
189
+ if (tag === 'input' && el.type === 'hidden') return false;
190
+ return true;
191
+ }
192
+ const role = el.getAttribute('role');
193
+ if (role && INTERACTIVE_ROLES.has(role)) return true;
194
+ if (el.contentEditable === 'true') return true;
195
+ if (el.tabIndex >= 0 && el.getAttribute('tabindex') !== null) return true;
196
+ if (el.onclick) return true;
197
+ return false;
198
+ }
199
+
200
+ // \u2500\u2500 Stage 8: Attribute filtering \u2500\u2500
201
+ function getAttrs(el) {
202
+ const parts = [];
203
+ for (const name of ATTR_WHITELIST) {
204
+ let v = el.getAttribute(name);
205
+ if (v === null || v === '') continue;
206
+ // Truncate long values
207
+ if (v.length > 80) v = v.slice(0, 77) + '...';
208
+ // Skip href="javascript:..."
209
+ if (name === 'href' && v.startsWith('javascript:')) continue;
210
+ parts.push(name + '=' + v);
211
+ }
212
+ return parts.length ? ' ' + parts.join(' ') : '';
213
+ }
214
+
215
+ // \u2500\u2500 Stage 9: Ad filtering \u2500\u2500
216
+ function isAd(el) {
217
+ const id = el.id || '';
218
+ const cls = el.className || '';
219
+ if (typeof cls === 'string' && AD_PATTERNS.test(cls)) return true;
220
+ if (AD_PATTERNS.test(id)) return true;
221
+ if (el.tagName === 'IFRAME' && AD_PATTERNS.test(el.src || '')) return true;
222
+ return false;
223
+ }
224
+
225
+ // \u2500\u2500 Stage 10: Scroll info \u2500\u2500
226
+ function getScrollInfo(el) {
227
+ const s = getComputedStyle(el);
228
+ const overflowY = s.overflowY;
229
+ const overflowX = s.overflowX;
230
+ const scrollableY = (overflowY === 'auto' || overflowY === 'scroll') && el.scrollHeight > el.clientHeight;
231
+ const scrollableX = (overflowX === 'auto' || overflowX === 'scroll') && el.scrollWidth > el.clientWidth;
232
+ if (!scrollableY && !scrollableX) return '';
233
+
234
+ const parts = [];
235
+ if (scrollableY) {
236
+ const up = Math.round(el.scrollTop);
237
+ const down = Math.round(el.scrollHeight - el.clientHeight - el.scrollTop);
238
+ if (up > 0) parts.push(up + 'px up');
239
+ if (down > 0) parts.push(down + 'px down');
240
+ }
241
+ if (scrollableX) {
242
+ const left = Math.round(el.scrollLeft);
243
+ const right = Math.round(el.scrollWidth - el.clientWidth - el.scrollLeft);
244
+ if (left > 0) parts.push(left + 'px left');
245
+ if (right > 0) parts.push(right + 'px right');
246
+ }
247
+ return parts.length ? ' |scroll: ' + parts.join(', ') + '|' : '';
248
+ }
249
+
250
+ // \u2500\u2500 Stage 6: Bounding-box dedup \u2500\u2500
251
+ // If a parent and child are both interactive and have ~same bounding box,
252
+ // skip the parent (e.g., <a><button>Click</button></a>)
253
+ function isWrappingInteractive(el) {
254
+ if (!isInteractive(el)) return false;
255
+ const rect = el.getBoundingClientRect();
256
+ if (rect.width === 0 || rect.height === 0) return false;
257
+ for (const child of el.children) {
258
+ if (!isInteractive(child)) continue;
259
+ const cr = child.getBoundingClientRect();
260
+ const overlapX = Math.min(rect.right, cr.right) - Math.max(rect.left, cr.left);
261
+ const overlapY = Math.min(rect.bottom, cr.bottom) - Math.max(rect.top, cr.top);
262
+ const overlapArea = Math.max(0, overlapX) * Math.max(0, overlapY);
263
+ const parentArea = rect.width * rect.height;
264
+ if (parentArea > 0 && overlapArea / parentArea > 0.85) return true;
265
+ }
266
+ return false;
267
+ }
268
+
269
+ // \u2500\u2500 Stage 7: Occlusion detection \u2500\u2500
270
+ function isOccluded(el) {
271
+ const rect = el.getBoundingClientRect();
272
+ if (rect.width === 0 || rect.height === 0) return false;
273
+ const cx = rect.left + rect.width / 2;
274
+ const cy = rect.top + rect.height / 2;
275
+ const topEl = document.elementFromPoint(cx, cy);
276
+ if (!topEl) return false;
277
+ if (topEl === el || el.contains(topEl) || topEl.contains(el)) return false;
278
+ // Check z-index \u2014 if top element is a modal/overlay, mark as occluded
279
+ const topZ = parseInt(getComputedStyle(topEl).zIndex) || 0;
280
+ const elZ = parseInt(getComputedStyle(el).zIndex) || 0;
281
+ return topZ > elZ + 10;
282
+ }
283
+
284
+ // \u2500\u2500 Stage 5: Iframe content extraction \u2500\u2500
285
+ function getIframeContent(iframe, depth, maxDepth) {
286
+ try {
287
+ const doc = iframe.contentDocument;
288
+ if (!doc || !doc.body) return '';
289
+ return '\\n' + walkNode(doc.body, depth, maxDepth);
290
+ } catch { return ''; }
291
+ }
292
+
293
+ // \u2500\u2500 Stage 4: Shadow DOM traversal \u2500\u2500
294
+ function getShadowContent(el, depth, maxDepth) {
295
+ if (!el.shadowRoot) return '';
296
+ let out = '';
297
+ for (const child of el.shadowRoot.childNodes) {
298
+ out += walkNode(child, depth, maxDepth);
299
+ }
300
+ return out;
301
+ }
302
+
303
+ // \u2500\u2500 Input value hint \u2500\u2500
304
+ function getInputHint(el) {
305
+ const tag = el.tagName.toLowerCase();
306
+ if (tag === 'input') {
307
+ const type = el.type || 'text';
308
+ const val = el.value || '';
309
+ const checked = el.checked;
310
+ if (type === 'checkbox' || type === 'radio') {
311
+ return checked ? ' [checked]' : ' [unchecked]';
312
+ }
313
+ if (val) return ' value="' + val.slice(0, 50) + '"';
314
+ }
315
+ if (tag === 'textarea' && el.value) {
316
+ return ' value="' + el.value.slice(0, 50) + '"';
317
+ }
318
+ if (tag === 'select' && el.selectedOptions?.length) {
319
+ return ' selected="' + el.selectedOptions[0].text.slice(0, 40) + '"';
320
+ }
321
+ return '';
322
+ }
323
+
324
+ const MAX_DEPTH = 25;
325
+ const MAX_TEXT = 150;
326
+
327
+ function walkNode(node, depth, maxDepth) {
328
+ if (depth > maxDepth) return '';
329
+ if (!node) return '';
330
+
331
+ // Text node
332
+ if (node.nodeType === 3) {
333
+ const t = node.textContent.trim();
334
+ if (!t) return '';
335
+ const text = t.length > MAX_TEXT ? t.slice(0, MAX_TEXT) + '...' : t;
336
+ return ' '.repeat(depth) + text + '\\n';
337
+ }
338
+
339
+ // Comment node \u2014 skip
340
+ if (node.nodeType === 8) return '';
341
+
342
+ // Only element nodes from here
343
+ if (node.nodeType !== 1) return '';
344
+
345
+ const el = node;
346
+ const tag = el.tagName.toLowerCase();
347
+
348
+ // \u2500\u2500 Stage 3: Skip tags \u2500\u2500
349
+ if (SKIP_TAGS.has(tag)) return '';
350
+
351
+ // \u2500\u2500 Stage 2: Visibility \u2500\u2500
352
+ if (!isVisible(el)) return '';
353
+
354
+ // \u2500\u2500 Stage 9: Ad filtering \u2500\u2500
355
+ if (isAd(el)) return '';
356
+
357
+ // \u2500\u2500 Stage 6: Bbox dedup \u2014 skip wrapping interactive parent \u2500\u2500
358
+ const skipSelf = isWrappingInteractive(el);
359
+
360
+ const indent = ' '.repeat(depth);
361
+ const inter = !skipSelf && isInteractive(el);
362
+ let prefix = '';
363
+ if (inter) {
364
+ const thisIdx = idx++;
365
+ // Hash: tag + text + key attributes for diff tracking
366
+ const hashText = tag + ':' + (el.textContent || '').trim().slice(0, 40) + ':' + (el.getAttribute('href') || '') + ':' + (el.getAttribute('aria-label') || '');
367
+ __currentHashes.push(hashText);
368
+ const isNew = __prevHashes && __prevHashes.size > 0 && !__prevHashes.has(hashText);
369
+ prefix = isNew ? '*[' + thisIdx + ']' : '[' + thisIdx + ']';
370
+ }
371
+
372
+ // \u2500\u2500 Stage 11: Annotate with data-ref \u2500\u2500
373
+ if (inter) {
374
+ try { el.dataset.ref = String(idx - 1); } catch {}
375
+ }
376
+
377
+ // \u2500\u2500 Stage 7: Occlusion check for interactive elements \u2500\u2500
378
+ if (inter && isOccluded(el)) {
379
+ // Still include but mark as occluded
380
+ // (agent needs to know element exists but may need to scroll/close modal)
381
+ }
382
+
383
+ const a = getAttrs(el);
384
+ const scrollInfo = getScrollInfo(el);
385
+ const inputHint = inter ? getInputHint(el) : '';
386
+
387
+ // Leaf text extraction
388
+ let leafText = '';
389
+ if (el.childNodes.length === 1 && el.childNodes[0].nodeType === 3) {
390
+ const t = el.childNodes[0].textContent.trim();
391
+ if (t) leafText = t.length > MAX_TEXT ? t.slice(0, MAX_TEXT) + '...' : t;
392
+ }
393
+
394
+ // \u2500\u2500 Stage 5: Iframe \u2500\u2500
395
+ if (tag === 'iframe') {
396
+ const iframeContent = getIframeContent(el, depth + 1, maxDepth);
397
+ if (iframeContent) {
398
+ return indent + prefix + '<iframe' + a + '>\\n' + iframeContent;
399
+ }
400
+ return '';
401
+ }
402
+
403
+ // Build output
404
+ let out = '';
405
+
406
+ if (skipSelf) {
407
+ // Skip self but render children
408
+ for (const c of el.childNodes) out += walkNode(c, depth, maxDepth);
409
+ out += getShadowContent(el, depth, maxDepth);
410
+ return out;
411
+ }
412
+
413
+ if (inter || leafText || el.children.length === 0) {
414
+ if (leafText) {
415
+ out = indent + prefix + '<' + tag + a + scrollInfo + inputHint + '>' + leafText + '</' + tag + '>\\n';
416
+ } else {
417
+ out = indent + prefix + '<' + tag + a + scrollInfo + inputHint + '>\\n';
418
+ for (const c of el.childNodes) out += walkNode(c, depth + 1, maxDepth);
419
+ out += getShadowContent(el, depth + 1, maxDepth);
420
+ }
421
+ } else {
422
+ // Non-interactive container \u2014 flatten depth if no useful info
423
+ if (scrollInfo) {
424
+ out = indent + '<' + tag + scrollInfo + '>\\n';
425
+ for (const c of el.childNodes) out += walkNode(c, depth + 1, maxDepth);
426
+ out += getShadowContent(el, depth + 1, maxDepth);
427
+ } else {
428
+ for (const c of el.childNodes) out += walkNode(c, depth, maxDepth);
429
+ out += getShadowContent(el, depth, maxDepth);
430
+ }
431
+ }
432
+
433
+ return out;
434
+ }
435
+
436
+ // \u2500\u2500 Page-level scroll info header \u2500\u2500
437
+ const scrollY = window.scrollY;
438
+ const scrollMax = document.documentElement.scrollHeight - window.innerHeight;
439
+ const scrollPct = scrollMax > 0 ? Math.round((scrollY / scrollMax) * 100) : 0;
440
+ const vpW = window.innerWidth;
441
+ const vpH = window.innerHeight;
442
+ const pageH = document.documentElement.scrollHeight;
443
+
444
+ let header = '';
445
+ header += 'viewport: ' + vpW + 'x' + vpH + ' | page_height: ' + pageH + 'px';
446
+ header += ' | scroll: ' + scrollPct + '%';
447
+ if (scrollY > 50) header += ' (' + Math.round(scrollY) + 'px from top)';
448
+ if (scrollMax - scrollY > 50) header += ' (' + Math.round(scrollMax - scrollY) + 'px more below)';
449
+ header += '\\n---\\n';
450
+
451
+ // Store current hashes for next diff comparison
452
+ window.__lobster_prev_hashes = __currentHashes;
453
+
454
+ return header + walkNode(document.body, 0, MAX_DEPTH);
455
+ })()
456
+ `;
457
+
458
+ // src/browser/dom/semantic-tree.ts
459
+ var SEMANTIC_TREE_SCRIPT = `
460
+ (() => {
461
+ const SKIP = new Set(['script','style','noscript','svg','head','meta','link','template']);
462
+
463
+ const ROLE_MAP = {
464
+ a: 'link', button: 'button', input: 'textbox', select: 'combobox',
465
+ textarea: 'textbox', h1: 'heading', h2: 'heading', h3: 'heading',
466
+ h4: 'heading', h5: 'heading', h6: 'heading', nav: 'navigation',
467
+ main: 'main', header: 'banner', footer: 'contentinfo', aside: 'complementary',
468
+ form: 'form', table: 'table', img: 'img', ul: 'list', ol: 'list', li: 'listitem',
469
+ section: 'region', article: 'article', dialog: 'dialog', details: 'group',
470
+ summary: 'button', progress: 'progressbar', meter: 'meter', output: 'status',
471
+ label: 'label', legend: 'legend', fieldset: 'group', option: 'option',
472
+ tr: 'row', td: 'cell', th: 'columnheader', caption: 'caption',
473
+ };
474
+
475
+ const INTERACTIVE_ROLES = new Set([
476
+ 'button','link','textbox','checkbox','radio','combobox','listbox',
477
+ 'menu','menuitem','tab','switch','slider','searchbox','spinbutton',
478
+ 'option','menuitemcheckbox','menuitemradio','treeitem',
479
+ ]);
480
+
481
+ // \u2500\u2500 W3C Accessible Name Algorithm (simplified) \u2500\u2500
482
+ function getAccessibleName(el) {
483
+ // 1. aria-labelledby (highest priority)
484
+ const labelledBy = el.getAttribute('aria-labelledby');
485
+ if (labelledBy) {
486
+ const ids = labelledBy.split(/\\s+/);
487
+ const parts = ids.map(id => {
488
+ const ref = document.getElementById(id);
489
+ return ref ? ref.textContent.trim() : '';
490
+ }).filter(Boolean);
491
+ if (parts.length > 0) return parts.join(' ').slice(0, 120);
492
+ }
493
+
494
+ // 2. aria-label
495
+ const ariaLabel = el.getAttribute('aria-label');
496
+ if (ariaLabel) return ariaLabel.slice(0, 120);
497
+
498
+ // 3. alt (for images)
499
+ const alt = el.getAttribute('alt');
500
+ if (alt) return alt.slice(0, 120);
501
+
502
+ // 4. title
503
+ const title = el.getAttribute('title');
504
+ if (title) return title.slice(0, 120);
505
+
506
+ // 5. placeholder (for inputs)
507
+ const placeholder = el.getAttribute('placeholder');
508
+ if (placeholder) return placeholder.slice(0, 120);
509
+
510
+ // 6. value (for buttons)
511
+ if (el.tagName === 'INPUT' && (el.type === 'submit' || el.type === 'button')) {
512
+ const val = el.getAttribute('value');
513
+ if (val) return val.slice(0, 120);
514
+ }
515
+
516
+ // 7. Associated label
517
+ if (el.id) {
518
+ const label = document.querySelector('label[for="' + el.id + '"]');
519
+ if (label) return label.textContent.trim().slice(0, 120);
520
+ }
521
+
522
+ // 8. Direct text content (only for leaf-ish elements)
523
+ if (el.children.length <= 2) {
524
+ const text = el.textContent.trim();
525
+ if (text && text.length < 120) return text;
526
+ }
527
+
528
+ return '';
529
+ }
530
+
531
+ // \u2500\u2500 XPath generation \u2500\u2500
532
+ function getXPath(el) {
533
+ const parts = [];
534
+ let current = el;
535
+ while (current && current.nodeType === 1) {
536
+ let index = 1;
537
+ let sibling = current.previousElementSibling;
538
+ while (sibling) {
539
+ if (sibling.tagName === current.tagName) index++;
540
+ sibling = sibling.previousElementSibling;
541
+ }
542
+ const tag = current.tagName.toLowerCase();
543
+ parts.unshift(tag + '[' + index + ']');
544
+ current = current.parentElement;
545
+ }
546
+ return '/' + parts.join('/');
547
+ }
548
+
549
+ // \u2500\u2500 Interactivity classification \u2500\u2500
550
+ function classifyInteractivity(el) {
551
+ const types = [];
552
+ const tag = el.tagName.toLowerCase();
553
+
554
+ // Native
555
+ if (['a','button','input','select','textarea','details','summary'].includes(tag)) {
556
+ if (tag === 'a' && !el.href) {} // anchor without href is not interactive
557
+ else if (tag === 'input' && el.type === 'hidden') {} // hidden inputs
558
+ else types.push('native');
559
+ }
560
+
561
+ // ARIA role
562
+ const role = el.getAttribute('role');
563
+ if (role && INTERACTIVE_ROLES.has(role)) types.push('aria');
564
+
565
+ // Contenteditable
566
+ if (el.contentEditable === 'true') types.push('contenteditable');
567
+
568
+ // Focusable
569
+ if (el.tabIndex >= 0 && el.getAttribute('tabindex') !== null) types.push('focusable');
570
+
571
+ // Event listeners (check onclick and common inline handlers)
572
+ if (el.onclick || el.onmousedown || el.onkeydown || el.onkeypress ||
573
+ el.getAttribute('onclick') || el.getAttribute('onmousedown')) {
574
+ types.push('listener');
575
+ }
576
+
577
+ return types;
578
+ }
579
+
580
+ // \u2500\u2500 Disabled state with fieldset inheritance \u2500\u2500
581
+ function isDisabled(el) {
582
+ if (el.disabled) return true;
583
+ // Check fieldset disabled inheritance
584
+ let parent = el.parentElement;
585
+ while (parent) {
586
+ if (parent.tagName === 'FIELDSET' && parent.disabled) {
587
+ // Exception: elements inside the first legend child are NOT disabled
588
+ const firstLegend = parent.querySelector(':scope > legend');
589
+ if (firstLegend && firstLegend.contains(el)) return false;
590
+ return true;
591
+ }
592
+ parent = parent.parentElement;
593
+ }
594
+ return false;
595
+ }
596
+
597
+ // \u2500\u2500 Walk the DOM \u2500\u2500
598
+ function walk(el, depth, maxDepth) {
599
+ if (!el || depth > maxDepth) return '';
600
+
601
+ if (el.nodeType === 3) {
602
+ const t = el.textContent.trim();
603
+ return t ? ' '.repeat(depth) + 'text "' + t.slice(0, 100) + '"\\n' : '';
604
+ }
605
+
606
+ if (el.nodeType !== 1) return '';
607
+ const tag = el.tagName.toLowerCase();
608
+ if (SKIP.has(tag)) return '';
609
+
610
+ const style = getComputedStyle(el);
611
+ if (style.display === 'none' || style.visibility === 'hidden') return '';
612
+
613
+ const indent = ' '.repeat(depth);
614
+ const role = el.getAttribute('role') || ROLE_MAP[tag] || '';
615
+ const name = getAccessibleName(el);
616
+ const interTypes = classifyInteractivity(el);
617
+ const interactive = interTypes.length > 0;
618
+ const disabled = interactive && isDisabled(el);
619
+
620
+ let line = indent;
621
+ line += role || tag;
622
+
623
+ if (name) line += ' "' + name.slice(0, 80) + '"';
624
+
625
+ if (interactive) {
626
+ line += ' [' + interTypes.join(',') + ']';
627
+ if (disabled) line += ' {disabled}';
628
+ line += ' xpath=' + getXPath(el);
629
+ }
630
+
631
+ // Input state
632
+ if (tag === 'input') {
633
+ const type = el.type || 'text';
634
+ line += ' type=' + type;
635
+ if (type === 'checkbox' || type === 'radio') {
636
+ line += el.checked ? ' [checked]' : ' [unchecked]';
637
+ } else if (el.value) {
638
+ line += ' value="' + el.value.slice(0, 50) + '"';
639
+ }
640
+ }
641
+ if (tag === 'textarea' && el.value) {
642
+ line += ' value="' + el.value.slice(0, 50) + '"';
643
+ }
644
+ if (tag === 'select') {
645
+ const opts = Array.from(el.options || []).map(o => ({
646
+ text: o.text.slice(0, 30),
647
+ value: o.value,
648
+ selected: o.selected,
649
+ }));
650
+ const selected = opts.find(o => o.selected);
651
+ if (selected) line += ' selected="' + selected.text + '"';
652
+ if (opts.length <= 10) {
653
+ line += ' options=[' + opts.map(o => o.text).join('|') + ']';
654
+ }
655
+ }
656
+
657
+ line += '\\n';
658
+
659
+ let out = line;
660
+ for (const c of el.childNodes) {
661
+ out += walk(c, depth + 1, maxDepth);
662
+ }
663
+
664
+ // Shadow DOM
665
+ if (el.shadowRoot) {
666
+ for (const c of el.shadowRoot.childNodes) {
667
+ out += walk(c, depth + 1, maxDepth);
668
+ }
669
+ }
670
+
671
+ return out;
672
+ }
673
+
674
+ return walk(document.body, 0, 20);
675
+ })()
676
+ `;
677
+
678
+ // src/browser/dom/markdown.ts
679
+ var MARKDOWN_SCRIPT = `
680
+ (() => {
681
+ const SKIP = new Set(['script','style','noscript','svg','head','template']);
682
+ const baseUrl = location.href;
683
+
684
+ // Resolve relative URLs to absolute
685
+ function resolveUrl(href) {
686
+ if (!href || href.startsWith('javascript:') || href.startsWith('#')) return href;
687
+ try { return new URL(href, baseUrl).href; } catch { return href; }
688
+ }
689
+
690
+ // Escape Markdown special chars in text
691
+ function escapeText(text) {
692
+ return text
693
+ .replace(/\\\\/g, '\\\\\\\\')
694
+ .replace(/([*_~\`\\[\\]|])/g, '\\\\$1');
695
+ }
696
+
697
+ // State tracking
698
+ let listDepth = 0;
699
+ let orderedCounters = [];
700
+ let inPre = false;
701
+ let inTable = false;
702
+
703
+ function listIndent() { return ' '.repeat(listDepth); }
704
+
705
+ function walk(el) {
706
+ if (!el) return '';
707
+
708
+ // Text node
709
+ if (el.nodeType === 3) {
710
+ const text = el.textContent || '';
711
+ if (inPre) return text;
712
+ // Collapse whitespace
713
+ const collapsed = text.replace(/\\s+/g, ' ');
714
+ return collapsed === ' ' && !el.previousSibling && !el.nextSibling ? '' : collapsed;
715
+ }
716
+
717
+ if (el.nodeType !== 1) return '';
718
+ const tag = el.tagName.toLowerCase();
719
+ if (SKIP.has(tag)) return '';
720
+
721
+ // Visibility check
722
+ try {
723
+ const s = getComputedStyle(el);
724
+ if (s.display === 'none' || s.visibility === 'hidden') return '';
725
+ } catch {}
726
+
727
+ // Get children content
728
+ function childContent() {
729
+ let out = '';
730
+ for (const c of el.childNodes) out += walk(c);
731
+ return out;
732
+ }
733
+
734
+ switch (tag) {
735
+ // \u2500\u2500 Headings \u2500\u2500
736
+ case 'h1': return '\\n\\n# ' + childContent().trim() + '\\n\\n';
737
+ case 'h2': return '\\n\\n## ' + childContent().trim() + '\\n\\n';
738
+ case 'h3': return '\\n\\n### ' + childContent().trim() + '\\n\\n';
739
+ case 'h4': return '\\n\\n#### ' + childContent().trim() + '\\n\\n';
740
+ case 'h5': return '\\n\\n##### ' + childContent().trim() + '\\n\\n';
741
+ case 'h6': return '\\n\\n###### ' + childContent().trim() + '\\n\\n';
742
+
743
+ // \u2500\u2500 Block elements \u2500\u2500
744
+ case 'p': return '\\n\\n' + childContent().trim() + '\\n\\n';
745
+ case 'br': return '\\n';
746
+ case 'hr': return '\\n\\n---\\n\\n';
747
+
748
+ // \u2500\u2500 Inline formatting \u2500\u2500
749
+ case 'strong': case 'b': {
750
+ const inner = childContent().trim();
751
+ return inner ? '**' + inner + '**' : '';
752
+ }
753
+ case 'em': case 'i': {
754
+ const inner = childContent().trim();
755
+ return inner ? '*' + inner + '*' : '';
756
+ }
757
+ case 's': case 'del': case 'strike': {
758
+ const inner = childContent().trim();
759
+ return inner ? '~~' + inner + '~~' : '';
760
+ }
761
+ case 'code': {
762
+ if (inPre) return childContent();
763
+ const inner = childContent();
764
+ return inner ? '\\x60' + inner + '\\x60' : '';
765
+ }
766
+
767
+ // \u2500\u2500 Code blocks \u2500\u2500
768
+ case 'pre': {
769
+ inPre = true;
770
+ const inner = childContent();
771
+ inPre = false;
772
+ const lang = el.querySelector('code')?.className?.match(/language-(\\w+)/)?.[1] || '';
773
+ return '\\n\\n\\x60\\x60\\x60' + lang + '\\n' + inner.trim() + '\\n\\x60\\x60\\x60\\n\\n';
774
+ }
775
+
776
+ // \u2500\u2500 Links \u2500\u2500
777
+ case 'a': {
778
+ const href = resolveUrl(el.getAttribute('href') || '');
779
+ const inner = childContent().trim();
780
+ const name = inner || el.getAttribute('aria-label') || el.getAttribute('title') || '';
781
+ if (!name) return '';
782
+ if (!href || href === '#' || href.startsWith('javascript:')) return name;
783
+ return '[' + name + '](' + href + ')';
784
+ }
785
+
786
+ // \u2500\u2500 Images \u2500\u2500
787
+ case 'img': {
788
+ const alt = el.getAttribute('alt') || '';
789
+ const src = resolveUrl(el.getAttribute('src') || '');
790
+ return src ? '![' + alt + '](' + src + ')' : '';
791
+ }
792
+
793
+ // \u2500\u2500 Lists \u2500\u2500
794
+ case 'ul': {
795
+ listDepth++;
796
+ orderedCounters.push(0);
797
+ const inner = childContent();
798
+ listDepth--;
799
+ orderedCounters.pop();
800
+ return '\\n' + inner;
801
+ }
802
+ case 'ol': {
803
+ listDepth++;
804
+ orderedCounters.push(0);
805
+ const inner = childContent();
806
+ listDepth--;
807
+ orderedCounters.pop();
808
+ return '\\n' + inner;
809
+ }
810
+ case 'li': {
811
+ const parent = el.parentElement?.tagName?.toLowerCase();
812
+ const isOrdered = parent === 'ol';
813
+ const inner = childContent().trim();
814
+ if (!inner) return '';
815
+ if (isOrdered) {
816
+ const counter = orderedCounters.length > 0
817
+ ? ++orderedCounters[orderedCounters.length - 1] : 1;
818
+ return listIndent() + counter + '. ' + inner + '\\n';
819
+ }
820
+ return listIndent() + '- ' + inner + '\\n';
821
+ }
822
+
823
+ // \u2500\u2500 Blockquote \u2500\u2500
824
+ case 'blockquote': {
825
+ const inner = childContent().trim();
826
+ if (!inner) return '';
827
+ return '\\n\\n' + inner.split('\\n').map(line => '> ' + line).join('\\n') + '\\n\\n';
828
+ }
829
+
830
+ // \u2500\u2500 Tables \u2500\u2500
831
+ case 'table': {
832
+ inTable = true;
833
+ let out = '\\n\\n';
834
+ const rows = el.querySelectorAll('tr');
835
+ let headerDone = false;
836
+
837
+ for (let i = 0; i < rows.length; i++) {
838
+ const cells = rows[i].querySelectorAll('th, td');
839
+ const isHeader = rows[i].querySelector('th') !== null;
840
+ const cellTexts = [];
841
+ for (const cell of cells) {
842
+ let cellText = '';
843
+ for (const c of cell.childNodes) cellText += walk(c);
844
+ cellTexts.push(cellText.trim().replace(/\\|/g, '\\\\|').replace(/\\n/g, ' '));
845
+ }
846
+
847
+ out += '| ' + cellTexts.join(' | ') + ' |\\n';
848
+
849
+ if (isHeader && !headerDone) {
850
+ out += '| ' + cellTexts.map(() => '---').join(' | ') + ' |\\n';
851
+ headerDone = true;
852
+ }
853
+
854
+ // First data row without headers \u2014 synthesize separator
855
+ if (i === 0 && !isHeader && !headerDone) {
856
+ out += '| ' + cellTexts.map(() => '---').join(' | ') + ' |\\n';
857
+ headerDone = true;
858
+ }
859
+ }
860
+
861
+ inTable = false;
862
+ return out + '\\n';
863
+ }
864
+ case 'thead': case 'tbody': case 'tfoot':
865
+ return childContent();
866
+ case 'tr': case 'td': case 'th':
867
+ // Handled by table walker above; fallback for orphaned elements
868
+ return childContent();
869
+
870
+ // \u2500\u2500 Definition lists \u2500\u2500
871
+ case 'dl': return '\\n\\n' + childContent() + '\\n\\n';
872
+ case 'dt': return '\\n**' + childContent().trim() + '**\\n';
873
+ case 'dd': return ': ' + childContent().trim() + '\\n';
874
+
875
+ // \u2500\u2500 Figure \u2500\u2500
876
+ case 'figure': return '\\n\\n' + childContent().trim() + '\\n\\n';
877
+ case 'figcaption': return '\\n*' + childContent().trim() + '*\\n';
878
+
879
+ // \u2500\u2500 Details/Summary \u2500\u2500
880
+ case 'details': return '\\n\\n' + childContent() + '\\n\\n';
881
+ case 'summary': return '**' + childContent().trim() + '**\\n\\n';
882
+
883
+ // \u2500\u2500 Generic blocks \u2500\u2500
884
+ case 'div': case 'section': case 'article': case 'main': case 'aside':
885
+ case 'header': case 'footer': case 'nav':
886
+ return '\\n' + childContent() + '\\n';
887
+
888
+ case 'span': case 'small': case 'sub': case 'sup': case 'abbr':
889
+ case 'time': case 'mark': case 'cite': case 'q':
890
+ return childContent();
891
+
892
+ default:
893
+ return childContent();
894
+ }
895
+ }
896
+
897
+ const raw = walk(document.body);
898
+ // Clean up: collapse 3+ newlines to 2, trim
899
+ return raw.replace(/\\n{3,}/g, '\\n\\n').replace(/^\\n+|\\n+$/g, '').trim();
900
+ })()
901
+ `;
902
+
903
+ // src/browser/dom/form-state.ts
904
+ var FORM_STATE_SCRIPT = `
905
+ (() => {
906
+ function extractField(el) {
907
+ const tag = el.tagName.toLowerCase();
908
+ const type = (el.getAttribute('type') || tag).toLowerCase();
909
+
910
+ // Skip non-user-facing inputs
911
+ if (['hidden', 'submit', 'button', 'reset', 'image'].includes(type)) return null;
912
+
913
+ const name = el.name || el.id || '';
914
+
915
+ // Find label via multiple strategies
916
+ const label =
917
+ el.getAttribute('aria-label') ||
918
+ (el.id ? document.querySelector('label[for="' + el.id + '"]')?.textContent?.trim() : null) ||
919
+ el.closest('label')?.textContent?.trim() ||
920
+ el.placeholder ||
921
+ '';
922
+
923
+ // Extract value based on type
924
+ let value;
925
+ if (tag === 'select') {
926
+ const selected = el.options[el.selectedIndex];
927
+ value = selected ? selected.textContent.trim() : '';
928
+ } else if (type === 'checkbox' || type === 'radio') {
929
+ value = el.checked;
930
+ } else if (type === 'password') {
931
+ value = el.value ? '\u2022\u2022\u2022\u2022' : '';
932
+ } else if (el.isContentEditable) {
933
+ value = el.textContent?.trim()?.slice(0, 200) || '';
934
+ } else {
935
+ value = el.value || '';
936
+ }
937
+
938
+ return {
939
+ tag,
940
+ type,
941
+ name,
942
+ label: label.slice(0, 80),
943
+ value: typeof value === 'string' ? value.slice(0, 200) : value,
944
+ required: !!el.required,
945
+ disabled: !!el.disabled,
946
+ ref: el.dataset?.ref || null,
947
+ };
948
+ }
949
+
950
+ const result = { forms: [], orphanFields: [] };
951
+
952
+ // Collect forms
953
+ for (const form of document.forms) {
954
+ const fields = [];
955
+ for (const el of form.elements) {
956
+ const field = extractField(el);
957
+ if (field) fields.push(field);
958
+ }
959
+ result.forms.push({
960
+ id: form.id || '',
961
+ name: form.name || '',
962
+ action: form.action || '',
963
+ method: (form.method || 'get').toUpperCase(),
964
+ fields,
965
+ });
966
+ }
967
+
968
+ // Collect orphan fields (not in a <form>)
969
+ const allInputs = document.querySelectorAll(
970
+ 'input, textarea, select, [contenteditable="true"]'
971
+ );
972
+ for (const el of allInputs) {
973
+ if (!el.form) {
974
+ const field = extractField(el);
975
+ if (field) result.orphanFields.push(field);
976
+ }
977
+ }
978
+
979
+ return result;
980
+ })()
981
+ `;
982
+
983
+ // src/browser/interceptor.ts
984
+ function buildInterceptorScript(pattern) {
985
+ return `
986
+ (() => {
987
+ if (window.__lobster_interceptor__) return;
988
+ window.__lobster_interceptor__ = { requests: [] };
989
+ const store = window.__lobster_interceptor__;
990
+ const pattern = ${JSON.stringify(pattern)};
991
+
992
+ // Patch fetch
993
+ const origFetch = window.fetch;
994
+ window.fetch = async function(...args) {
995
+ const url = typeof args[0] === 'string' ? args[0] : args[0]?.url || '';
996
+ const resp = await origFetch.apply(this, args);
997
+ if (url.includes(pattern)) {
998
+ const clone = resp.clone();
999
+ try {
1000
+ const body = await clone.json();
1001
+ store.requests.push({ url, method: 'GET', status: resp.status, body, timestamp: Date.now() });
1002
+ } catch {}
1003
+ }
1004
+ return resp;
1005
+ };
1006
+
1007
+ // Patch XHR
1008
+ const origOpen = XMLHttpRequest.prototype.open;
1009
+ const origSend = XMLHttpRequest.prototype.send;
1010
+ XMLHttpRequest.prototype.open = function(method, url, ...rest) {
1011
+ this.__url = url;
1012
+ this.__method = method;
1013
+ return origOpen.call(this, method, url, ...rest);
1014
+ };
1015
+ XMLHttpRequest.prototype.send = function(...args) {
1016
+ this.addEventListener('load', function() {
1017
+ if (this.__url && this.__url.includes(pattern)) {
1018
+ try {
1019
+ const body = JSON.parse(this.responseText);
1020
+ store.requests.push({ url: this.__url, method: this.__method, status: this.status, body, timestamp: Date.now() });
1021
+ } catch {}
1022
+ }
1023
+ });
1024
+ return origSend.apply(this, args);
1025
+ };
1026
+ })()
1027
+ `;
1028
+ }
1029
+ var GET_INTERCEPTED_SCRIPT = `
1030
+ (() => {
1031
+ const store = window.__lobster_interceptor__;
1032
+ if (!store) return [];
1033
+ const reqs = [...store.requests];
1034
+ store.requests = [];
1035
+ return reqs;
1036
+ })()
1037
+ `;
1038
+
1039
+ // src/browser/page-adapter.ts
1040
+ var PuppeteerPage = class {
1041
+ page;
1042
+ constructor(page) {
1043
+ this.page = page;
1044
+ }
1045
+ get raw() {
1046
+ return this.page;
1047
+ }
1048
+ async goto(url, options) {
1049
+ await this.page.goto(url, {
1050
+ waitUntil: options?.waitUntil || "networkidle2",
1051
+ timeout: options?.timeout || 3e4
1052
+ });
1053
+ }
1054
+ async goBack() {
1055
+ await this.page.goBack({ waitUntil: "networkidle2" });
1056
+ }
1057
+ async url() {
1058
+ return this.page.url();
1059
+ }
1060
+ async title() {
1061
+ return this.page.title();
1062
+ }
1063
+ async evaluate(js) {
1064
+ return this.page.evaluate(js);
1065
+ }
1066
+ async snapshot(_opts) {
1067
+ return this.page.evaluate(SNAPSHOT_SCRIPT);
1068
+ }
1069
+ async semanticTree(_opts) {
1070
+ return this.page.evaluate(SEMANTIC_TREE_SCRIPT);
1071
+ }
1072
+ async flatTree() {
1073
+ const raw = await this.page.evaluate(FLAT_TREE_SCRIPT);
1074
+ return raw;
1075
+ }
1076
+ async markdown() {
1077
+ return this.page.evaluate(MARKDOWN_SCRIPT);
1078
+ }
1079
+ async browserState() {
1080
+ const state = await this.page.evaluate(`
1081
+ (() => {
1082
+ const scrollY = window.scrollY;
1083
+ const scrollX = window.scrollX;
1084
+ const vpW = window.innerWidth;
1085
+ const vpH = window.innerHeight;
1086
+ const pageW = document.documentElement.scrollWidth;
1087
+ const pageH = document.documentElement.scrollHeight;
1088
+ const maxScrollY = pageH - vpH;
1089
+ return {
1090
+ url: location.href,
1091
+ title: document.title,
1092
+ viewportWidth: vpW,
1093
+ viewportHeight: vpH,
1094
+ pageWidth: pageW,
1095
+ pageHeight: pageH,
1096
+ scrollX: scrollX,
1097
+ scrollY: scrollY,
1098
+ scrollPercent: maxScrollY > 0 ? Math.round((scrollY / maxScrollY) * 100) : 0,
1099
+ pixelsAbove: Math.round(scrollY),
1100
+ pixelsBelow: Math.round(Math.max(0, maxScrollY - scrollY)),
1101
+ };
1102
+ })()
1103
+ `);
1104
+ return state;
1105
+ }
1106
+ async formState() {
1107
+ return this.page.evaluate(FORM_STATE_SCRIPT);
1108
+ }
1109
+ async click(ref) {
1110
+ if (typeof ref === "number") {
1111
+ await this.page.evaluate((idx) => {
1112
+ const el = document.querySelector('[data-ref="' + idx + '"]');
1113
+ if (!el) throw new Error("Element with index " + idx + " not found");
1114
+ const prev = document.activeElement;
1115
+ if (prev && prev !== el && prev !== document.body) {
1116
+ prev.blur();
1117
+ prev.dispatchEvent(new MouseEvent("mouseout", { bubbles: true, cancelable: true }));
1118
+ prev.dispatchEvent(new MouseEvent("mouseleave", { bubbles: false, cancelable: true }));
1119
+ }
1120
+ if (typeof el.scrollIntoViewIfNeeded === "function") {
1121
+ el.scrollIntoViewIfNeeded();
1122
+ } else {
1123
+ el.scrollIntoView({ behavior: "auto", block: "center", inline: "nearest" });
1124
+ }
1125
+ el.dispatchEvent(new MouseEvent("mouseenter", { bubbles: true, cancelable: true }));
1126
+ el.dispatchEvent(new MouseEvent("mouseover", { bubbles: true, cancelable: true }));
1127
+ el.dispatchEvent(new MouseEvent("mousedown", { bubbles: true, cancelable: true }));
1128
+ el.focus();
1129
+ el.dispatchEvent(new MouseEvent("mouseup", { bubbles: true, cancelable: true }));
1130
+ el.dispatchEvent(new MouseEvent("click", { bubbles: true, cancelable: true }));
1131
+ }, ref);
1132
+ await new Promise((r) => setTimeout(r, 200));
1133
+ } else {
1134
+ await this.page.click(ref);
1135
+ }
1136
+ }
1137
+ async typeText(ref, text) {
1138
+ if (typeof ref === "number") {
1139
+ await this.click(ref);
1140
+ await this.page.evaluate((idx, txt) => {
1141
+ const el = document.querySelector('[data-ref="' + idx + '"]');
1142
+ if (!el) throw new Error("Element with index " + idx + " not found");
1143
+ const isInput = el.tagName === "INPUT" || el.tagName === "TEXTAREA";
1144
+ const isContentEditable = el.isContentEditable;
1145
+ if (isContentEditable) {
1146
+ if (el.dispatchEvent(new InputEvent("beforeinput", {
1147
+ bubbles: true,
1148
+ cancelable: true,
1149
+ inputType: "deleteContent"
1150
+ }))) {
1151
+ el.innerText = "";
1152
+ el.dispatchEvent(new InputEvent("input", {
1153
+ bubbles: true,
1154
+ inputType: "deleteContent"
1155
+ }));
1156
+ }
1157
+ if (el.dispatchEvent(new InputEvent("beforeinput", {
1158
+ bubbles: true,
1159
+ cancelable: true,
1160
+ inputType: "insertText",
1161
+ data: txt
1162
+ }))) {
1163
+ el.innerText = txt;
1164
+ el.dispatchEvent(new InputEvent("input", {
1165
+ bubbles: true,
1166
+ inputType: "insertText",
1167
+ data: txt
1168
+ }));
1169
+ }
1170
+ const planAOk = el.innerText.trim() === txt.trim();
1171
+ if (!planAOk) {
1172
+ el.focus();
1173
+ const doc = el.ownerDocument;
1174
+ const sel = (doc.defaultView || window).getSelection();
1175
+ const range = doc.createRange();
1176
+ range.selectNodeContents(el);
1177
+ sel?.removeAllRanges();
1178
+ sel?.addRange(range);
1179
+ doc.execCommand("delete", false);
1180
+ doc.execCommand("insertText", false, txt);
1181
+ }
1182
+ el.dispatchEvent(new Event("change", { bubbles: true }));
1183
+ el.blur();
1184
+ } else if (isInput) {
1185
+ const inputEl = el;
1186
+ const proto = Object.getPrototypeOf(inputEl);
1187
+ const descriptor = Object.getOwnPropertyDescriptor(proto, "value") || Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value") || Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, "value");
1188
+ if (descriptor?.set) {
1189
+ descriptor.set.call(inputEl, txt);
1190
+ } else {
1191
+ inputEl.value = txt;
1192
+ }
1193
+ inputEl.dispatchEvent(new Event("input", { bubbles: true }));
1194
+ inputEl.dispatchEvent(new Event("change", { bubbles: true }));
1195
+ } else {
1196
+ el.value = txt;
1197
+ el.dispatchEvent(new Event("input", { bubbles: true }));
1198
+ el.dispatchEvent(new Event("change", { bubbles: true }));
1199
+ }
1200
+ }, ref, text);
1201
+ } else {
1202
+ await this.page.click(ref, { count: 3 });
1203
+ await this.page.keyboard.type(text);
1204
+ }
1205
+ }
1206
+ async pressKey(key) {
1207
+ await this.page.keyboard.press(key);
1208
+ }
1209
+ async selectOption(ref, value) {
1210
+ const selector = typeof ref === "number" ? '[data-ref="' + ref + '"]' : ref;
1211
+ await this.page.select(selector, value);
1212
+ }
1213
+ async scroll(direction, amount) {
1214
+ const distance = amount || 500;
1215
+ const isVertical = direction === "up" || direction === "down";
1216
+ const positive = direction === "down" || direction === "right";
1217
+ const delta = positive ? distance : -distance;
1218
+ await this.page.evaluate((dy, dx, isVert) => {
1219
+ const canScroll = (el2) => {
1220
+ if (!el2) return false;
1221
+ const s = getComputedStyle(el2);
1222
+ if (isVert) {
1223
+ return /(auto|scroll|overlay)/.test(s.overflowY) && el2.scrollHeight > el2.clientHeight && el2.clientHeight >= window.innerHeight * 0.3;
1224
+ } else {
1225
+ return /(auto|scroll|overlay)/.test(s.overflowX) && el2.scrollWidth > el2.clientWidth && el2.clientWidth >= window.innerWidth * 0.3;
1226
+ }
1227
+ };
1228
+ let el = document.activeElement;
1229
+ while (el && !canScroll(el) && el !== document.body) {
1230
+ el = el.parentElement;
1231
+ }
1232
+ if (!canScroll(el)) {
1233
+ el = Array.from(document.querySelectorAll("*")).find(canScroll) || null;
1234
+ }
1235
+ const isPageLevel = !el || el === document.body || el === document.documentElement || el === document.scrollingElement;
1236
+ if (isPageLevel) {
1237
+ if (isVert) {
1238
+ window.scrollBy(0, dy);
1239
+ } else {
1240
+ window.scrollBy(dx, 0);
1241
+ }
1242
+ } else {
1243
+ if (isVert) {
1244
+ el.scrollBy({ top: dy, behavior: "smooth" });
1245
+ } else {
1246
+ el.scrollBy({ left: dx, behavior: "smooth" });
1247
+ }
1248
+ }
1249
+ }, isVertical ? delta : 0, isVertical ? 0 : delta, isVertical);
1250
+ await new Promise((r) => setTimeout(r, 150));
1251
+ }
1252
+ async scrollToElement(ref) {
1253
+ const selector = typeof ref === "number" ? '[data-ref="' + ref + '"]' : ref;
1254
+ await this.page.evaluate((sel) => {
1255
+ const el = document.querySelector(sel);
1256
+ if (!el) return;
1257
+ if (typeof el.scrollIntoViewIfNeeded === "function") {
1258
+ el.scrollIntoViewIfNeeded();
1259
+ } else {
1260
+ el.scrollIntoView({ behavior: "auto", block: "center", inline: "nearest" });
1261
+ }
1262
+ }, selector);
1263
+ }
1264
+ async getCookies(opts) {
1265
+ const cookies = await this.page.cookies();
1266
+ const filtered = opts?.domain ? cookies.filter((c) => c.domain.includes(opts.domain)) : cookies;
1267
+ return filtered.map((c) => ({
1268
+ name: c.name,
1269
+ value: c.value,
1270
+ domain: c.domain,
1271
+ path: c.path,
1272
+ expires: c.expires,
1273
+ httpOnly: c.httpOnly,
1274
+ secure: c.secure,
1275
+ sameSite: c.sameSite
1276
+ }));
1277
+ }
1278
+ async wait(options) {
1279
+ if (typeof options === "number") {
1280
+ await new Promise((r) => setTimeout(r, options * 1e3));
1281
+ return;
1282
+ }
1283
+ if (options.time) {
1284
+ await new Promise((r) => setTimeout(r, options.time * 1e3));
1285
+ }
1286
+ if (options.text) {
1287
+ await this.page.waitForFunction(
1288
+ (t) => document.body.innerText.includes(t),
1289
+ { timeout: options.timeout || 3e4 },
1290
+ options.text
1291
+ );
1292
+ }
1293
+ }
1294
+ async networkRequests(includeStatic) {
1295
+ const entries = await this.page.evaluate(`
1296
+ (() => {
1297
+ const entries = performance.getEntriesByType('resource');
1298
+ const staticTypes = new Set(['img', 'font', 'css', 'script', 'link']);
1299
+ const includeStatic = ${!!includeStatic};
1300
+
1301
+ return entries
1302
+ .filter(e => includeStatic || !staticTypes.has(e.initiatorType))
1303
+ .map(e => ({
1304
+ url: e.name,
1305
+ method: 'GET',
1306
+ status: 200,
1307
+ type: e.initiatorType || 'other',
1308
+ size: e.transferSize || e.encodedBodySize || 0,
1309
+ duration: Math.round(e.duration),
1310
+ }));
1311
+ })()
1312
+ `);
1313
+ return entries || [];
1314
+ }
1315
+ async installInterceptor(pattern) {
1316
+ await this.page.evaluate(buildInterceptorScript(pattern));
1317
+ }
1318
+ async getInterceptedRequests() {
1319
+ return this.page.evaluate(GET_INTERCEPTED_SCRIPT);
1320
+ }
1321
+ async screenshot(opts) {
1322
+ const result = await this.page.screenshot({
1323
+ type: opts?.format || "png",
1324
+ fullPage: opts?.fullPage ?? false
1325
+ });
1326
+ return Buffer.from(result);
1327
+ }
1328
+ async tabs() {
1329
+ const browser = this.page.browser();
1330
+ const pages = await browser.pages();
1331
+ return pages.map((p, i) => ({
1332
+ id: i,
1333
+ url: p.url(),
1334
+ title: "",
1335
+ active: p === this.page
1336
+ }));
1337
+ }
1338
+ async close() {
1339
+ await this.page.close();
1340
+ }
1341
+ };
1342
+ export {
1343
+ PuppeteerPage
1344
+ };
1345
+ //# sourceMappingURL=page-adapter.js.map