camel-ai 0.2.73a1__py3-none-any.whl → 0.2.73a2__py3-none-any.whl

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.

Potentially problematic release.


This version of camel-ai might be problematic. Click here for more details.

@@ -0,0 +1,1002 @@
1
+ (() => {
2
+ // Unified analyzer that combines visual and structural analysis
3
+ // Preserves complete snapshot.js logic while adding visual coordinate information
4
+
5
+ // Memory management constants and configuration
6
+ const MAX_REFS = 2000; // Maximum number of refs to keep in memory
7
+ const MAX_UNUSED_AGE_MS = 90000; // Remove refs unused for more than xx seconds
8
+ const CLEANUP_THRESHOLD = 0.8; // Start aggressive cleanup when 80% of max refs reached
9
+
10
+ // Persistent ref management across page analysis calls with memory leak prevention
11
+ let refCounter = window.__camelRefCounter || 1;
12
+ let elementRefMap = window.__camelElementRefMap || new WeakMap();
13
+ let refElementMap = window.__camelRefElementMap || new Map();
14
+ let elementSignatureMap = window.__camelElementSignatureMap || new Map();
15
+
16
+ // LRU tracking for ref access times
17
+ let refAccessTimes = window.__camelRefAccessTimes || new Map();
18
+ let lastNavigationUrl = window.__camelLastNavigationUrl || window.location.href;
19
+
20
+ // Initialize navigation event listeners for automatic cleanup
21
+ if (!window.__camelNavigationListenersInitialized) {
22
+ window.__camelNavigationListenersInitialized = true;
23
+
24
+ // Listen for page navigation events
25
+ window.addEventListener('beforeunload', clearAllRefs);
26
+ window.addEventListener('pagehide', clearAllRefs);
27
+
28
+ // Listen for pushState/replaceState navigation (SPA navigation)
29
+ const originalPushState = history.pushState;
30
+ const originalReplaceState = history.replaceState;
31
+
32
+ history.pushState = function(...args) {
33
+ clearAllRefs();
34
+ return originalPushState.apply(this, args);
35
+ };
36
+
37
+ history.replaceState = function(...args) {
38
+ clearAllRefs();
39
+ return originalReplaceState.apply(this, args);
40
+ };
41
+
42
+ // Listen for popstate (back/forward navigation)
43
+ window.addEventListener('popstate', clearAllRefs);
44
+
45
+ // Check for URL changes periodically (fallback for other navigation types)
46
+ setInterval(() => {
47
+ if (window.location.href !== lastNavigationUrl) {
48
+ clearAllRefs();
49
+ lastNavigationUrl = window.location.href;
50
+ window.__camelLastNavigationUrl = lastNavigationUrl;
51
+ }
52
+ }, 1000);
53
+ }
54
+
55
+ function generateRef() {
56
+ const ref = `e${refCounter++}`;
57
+ // Persist counter globally
58
+ window.__camelRefCounter = refCounter;
59
+ return ref;
60
+ }
61
+
62
+ // Clear all refs and reset memory state
63
+ function clearAllRefs() {
64
+ try {
65
+ // Clear all DOM aria-ref attributes
66
+ document.querySelectorAll('[aria-ref]').forEach(element => {
67
+ element.removeAttribute('aria-ref');
68
+ });
69
+
70
+ // Clear all maps and reset counters
71
+ elementRefMap.clear();
72
+ refElementMap.clear();
73
+ elementSignatureMap.clear();
74
+ refAccessTimes.clear();
75
+
76
+ // Reset global state
77
+ window.__camelElementRefMap = elementRefMap;
78
+ window.__camelRefElementMap = refElementMap;
79
+ window.__camelElementSignatureMap = elementSignatureMap;
80
+ window.__camelRefAccessTimes = refAccessTimes;
81
+
82
+ // Clear cached analysis results
83
+ delete window.__camelLastAnalysisResult;
84
+ delete window.__camelLastAnalysisTime;
85
+
86
+ console.log('CAMEL: Cleared all refs due to navigation');
87
+ } catch (error) {
88
+ console.warn('CAMEL: Error clearing refs:', error);
89
+ }
90
+ }
91
+
92
+ // LRU eviction: Remove least recently used refs when limit exceeded
93
+ function evictLRURefs() {
94
+ const refsToEvict = refAccessTimes.size - MAX_REFS + Math.floor(MAX_REFS * 0.1); // Remove 10% extra for breathing room
95
+ if (refsToEvict <= 0) return 0;
96
+
97
+ // Sort refs by access time (oldest first)
98
+ const sortedRefs = Array.from(refAccessTimes.entries())
99
+ .sort((a, b) => a[1] - b[1])
100
+ .slice(0, refsToEvict);
101
+
102
+ let evictedCount = 0;
103
+ for (const [ref, _] of sortedRefs) {
104
+ const element = refElementMap.get(ref);
105
+ if (element) {
106
+ // Remove aria-ref attribute from DOM
107
+ try {
108
+ element.removeAttribute('aria-ref');
109
+ } catch (e) {
110
+ // Element might be detached from DOM
111
+ }
112
+ elementRefMap.delete(element);
113
+
114
+ // Remove from signature map
115
+ const signature = generateElementSignature(element);
116
+ if (signature && elementSignatureMap.get(signature) === ref) {
117
+ elementSignatureMap.delete(signature);
118
+ }
119
+ }
120
+
121
+ refElementMap.delete(ref);
122
+ refAccessTimes.delete(ref);
123
+ evictedCount++;
124
+ }
125
+
126
+ // Persist updated maps
127
+ window.__camelElementRefMap = elementRefMap;
128
+ window.__camelRefElementMap = refElementMap;
129
+ window.__camelElementSignatureMap = elementSignatureMap;
130
+ window.__camelRefAccessTimes = refAccessTimes;
131
+
132
+ return evictedCount;
133
+ }
134
+
135
+ // Update ref access time for LRU tracking
136
+ function updateRefAccessTime(ref) {
137
+ refAccessTimes.set(ref, Date.now());
138
+ window.__camelRefAccessTimes = refAccessTimes;
139
+ }
140
+
141
+ // Generate a unique signature for an element based on its characteristics
142
+ function generateElementSignature(element) {
143
+ if (!element || !element.tagName) return null;
144
+
145
+ const tagName = element.tagName.toLowerCase();
146
+ const textContent = (element.textContent || '').trim().substring(0, 50);
147
+ const className = element.className || '';
148
+ const id = element.id || '';
149
+ const href = element.href || '';
150
+ const src = element.src || '';
151
+ const value = element.value || '';
152
+ const type = element.type || '';
153
+ const placeholder = element.placeholder || '';
154
+
155
+ // Include position in DOM tree for uniqueness
156
+ let pathElements = [];
157
+ let current = element;
158
+ let depth = 0;
159
+ while (current && current.parentElement && depth < 5) {
160
+ const siblings = Array.from(current.parentElement.children);
161
+ const index = siblings.indexOf(current);
162
+ pathElements.unshift(`${current.tagName.toLowerCase()}[${index}]`);
163
+ current = current.parentElement;
164
+ depth++;
165
+ }
166
+ const domPath = pathElements.join('>');
167
+
168
+ return `${tagName}|${textContent}|${className}|${id}|${href}|${src}|${value}|${type}|${placeholder}|${domPath}`;
169
+ }
170
+
171
+ // Get or assign a persistent ref for an element
172
+ function getOrAssignRef(element) {
173
+ // Check if element already has a ref assigned
174
+ if (elementRefMap.has(element)) {
175
+ const existingRef = elementRefMap.get(element);
176
+ // Verify the ref is still valid
177
+ if (refElementMap.get(existingRef) === element) {
178
+ updateRefAccessTime(existingRef);
179
+ return existingRef;
180
+ }
181
+ }
182
+
183
+ // Check if element has aria-ref attribute (from previous analysis)
184
+ const existingAriaRef = element.getAttribute('aria-ref');
185
+ if (existingAriaRef && refElementMap.get(existingAriaRef) === element) {
186
+ // Re-establish mappings
187
+ elementRefMap.set(element, existingAriaRef);
188
+ updateRefAccessTime(existingAriaRef);
189
+ return existingAriaRef;
190
+ }
191
+
192
+ // Try to find element by signature (in case DOM was modified)
193
+ const signature = generateElementSignature(element);
194
+ if (signature && elementSignatureMap.has(signature)) {
195
+ const existingRef = elementSignatureMap.get(signature);
196
+ // Verify the old element is no longer in DOM or has changed
197
+ const oldElement = refElementMap.get(existingRef);
198
+ if (!oldElement || !document.contains(oldElement) || generateElementSignature(oldElement) !== signature) {
199
+ // Reassign the ref to the new element
200
+ elementRefMap.set(element, existingRef);
201
+ refElementMap.set(existingRef, element);
202
+ elementSignatureMap.set(signature, existingRef);
203
+ element.setAttribute('aria-ref', existingRef);
204
+ updateRefAccessTime(existingRef);
205
+ return existingRef;
206
+ }
207
+ }
208
+
209
+ // Check if we need to evict refs before creating new ones
210
+ if (refElementMap.size >= MAX_REFS) {
211
+ evictLRURefs();
212
+ }
213
+
214
+ // Generate new ref for new element
215
+ const newRef = generateRef();
216
+ elementRefMap.set(element, newRef);
217
+ refElementMap.set(newRef, element);
218
+ if (signature) {
219
+ elementSignatureMap.set(signature, newRef);
220
+ }
221
+ element.setAttribute('aria-ref', newRef);
222
+ updateRefAccessTime(newRef);
223
+ return newRef;
224
+ }
225
+
226
+ // Enhanced cleanup function with aggressive stale ref removal
227
+ function cleanupStaleRefs() {
228
+ const staleRefs = [];
229
+ const currentTime = Date.now();
230
+ const isAggressiveCleanup = refElementMap.size > (MAX_REFS * CLEANUP_THRESHOLD);
231
+
232
+ // Check all mapped elements to see if they're still in DOM or too old
233
+ for (const [ref, element] of refElementMap.entries()) {
234
+ let shouldRemove = false;
235
+
236
+ // Standard checks: element not in DOM
237
+ if (!element || !document.contains(element)) {
238
+ shouldRemove = true;
239
+ }
240
+ // Aggressive cleanup: remove refs unused for too long
241
+ else if (isAggressiveCleanup) {
242
+ const lastAccess = refAccessTimes.get(ref) || 0;
243
+ const age = currentTime - lastAccess;
244
+ if (age > MAX_UNUSED_AGE_MS) {
245
+ shouldRemove = true;
246
+ }
247
+ }
248
+ // Additional checks for aggressive cleanup
249
+ else if (isAggressiveCleanup) {
250
+ // Remove refs for elements that are hidden or have no meaningful content
251
+ try {
252
+ const style = window.getComputedStyle(element);
253
+ const hasNoVisibleContent = !element.textContent?.trim() &&
254
+ !element.value?.trim() &&
255
+ !element.src &&
256
+ !element.href;
257
+
258
+ if ((style.display === 'none' || style.visibility === 'hidden') && hasNoVisibleContent) {
259
+ shouldRemove = true;
260
+ }
261
+ } catch (e) {
262
+ // If we can't get computed style, element might be detached
263
+ shouldRemove = true;
264
+ }
265
+ }
266
+
267
+ if (shouldRemove) {
268
+ staleRefs.push(ref);
269
+ }
270
+ }
271
+
272
+ // Remove stale mappings
273
+ for (const ref of staleRefs) {
274
+ const element = refElementMap.get(ref);
275
+ if (element) {
276
+ // Remove aria-ref attribute from DOM
277
+ try {
278
+ element.removeAttribute('aria-ref');
279
+ } catch (e) {
280
+ // Element might be detached from DOM
281
+ }
282
+ elementRefMap.delete(element);
283
+
284
+ // Remove from signature map
285
+ const signature = generateElementSignature(element);
286
+ if (signature && elementSignatureMap.get(signature) === ref) {
287
+ elementSignatureMap.delete(signature);
288
+ }
289
+ }
290
+ refElementMap.delete(ref);
291
+ refAccessTimes.delete(ref);
292
+ }
293
+
294
+ // Persist maps globally
295
+ window.__camelElementRefMap = elementRefMap;
296
+ window.__camelRefElementMap = refElementMap;
297
+ window.__camelElementSignatureMap = elementSignatureMap;
298
+ window.__camelRefAccessTimes = refAccessTimes;
299
+
300
+ return staleRefs.length;
301
+ }
302
+
303
+ // === Complete snapshot.js logic preservation ===
304
+
305
+ function isVisible(node) {
306
+ // Check if node is null or not a valid DOM node
307
+ if (!node || typeof node.nodeType === 'undefined') return false;
308
+ if (node.nodeType !== Node.ELEMENT_NODE) return true;
309
+
310
+ try {
311
+ const style = window.getComputedStyle(node);
312
+ if (style.display === 'none' || style.visibility === 'hidden' || style.opacity === '0')
313
+ return false;
314
+ // An element with `display: contents` is not rendered itself, but its children are.
315
+ if (style.display === 'contents')
316
+ return true;
317
+ const rect = node.getBoundingClientRect();
318
+ return rect.width > 0 && rect.height > 0;
319
+ } catch (e) {
320
+ // If there's an error getting computed style or bounding rect, assume element is not visible
321
+ return false;
322
+ }
323
+ }
324
+
325
+ // Optimized occlusion detection with fewer test points
326
+ function isOccluded(element) {
327
+ if (!element || element.nodeType !== Node.ELEMENT_NODE) return false;
328
+
329
+ try {
330
+ const rect = element.getBoundingClientRect();
331
+ if (rect.width === 0 || rect.height === 0) return true;
332
+
333
+ // Simplified: Use fewer test points for better performance
334
+ const testPoints = [
335
+ // Center point (most important)
336
+ { x: rect.left + rect.width * 0.5, y: rect.top + rect.height * 0.5, weight: 4 },
337
+ // Only test 4 strategic points instead of 9
338
+ { x: rect.left + rect.width * 0.25, y: rect.top + rect.height * 0.25, weight: 1 },
339
+ { x: rect.left + rect.width * 0.75, y: rect.top + rect.height * 0.25, weight: 1 },
340
+ { x: rect.left + rect.width * 0.25, y: rect.top + rect.height * 0.75, weight: 1 },
341
+ { x: rect.left + rect.width * 0.75, y: rect.top + rect.height * 0.75, weight: 1 }
342
+ ];
343
+
344
+ let totalWeight = 0;
345
+ let visibleWeight = 0;
346
+
347
+ for (const point of testPoints) {
348
+ // Skip points outside viewport
349
+ if (point.x < 0 || point.y < 0 ||
350
+ point.x >= window.innerWidth || point.y >= window.innerHeight) {
351
+ continue;
352
+ }
353
+
354
+ const hitElement = document.elementFromPoint(point.x, point.y);
355
+ totalWeight += point.weight;
356
+
357
+ // Simplified visibility check
358
+ if (hitElement && (hitElement === element || element.contains(hitElement) || hitElement.contains(element))) {
359
+ visibleWeight += point.weight;
360
+ }
361
+ }
362
+
363
+ // If no valid test points, assume not occluded
364
+ if (totalWeight === 0) return false;
365
+
366
+ // Element is occluded if less than 40% of weighted points are visible
367
+ return (visibleWeight / totalWeight) < 0.4;
368
+
369
+ } catch (e) {
370
+ return false;
371
+ }
372
+ }
373
+
374
+ function getRole(node) {
375
+ // Check if node is null or doesn't have required properties
376
+ if (!node || !node.tagName || !node.getAttribute) {
377
+ return 'generic';
378
+ }
379
+
380
+ const role = node.getAttribute('role');
381
+ if (role) return role;
382
+
383
+ const tagName = node.tagName.toLowerCase();
384
+
385
+ // Extended role mapping to better match Playwright
386
+ if (tagName === 'a') return 'link';
387
+ if (tagName === 'button') return 'button';
388
+ if (tagName === 'input') {
389
+ const type = node.getAttribute('type')?.toLowerCase();
390
+ if (['button', 'checkbox', 'radio', 'reset', 'submit'].includes(type)) return type;
391
+ return 'textbox';
392
+ }
393
+ if (['select', 'textarea'].includes(tagName)) return tagName;
394
+ if (['h1', 'h2', 'h3', 'h4', 'h5', 'h6'].includes(tagName)) return 'heading';
395
+
396
+ // Additional roles for better Playwright compatibility
397
+ if (tagName === 'img') return 'img';
398
+ if (tagName === 'main') return 'main';
399
+ if (tagName === 'nav') return 'navigation';
400
+ if (tagName === 'ul' || tagName === 'ol') return 'list';
401
+ if (tagName === 'li') return 'listitem';
402
+ if (tagName === 'em') return 'emphasis';
403
+ if (tagName === 'form' && node.getAttribute('role') === 'search') return 'search';
404
+ if (tagName === 'section' || tagName === 'article') return 'region';
405
+ if (tagName === 'aside') return 'complementary';
406
+ if (tagName === 'header') return 'banner';
407
+ if (tagName === 'footer') return 'contentinfo';
408
+ if (tagName === 'fieldset') return 'group';
409
+
410
+ return 'generic';
411
+ }
412
+
413
+ // Playwright-inspired function to check if element receives pointer events
414
+ function receivesPointerEvents(element) {
415
+ if (!element || !element.nodeType || element.nodeType !== Node.ELEMENT_NODE) return false;
416
+
417
+ try {
418
+ let e = element;
419
+ while (e) {
420
+ const style = window.getComputedStyle(e);
421
+ if (!style) break;
422
+
423
+ const pointerEvents = style.pointerEvents;
424
+ if (pointerEvents === 'none') return false;
425
+ if (pointerEvents && pointerEvents !== 'auto') return true;
426
+
427
+ e = e.parentElement;
428
+ }
429
+ return true;
430
+ } catch (error) {
431
+ return false;
432
+ }
433
+ }
434
+
435
+ // Playwright-inspired function to check if element has pointer cursor
436
+ function hasPointerCursor(element) {
437
+ if (!element || !element.nodeType || element.nodeType !== Node.ELEMENT_NODE) return false;
438
+
439
+ try {
440
+ const style = window.getComputedStyle(element);
441
+ return style.cursor === 'pointer';
442
+ } catch (error) {
443
+ return false;
444
+ }
445
+ }
446
+
447
+ // Playwright-inspired function to get aria level
448
+ function getAriaLevel(element) {
449
+ if (!element || !element.tagName) return 0;
450
+
451
+ // Native HTML heading levels (H1=1, H2=2, etc.)
452
+ const tagName = element.tagName.toUpperCase();
453
+ const nativeLevel = { 'H1': 1, 'H2': 2, 'H3': 3, 'H4': 4, 'H5': 5, 'H6': 6 }[tagName];
454
+ if (nativeLevel) return nativeLevel;
455
+
456
+ // Check aria-level attribute for roles that support it
457
+ const role = getRole(element);
458
+ const kAriaLevelRoles = ['heading', 'listitem', 'row', 'treeitem'];
459
+ if (kAriaLevelRoles.includes(role)) {
460
+ const ariaLevel = element.getAttribute('aria-level');
461
+ if (ariaLevel !== null) {
462
+ const value = Number(ariaLevel);
463
+ if (Number.isInteger(value) && value >= 1) {
464
+ return value;
465
+ }
466
+ }
467
+ }
468
+
469
+ return 0;
470
+ }
471
+
472
+ function getAccessibleName(node) {
473
+ // Check if node is null or doesn't have required methods
474
+ if (!node || !node.hasAttribute || !node.getAttribute) return '';
475
+
476
+ if (node.hasAttribute('aria-label')) return node.getAttribute('aria-label') || '';
477
+ if (node.hasAttribute('aria-labelledby')) {
478
+ const id = node.getAttribute('aria-labelledby');
479
+ const labelEl = document.getElementById(id);
480
+ if (labelEl) return labelEl.textContent || '';
481
+ }
482
+ // This is the new, visibility-aware text extraction logic.
483
+ const text = getVisibleTextContent(node);
484
+
485
+ // Add a heuristic to ignore code-like text that might be in the DOM
486
+ if ((text.match(/[;:{}]/g)?.length || 0) > 2) return '';
487
+ return text;
488
+ }
489
+
490
+ const textCache = new Map();
491
+ function getVisibleTextContent(_node) {
492
+ // Check if node is null or doesn't have nodeType
493
+ if (!_node || typeof _node.nodeType === 'undefined') return '';
494
+
495
+ if (textCache.has(_node)) return textCache.get(_node);
496
+
497
+ if (_node.nodeType === Node.TEXT_NODE) {
498
+ // For a text node, its content is visible if its parent is.
499
+ // The isVisible check on the parent happens before this recursion.
500
+ return _node.nodeValue || '';
501
+ }
502
+
503
+ if (_node.nodeType !== Node.ELEMENT_NODE || !isVisible(_node) || ['SCRIPT', 'STYLE', 'NOSCRIPT', 'META', 'HEAD'].includes(_node.tagName)) {
504
+ return '';
505
+ }
506
+
507
+ let result = '';
508
+ for (const child of _node.childNodes) {
509
+ result += getVisibleTextContent(child);
510
+ }
511
+
512
+ // Caching the result for performance.
513
+ textCache.set(_node, result);
514
+ return result;
515
+ }
516
+
517
+ /**
518
+ * Phase 1: Build an in-memory representation of the accessibility tree.
519
+ * Complete preservation of snapshot.js buildAriaTree logic
520
+ */
521
+ function buildAriaTree(rootElement) {
522
+ const visited = new Set();
523
+
524
+ function toAriaNode(element) {
525
+ // Check if element is null or not a valid DOM element
526
+ if (!element || !element.tagName) return null;
527
+
528
+ // Only consider visible elements
529
+ if (!isVisible(element)) return null;
530
+
531
+ const role = getRole(element);
532
+ // 'presentation' and 'none' roles are ignored, but their children are processed.
533
+ if (['presentation', 'none'].includes(role)) return null;
534
+
535
+ const name = getAccessibleName(element);
536
+
537
+ // Get persistent ref for this element
538
+ const ref = getOrAssignRef(element);
539
+
540
+ // Create the node
541
+ const node = {
542
+ role,
543
+ name,
544
+ children: [],
545
+ element: element,
546
+ ref: ref,
547
+ };
548
+
549
+ // Add states for interactive elements, similar to Playwright
550
+ if (element.hasAttribute('disabled') || element.disabled) node.disabled = true;
551
+
552
+ // NEW: Check if element is occluded and mark with occluded tag
553
+ if (isOccluded(element)) {
554
+ node.occluded = true; // Mark as occluded but don't disable
555
+ }
556
+
557
+ // Handle aria-checked and native checked
558
+ const ariaChecked = element.getAttribute('aria-checked');
559
+ if (ariaChecked) {
560
+ node.checked = ariaChecked;
561
+ } else if (element.type === 'checkbox' || element.type === 'radio') {
562
+ node.checked = element.checked;
563
+ }
564
+
565
+ // Handle aria-expanded
566
+ const ariaExpanded = element.getAttribute('aria-expanded');
567
+ if (ariaExpanded) {
568
+ node.expanded = ariaExpanded === 'true';
569
+ }
570
+
571
+ // Handle aria-selected
572
+ const ariaSelected = element.getAttribute('aria-selected');
573
+ if (ariaSelected === 'true') {
574
+ node.selected = true;
575
+ }
576
+
577
+ // Add level support following Playwright's implementation
578
+ const level = getAriaLevel(element);
579
+ if (level > 0) node.level = level;
580
+
581
+ return node;
582
+ }
583
+
584
+ function traverse(element, parentNode) {
585
+ // Check if element is null or not a valid DOM element
586
+ if (!element || !element.tagName || visited.has(element)) return;
587
+ visited.add(element);
588
+
589
+ // FIX: Completely skip script and style tags and their children.
590
+ const tagName = element.tagName.toLowerCase();
591
+ if (['script', 'style', 'meta', 'noscript'].includes(tagName))
592
+ return;
593
+
594
+ // Check if element is explicitly hidden by CSS - if so, skip entirely including children
595
+ const style = window.getComputedStyle(element);
596
+ if (style.display === 'none' || style.visibility === 'hidden' || style.opacity === '0') {
597
+ return;
598
+ }
599
+
600
+ const ariaNode = toAriaNode(element);
601
+ // If the element is not rendered or is presentational, its children
602
+ // are attached directly to the parent.
603
+ const newParent = ariaNode || parentNode;
604
+ if (ariaNode) parentNode.children.push(ariaNode);
605
+
606
+ for (const child of element.childNodes) {
607
+ if (child.nodeType === Node.ELEMENT_NODE) {
608
+ traverse(child, newParent);
609
+ } else if (child.nodeType === Node.TEXT_NODE) {
610
+ const text = (child.textContent || '').trim();
611
+ if (text) newParent.children.push(text);
612
+ }
613
+ }
614
+
615
+ // Also traverse into shadow DOM if it exists
616
+ if (element.shadowRoot) {
617
+ for (const child of element.shadowRoot.childNodes) {
618
+ if (child.nodeType === Node.ELEMENT_NODE) {
619
+ traverse(child, newParent);
620
+ } else if (child.nodeType === Node.TEXT_NODE) {
621
+ const text = (child.textContent || '').trim();
622
+ if (text) newParent.children.push(text);
623
+ }
624
+ }
625
+ }
626
+
627
+ // FIX: Remove redundant text children that match the element's name
628
+ if (ariaNode && ariaNode.children.length > 0) {
629
+ // Remove text children that are the same as the parent's name or are contained in it
630
+ ariaNode.children = ariaNode.children.filter(child => {
631
+ if (typeof child === 'string') {
632
+ const childText = child.trim();
633
+ const parentName = ariaNode.name.trim();
634
+
635
+ // Remove if text child exactly matches parent name
636
+ if (childText === parentName) {
637
+ return false;
638
+ }
639
+
640
+ // Also remove if the child text is completely contained in parent name
641
+ // and represents a significant portion (to avoid removing important partial text)
642
+ if (childText.length > 3 && parentName.includes(childText)) {
643
+ return false;
644
+ }
645
+
646
+ return true;
647
+ }
648
+ return true;
649
+ });
650
+
651
+ // If after filtering, we have only one text child that equals the name, remove it
652
+ if (ariaNode.children.length === 1 && typeof ariaNode.children[0] === 'string' && ariaNode.name === ariaNode.children[0]) {
653
+ ariaNode.children = [];
654
+ }
655
+ }
656
+ }
657
+
658
+ const root = { role: 'Root', name: '', children: [], element: rootElement };
659
+ traverse(rootElement, root);
660
+ return root;
661
+ }
662
+
663
+ /**
664
+ * Phase 2: Normalize the tree by removing redundant generic wrappers.
665
+ * Complete preservation of snapshot.js normalizeTree logic with cursor inheritance
666
+ */
667
+ function normalizeTree(node) {
668
+ if (typeof node === 'string') return [node];
669
+
670
+ const newChildren = [];
671
+ for (const child of node.children) {
672
+ newChildren.push(...normalizeTree(child));
673
+ }
674
+ node.children = newChildren;
675
+
676
+ // Remove child elements that have the same name as their parent
677
+ // and inherit cursor=pointer property if child had it
678
+ const filteredChildren = [];
679
+ for (const child of node.children) {
680
+ if (typeof child !== 'string' && child.name && node.name) {
681
+ const childName = child.name.trim();
682
+ const parentName = node.name.trim();
683
+ if (childName === parentName) {
684
+ // If child has same name as parent, merge its children into parent
685
+ filteredChildren.push(...(child.children || []));
686
+
687
+ // Inherit cursor=pointer from merged child
688
+ if (child.element && receivesPointerEvents(child.element) && hasPointerCursor(child.element)) {
689
+ node.inheritedCursor = true;
690
+ }
691
+
692
+ // Also inherit other properties if needed
693
+ if (child.disabled && !node.disabled) node.disabled = child.disabled;
694
+ if (child.selected && !node.selected) node.selected = child.selected;
695
+ } else {
696
+ filteredChildren.push(child);
697
+ }
698
+ } else {
699
+ filteredChildren.push(child);
700
+ }
701
+ }
702
+ node.children = filteredChildren;
703
+
704
+ // Also handle the case where we have only one child with same name
705
+ if (node.children.length === 1 && typeof node.children[0] !== 'string') {
706
+ const child = node.children[0];
707
+ if (child.name && node.name && child.name.trim() === node.name.trim()) {
708
+ // Inherit cursor=pointer from the child being merged
709
+ if (child.element && receivesPointerEvents(child.element) && hasPointerCursor(child.element)) {
710
+ node.inheritedCursor = true;
711
+ }
712
+
713
+ // Also inherit other properties
714
+ if (child.disabled && !node.disabled) node.disabled = child.disabled;
715
+ if (child.selected && !node.selected) node.selected = child.selected;
716
+
717
+ // Merge child's children into parent and remove the redundant child
718
+ node.children = child.children || [];
719
+ }
720
+ }
721
+
722
+ // A 'generic' role that just wraps a single other element is redundant.
723
+ // We lift its child up to replace it, simplifying the hierarchy.
724
+ const isRedundantWrapper = node.role === 'generic' && node.children.length === 1 && typeof node.children[0] !== 'string';
725
+ if (isRedundantWrapper) {
726
+ return node.children;
727
+ }
728
+ return [node];
729
+ }
730
+
731
+ /**
732
+ * Phase 3: Render the normalized tree into the final string format.
733
+ * Complete preservation of snapshot.js renderTree logic with Playwright enhancements
734
+ */
735
+ function renderTree(node, indent = '') {
736
+ const lines = [];
737
+ let meaningfulProps = '';
738
+ if (node.disabled) meaningfulProps += ' [disabled]';
739
+ if (node.occluded) meaningfulProps += ' [occluded]';
740
+ if (node.checked !== undefined) meaningfulProps += ` checked=${node.checked}`;
741
+ if (node.expanded !== undefined) meaningfulProps += ` expanded=${node.expanded}`;
742
+ if (node.selected) meaningfulProps += ' [selected]';
743
+
744
+ // Add level attribute following Playwright's format
745
+ if (node.level !== undefined) meaningfulProps += ` [level=${node.level}]`;
746
+
747
+ const ref = node.ref ? ` [ref=${node.ref}]` : '';
748
+
749
+ // Add cursor=pointer detection following Playwright's implementation
750
+ // Check both direct cursor and inherited cursor from merged children
751
+ let cursor = '';
752
+ const hasDirectCursor = node.element && receivesPointerEvents(node.element) && hasPointerCursor(node.element);
753
+ const hasInheritedCursor = node.inheritedCursor;
754
+
755
+ // Only add cursor=pointer if element is not occluded
756
+ if ((hasDirectCursor || hasInheritedCursor) && !node.occluded) {
757
+ cursor = ' [cursor=pointer]';
758
+ }
759
+
760
+ const name = (node.name || '').replace(/\s+/g, ' ').trim();
761
+
762
+ // Skip elements with empty names and no meaningful props (ref and cursor are not considered meaningful for this check)
763
+ if (!name && !meaningfulProps) {
764
+ // If element has no name and no meaningful props, render its children directly at current level
765
+ for (const child of node.children) {
766
+ if (typeof child === 'string') {
767
+ const childText = child.replace(/\s+/g, ' ').trim();
768
+ if (childText) { // Only add non-empty text
769
+ lines.push(`${indent}- text "${childText}"`);
770
+ }
771
+ } else {
772
+ lines.push(...renderTree(child, indent));
773
+ }
774
+ }
775
+ return lines;
776
+ }
777
+
778
+ lines.push(`${indent}- ${node.role}${name ? ` "${name}"` : ''}${meaningfulProps}${ref}${cursor}`);
779
+
780
+ for (const child of node.children) {
781
+ if (typeof child === 'string') {
782
+ const childText = child.replace(/\s+/g, ' ').trim();
783
+ if (childText) { // Only add non-empty text
784
+ lines.push(`${indent} - text "${childText}"`);
785
+ }
786
+ } else {
787
+ lines.push(...renderTree(child, indent + ' '));
788
+ }
789
+ }
790
+ return lines;
791
+ }
792
+
793
+ function processDocument(doc) {
794
+ if (!doc.body) return [];
795
+
796
+ // Clear cache for each new document processing.
797
+ textCache.clear();
798
+ let tree = buildAriaTree(doc.body);
799
+ [tree] = normalizeTree(tree);
800
+
801
+ const lines = renderTree(tree).slice(1); // Skip the root node line
802
+
803
+ const frames = doc.querySelectorAll('iframe');
804
+ for (const frame of frames) {
805
+ try {
806
+ if (frame.contentDocument) {
807
+ lines.push(...processDocument(frame.contentDocument));
808
+ }
809
+ } catch (e) {
810
+ // Skip cross-origin iframes
811
+ }
812
+ }
813
+ return lines;
814
+ }
815
+
816
+ // === Visual analysis functions from page_script.js ===
817
+
818
+ // From page_script.js - check if element is topmost at coordinates
819
+ function isTopmost(element, x, y) {
820
+ let hit = document.elementFromPoint(x, y);
821
+ if (hit === null) return true;
822
+
823
+ while (hit) {
824
+ if (hit == element) return true;
825
+ hit = hit.parentNode;
826
+ }
827
+ return false;
828
+ }
829
+
830
+ // From page_script.js - get visual coordinates
831
+ function getElementCoordinates(element) {
832
+ let rects = element.getClientRects();
833
+ let scale = window.devicePixelRatio || 1;
834
+ let validRects = [];
835
+
836
+ for (const rect of rects) {
837
+ let x = rect.left + rect.width / 2;
838
+ let y = rect.top + rect.height / 2;
839
+ if (isTopmost(element, x, y)) {
840
+ validRects.push({
841
+ x: rect.x * scale,
842
+ y: rect.y * scale,
843
+ width: rect.width * scale,
844
+ height: rect.height * scale,
845
+ top: rect.top * scale,
846
+ left: rect.left * scale,
847
+ right: rect.right * scale,
848
+ bottom: rect.bottom * scale
849
+ });
850
+ }
851
+ }
852
+
853
+ return validRects;
854
+ }
855
+
856
+ // === Unified analysis function ===
857
+
858
+ function collectElementsFromTree(node, elementsMap) {
859
+ if (typeof node === 'string') return;
860
+
861
+ if (node.element && node.ref) {
862
+ // Get visual coordinates for this element
863
+ const coordinates = getElementCoordinates(node.element);
864
+
865
+ // Store comprehensive element information
866
+ elementsMap[node.ref] = {
867
+ // Structural information (preserved from snapshot.js)
868
+ role: node.role,
869
+ name: node.name,
870
+ tagName: node.element.tagName.toLowerCase(),
871
+ disabled: node.disabled,
872
+ checked: node.checked,
873
+ expanded: node.expanded,
874
+ level: node.level,
875
+
876
+ // Visual information (from page_script.js)
877
+ coordinates: coordinates,
878
+
879
+ // Additional metadata
880
+ href: node.element.href || null,
881
+ value: node.element.value || null,
882
+ placeholder: node.element.placeholder || null,
883
+ scrollable: node.element.scrollHeight > node.element.clientHeight,
884
+
885
+ // Playwright-inspired properties
886
+ receivesPointerEvents: receivesPointerEvents(node.element),
887
+ hasPointerCursor: hasPointerCursor(node.element)
888
+ };
889
+ }
890
+
891
+ // Recursively process children
892
+ if (node.children) {
893
+ for (const child of node.children) {
894
+ collectElementsFromTree(child, elementsMap);
895
+ }
896
+ }
897
+ }
898
+
899
+ function analyzePageElements() {
900
+ // Clean up stale refs before analysis
901
+ const cleanedRefCount = cleanupStaleRefs();
902
+
903
+ // Performance optimization: Check if we can reuse recent analysis
904
+ const currentTime = Date.now();
905
+ const lastAnalysisTime = window.__camelLastAnalysisTime || 0;
906
+ const timeSinceLastAnalysis = currentTime - lastAnalysisTime;
907
+
908
+ // If less than 1 second since last analysis and page hasn't changed significantly
909
+ if (timeSinceLastAnalysis < 1000 && window.__camelLastAnalysisResult && cleanedRefCount === 0) {
910
+ const cachedResult = window.__camelLastAnalysisResult;
911
+ // Update timestamp and memory info in cached result
912
+ cachedResult.metadata.timestamp = new Date().toISOString();
913
+ cachedResult.metadata.memoryInfo = {
914
+ currentRefCount: refElementMap.size,
915
+ maxRefs: MAX_REFS,
916
+ memoryUtilization: (refElementMap.size / MAX_REFS * 100).toFixed(1) + '%',
917
+ lruAccessTimesCount: refAccessTimes.size
918
+ };
919
+ cachedResult.metadata.cacheHit = true;
920
+ return cachedResult;
921
+ }
922
+
923
+ // Generate the complete structured snapshot using original snapshot.js logic
924
+ const outputLines = processDocument(document);
925
+ const snapshotText = outputLines.join('\n');
926
+
927
+ // Build the tree again to collect element information with visual data
928
+ textCache.clear();
929
+ // Note: Don't reset refCounter anymore - use persistent counters
930
+ let tree = buildAriaTree(document.body);
931
+ [tree] = normalizeTree(tree);
932
+
933
+ const elementsMap = {};
934
+ collectElementsFromTree(tree, elementsMap);
935
+
936
+ // Verify uniqueness of aria-ref attributes (debugging aid)
937
+ const ariaRefCounts = {};
938
+ document.querySelectorAll('[aria-ref]').forEach(element => {
939
+ const ref = element.getAttribute('aria-ref');
940
+ ariaRefCounts[ref] = (ariaRefCounts[ref] || 0) + 1;
941
+ });
942
+
943
+ // Log any duplicates for debugging
944
+ const duplicateRefs = Object.entries(ariaRefCounts).filter(([ref, count]) => count > 1);
945
+ if (duplicateRefs.length > 0) {
946
+ console.warn('Duplicate aria-ref attributes found:', duplicateRefs);
947
+ }
948
+
949
+ // Validate ref consistency
950
+ const refValidationErrors = [];
951
+ for (const [ref, elementInfo] of Object.entries(elementsMap)) {
952
+ const mappedElement = refElementMap.get(ref);
953
+ if (!mappedElement || !document.contains(mappedElement)) {
954
+ refValidationErrors.push(`Ref ${ref} points to invalid or removed element`);
955
+ }
956
+ }
957
+
958
+ const result = {
959
+ url: window.location.href,
960
+ elements: elementsMap,
961
+ snapshotText: snapshotText,
962
+ metadata: {
963
+ timestamp: new Date().toISOString(),
964
+ elementCount: Object.keys(elementsMap).length,
965
+ screenInfo: {
966
+ width: window.innerWidth,
967
+ height: window.innerHeight,
968
+ devicePixelRatio: window.devicePixelRatio || 1
969
+ },
970
+ // Enhanced debugging information
971
+ ariaRefCounts: ariaRefCounts,
972
+ duplicateRefsFound: duplicateRefs.length > 0,
973
+ staleRefsCleanedUp: cleanedRefCount,
974
+ refValidationErrors: refValidationErrors,
975
+ totalMappedRefs: refElementMap.size,
976
+ refCounterValue: refCounter,
977
+ // Memory management information
978
+ memoryInfo: {
979
+ currentRefCount: refElementMap.size,
980
+ maxRefs: MAX_REFS,
981
+ memoryUtilization: (refElementMap.size / MAX_REFS * 100).toFixed(1) + '%',
982
+ lruAccessTimesCount: refAccessTimes.size,
983
+ unusedAgeThreshold: MAX_UNUSED_AGE_MS + 'ms',
984
+ cleanupThreshold: (CLEANUP_THRESHOLD * 100).toFixed(0) + '%',
985
+ isAggressiveCleanup: refElementMap.size > (MAX_REFS * CLEANUP_THRESHOLD)
986
+ },
987
+ // Performance information
988
+ cacheHit: false,
989
+ analysisTime: Date.now() - currentTime
990
+ }
991
+ };
992
+
993
+ // Cache the result for potential reuse
994
+ window.__camelLastAnalysisResult = result;
995
+ window.__camelLastAnalysisTime = currentTime;
996
+
997
+ return result;
998
+ }
999
+
1000
+ // Execute analysis and return result
1001
+ return analyzePageElements();
1002
+ })();