bb-browser-api 0.11.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1501 @@
1
+ window.buildDomTree = (
2
+ args = {
3
+ showHighlightElements: true,
4
+ focusHighlightIndex: -1,
5
+ viewportExpansion: 0,
6
+ debugMode: false,
7
+ startId: 0,
8
+ startHighlightIndex: 0,
9
+ },
10
+ ) => {
11
+ const { showHighlightElements, focusHighlightIndex, viewportExpansion, startHighlightIndex, startId, debugMode } =
12
+ args;
13
+ // Make sure to do highlight elements always, but we can hide the highlights if needed
14
+ const doHighlightElements = true;
15
+
16
+ let highlightIndex = startHighlightIndex; // Reset highlight index
17
+
18
+ // Add caching mechanisms at the top level
19
+ const DOM_CACHE = {
20
+ boundingRects: new WeakMap(),
21
+ clientRects: new WeakMap(),
22
+ computedStyles: new WeakMap(),
23
+ clearCache: () => {
24
+ DOM_CACHE.boundingRects = new WeakMap();
25
+ DOM_CACHE.clientRects = new WeakMap();
26
+ DOM_CACHE.computedStyles = new WeakMap();
27
+ },
28
+ };
29
+
30
+ /**
31
+ * Gets the cached bounding rect for an element.
32
+ *
33
+ * @param {HTMLElement} element - The element to get the bounding rect for.
34
+ * @returns {DOMRect | null} The cached bounding rect, or null if the element is not found.
35
+ */
36
+ function getCachedBoundingRect(element) {
37
+ if (!element) return null;
38
+
39
+ if (DOM_CACHE.boundingRects.has(element)) {
40
+ return DOM_CACHE.boundingRects.get(element);
41
+ }
42
+
43
+ const rect = element.getBoundingClientRect();
44
+
45
+ if (rect) {
46
+ DOM_CACHE.boundingRects.set(element, rect);
47
+ }
48
+ return rect;
49
+ }
50
+
51
+ /**
52
+ * Gets the cached computed style for an element.
53
+ *
54
+ * @param {HTMLElement} element - The element to get the computed style for.
55
+ * @returns {CSSStyleDeclaration | null} The cached computed style, or null if the element is not found.
56
+ */
57
+ function getCachedComputedStyle(element) {
58
+ if (!element) return null;
59
+
60
+ if (DOM_CACHE.computedStyles.has(element)) {
61
+ return DOM_CACHE.computedStyles.get(element);
62
+ }
63
+
64
+ const style = window.getComputedStyle(element);
65
+
66
+ if (style) {
67
+ DOM_CACHE.computedStyles.set(element, style);
68
+ }
69
+ return style;
70
+ }
71
+
72
+ /**
73
+ * Gets the cached client rects for an element.
74
+ *
75
+ * @param {HTMLElement} element - The element to get the client rects for.
76
+ * @returns {DOMRectList | null} The cached client rects, or null if the element is not found.
77
+ */
78
+ function getCachedClientRects(element) {
79
+ if (!element) return null;
80
+
81
+ if (DOM_CACHE.clientRects.has(element)) {
82
+ return DOM_CACHE.clientRects.get(element);
83
+ }
84
+
85
+ const rects = element.getClientRects();
86
+
87
+ if (rects) {
88
+ DOM_CACHE.clientRects.set(element, rects);
89
+ }
90
+ return rects;
91
+ }
92
+
93
+ /**
94
+ * Hash map of DOM nodes indexed by their highlight index.
95
+ *
96
+ * @type {Object<string, any>}
97
+ */
98
+ const DOM_HASH_MAP = {};
99
+
100
+ const ID = { current: startId };
101
+
102
+ const HIGHLIGHT_CONTAINER_ID = 'playwright-highlight-container';
103
+
104
+ // Clean up any previous highlights before building a new tree
105
+ if (window._highlightCleanupFunctions && window._highlightCleanupFunctions.length) {
106
+ window._highlightCleanupFunctions.forEach(fn => { try { fn(); } catch {} });
107
+ window._highlightCleanupFunctions = [];
108
+ }
109
+ const existingContainer = document.getElementById(HIGHLIGHT_CONTAINER_ID);
110
+ if (existingContainer) existingContainer.remove();
111
+
112
+ // Add a WeakMap cache for XPath strings
113
+ const xpathCache = new WeakMap();
114
+
115
+ // // Initialize once and reuse
116
+ // const viewportObserver = new IntersectionObserver(
117
+ // (entries) => {
118
+ // entries.forEach(entry => {
119
+ // elementVisibilityMap.set(entry.target, entry.isIntersecting);
120
+ // });
121
+ // },
122
+ // { rootMargin: `${viewportExpansion}px` }
123
+ // );
124
+
125
+ /**
126
+ * Highlights an element in the DOM and returns the index of the next element.
127
+ *
128
+ * @param {HTMLElement} element - The element to highlight.
129
+ * @param {number} index - The index of the element.
130
+ * @param {HTMLElement | null} parentIframe - The parent iframe node.
131
+ * @returns {number} The index of the next element.
132
+ */
133
+ function highlightElement(element, index, parentIframe = null) {
134
+ if (!element) return index;
135
+
136
+ const overlays = [];
137
+ /**
138
+ * @type {HTMLElement | null}
139
+ */
140
+ let label = null;
141
+ let labelWidth = 20;
142
+ let labelHeight = 16;
143
+ let cleanupFn = null;
144
+
145
+ try {
146
+ // Create or get highlight container
147
+ let container = document.getElementById(HIGHLIGHT_CONTAINER_ID);
148
+ if (!container) {
149
+ container = document.createElement('div');
150
+ container.id = HIGHLIGHT_CONTAINER_ID;
151
+ container.style.position = 'fixed';
152
+ container.style.pointerEvents = 'none';
153
+ container.style.top = '0';
154
+ container.style.left = '0';
155
+ container.style.width = '100%';
156
+ container.style.height = '100%';
157
+ // Use the maximum valid value in zIndex to ensure the element is not blocked by overlapping elements.
158
+ container.style.zIndex = '2147483647';
159
+ container.style.backgroundColor = 'transparent';
160
+ // Show or hide the container based on the showHighlightElements flag
161
+ container.style.display = showHighlightElements ? 'block' : 'none';
162
+ document.body.appendChild(container);
163
+ }
164
+
165
+ // Get element client rects
166
+ const rects = element.getClientRects(); // Use getClientRects()
167
+
168
+ if (!rects || rects.length === 0) return index; // Exit if no rects
169
+
170
+ // Generate a color based on the index
171
+ const colors = [
172
+ '#FF0000',
173
+ '#00FF00',
174
+ '#0000FF',
175
+ '#FFA500',
176
+ '#800080',
177
+ '#008080',
178
+ '#FF69B4',
179
+ '#4B0082',
180
+ '#FF4500',
181
+ '#2E8B57',
182
+ '#DC143C',
183
+ '#4682B4',
184
+ ];
185
+ const colorIndex = index % colors.length;
186
+ const baseColor = colors[colorIndex];
187
+ const backgroundColor = baseColor + '1A'; // 10% opacity version of the color
188
+
189
+ // Get iframe offset if necessary
190
+ let iframeOffset = { x: 0, y: 0 };
191
+ if (parentIframe) {
192
+ const iframeRect = parentIframe.getBoundingClientRect(); // Keep getBoundingClientRect for iframe offset
193
+ iframeOffset.x = iframeRect.left;
194
+ iframeOffset.y = iframeRect.top;
195
+ }
196
+
197
+ // Create fragment to hold overlay elements
198
+ const fragment = document.createDocumentFragment();
199
+
200
+ // Create highlight overlays for each client rect
201
+ for (const rect of rects) {
202
+ if (rect.width === 0 || rect.height === 0) continue; // Skip empty rects
203
+
204
+ const overlay = document.createElement('div');
205
+ overlay.style.position = 'fixed';
206
+ overlay.style.border = `2px solid ${baseColor}`;
207
+ overlay.style.backgroundColor = backgroundColor;
208
+ overlay.style.pointerEvents = 'none';
209
+ overlay.style.boxSizing = 'border-box';
210
+
211
+ const top = rect.top + iframeOffset.y;
212
+ const left = rect.left + iframeOffset.x;
213
+
214
+ overlay.style.top = `${top}px`;
215
+ overlay.style.left = `${left}px`;
216
+ overlay.style.width = `${rect.width}px`;
217
+ overlay.style.height = `${rect.height}px`;
218
+
219
+ fragment.appendChild(overlay);
220
+ overlays.push({ element: overlay, initialRect: rect }); // Store overlay and its rect
221
+ }
222
+
223
+ // Create and position a single label relative to the first rect
224
+ const firstRect = rects[0];
225
+ label = document.createElement('div');
226
+ label.className = 'playwright-highlight-label';
227
+ label.style.position = 'fixed';
228
+ label.style.background = baseColor;
229
+ label.style.color = 'white';
230
+ label.style.padding = '1px 4px';
231
+ label.style.borderRadius = '4px';
232
+ label.style.fontSize = `${Math.min(12, Math.max(8, firstRect.height / 2))}px`;
233
+ label.textContent = index.toString();
234
+
235
+ labelWidth = label.offsetWidth > 0 ? label.offsetWidth : labelWidth; // Update actual width if possible
236
+ labelHeight = label.offsetHeight > 0 ? label.offsetHeight : labelHeight; // Update actual height if possible
237
+
238
+ const firstRectTop = firstRect.top + iframeOffset.y;
239
+ const firstRectLeft = firstRect.left + iframeOffset.x;
240
+
241
+ let labelTop = firstRectTop + 2;
242
+ let labelLeft = firstRectLeft + firstRect.width - labelWidth - 2;
243
+
244
+ // Adjust label position if first rect is too small
245
+ if (firstRect.width < labelWidth + 4 || firstRect.height < labelHeight + 4) {
246
+ labelTop = firstRectTop - labelHeight - 2;
247
+ labelLeft = firstRectLeft + firstRect.width - labelWidth; // Align with right edge
248
+ if (labelLeft < iframeOffset.x) labelLeft = firstRectLeft; // Prevent going off-left
249
+ }
250
+
251
+ // Ensure label stays within viewport bounds slightly better
252
+ labelTop = Math.max(0, Math.min(labelTop, window.innerHeight - labelHeight));
253
+ labelLeft = Math.max(0, Math.min(labelLeft, window.innerWidth - labelWidth));
254
+
255
+ label.style.top = `${labelTop}px`;
256
+ label.style.left = `${labelLeft}px`;
257
+
258
+ fragment.appendChild(label);
259
+
260
+ // Update positions on scroll/resize
261
+ const updatePositions = () => {
262
+ const newRects = element.getClientRects(); // Get fresh rects
263
+ let newIframeOffset = { x: 0, y: 0 };
264
+
265
+ if (parentIframe) {
266
+ const iframeRect = parentIframe.getBoundingClientRect(); // Keep getBoundingClientRect for iframe
267
+ newIframeOffset.x = iframeRect.left;
268
+ newIframeOffset.y = iframeRect.top;
269
+ }
270
+
271
+ // Update each overlay
272
+ overlays.forEach((overlayData, i) => {
273
+ if (i < newRects.length) {
274
+ // Check if rect still exists
275
+ const newRect = newRects[i];
276
+ const newTop = newRect.top + newIframeOffset.y;
277
+ const newLeft = newRect.left + newIframeOffset.x;
278
+
279
+ overlayData.element.style.top = `${newTop}px`;
280
+ overlayData.element.style.left = `${newLeft}px`;
281
+ overlayData.element.style.width = `${newRect.width}px`;
282
+ overlayData.element.style.height = `${newRect.height}px`;
283
+ overlayData.element.style.display = newRect.width === 0 || newRect.height === 0 ? 'none' : 'block';
284
+ } else {
285
+ // If fewer rects now, hide extra overlays
286
+ overlayData.element.style.display = 'none';
287
+ }
288
+ });
289
+
290
+ // If there are fewer new rects than overlays, hide the extras
291
+ if (newRects.length < overlays.length) {
292
+ for (let i = newRects.length; i < overlays.length; i++) {
293
+ overlays[i].element.style.display = 'none';
294
+ }
295
+ }
296
+
297
+ // Update label position based on the first new rect
298
+ if (label && newRects.length > 0) {
299
+ const firstNewRect = newRects[0];
300
+ const firstNewRectTop = firstNewRect.top + newIframeOffset.y;
301
+ const firstNewRectLeft = firstNewRect.left + newIframeOffset.x;
302
+
303
+ let newLabelTop = firstNewRectTop + 2;
304
+ let newLabelLeft = firstNewRectLeft + firstNewRect.width - labelWidth - 2;
305
+
306
+ if (firstNewRect.width < labelWidth + 4 || firstNewRect.height < labelHeight + 4) {
307
+ newLabelTop = firstNewRectTop - labelHeight - 2;
308
+ newLabelLeft = firstNewRectLeft + firstNewRect.width - labelWidth;
309
+ if (newLabelLeft < newIframeOffset.x) newLabelLeft = firstNewRectLeft;
310
+ }
311
+
312
+ // Ensure label stays within viewport bounds
313
+ newLabelTop = Math.max(0, Math.min(newLabelTop, window.innerHeight - labelHeight));
314
+ newLabelLeft = Math.max(0, Math.min(newLabelLeft, window.innerWidth - labelWidth));
315
+
316
+ label.style.top = `${newLabelTop}px`;
317
+ label.style.left = `${newLabelLeft}px`;
318
+ label.style.display = 'block';
319
+ } else if (label) {
320
+ // Hide label if element has no rects anymore
321
+ label.style.display = 'none';
322
+ }
323
+ };
324
+
325
+ const throttleFunction = (func, delay) => {
326
+ let lastCall = 0;
327
+ return (...args) => {
328
+ const now = performance.now();
329
+ if (now - lastCall < delay) return;
330
+ lastCall = now;
331
+ return func(...args);
332
+ };
333
+ };
334
+
335
+ const throttledUpdatePositions = throttleFunction(updatePositions, 16); // ~60fps
336
+ window.addEventListener('scroll', throttledUpdatePositions, true);
337
+ window.addEventListener('resize', throttledUpdatePositions);
338
+
339
+ // Add cleanup function
340
+ cleanupFn = () => {
341
+ window.removeEventListener('scroll', throttledUpdatePositions, true);
342
+ window.removeEventListener('resize', throttledUpdatePositions);
343
+ // Remove overlay elements if needed
344
+ overlays.forEach(overlay => overlay.element.remove());
345
+ if (label) label.remove();
346
+ };
347
+
348
+ // Then add fragment to container in one operation
349
+ container.appendChild(fragment);
350
+
351
+ return index + 1;
352
+ } finally {
353
+ // Store cleanup function for later use
354
+ if (cleanupFn) {
355
+ // Keep a reference to cleanup functions in a global array
356
+ (window._highlightCleanupFunctions = window._highlightCleanupFunctions || []).push(cleanupFn);
357
+ }
358
+ }
359
+ }
360
+
361
+ /**
362
+ * Gets the position of an element in its parent.
363
+ *
364
+ * @param {HTMLElement} currentElement - The element to get the position for.
365
+ * @returns {number} The position of the element in its parent.
366
+ */
367
+ function getElementPosition(currentElement) {
368
+ if (!currentElement.parentElement) {
369
+ return 0; // No parent means no siblings
370
+ }
371
+
372
+ const tagName = currentElement.nodeName.toLowerCase();
373
+
374
+ const siblings = Array.from(currentElement.parentElement.children).filter(
375
+ sib => sib.nodeName.toLowerCase() === tagName,
376
+ );
377
+
378
+ if (siblings.length === 1) {
379
+ return 0; // Only element of its type
380
+ }
381
+
382
+ const index = siblings.indexOf(currentElement) + 1; // 1-based index
383
+ return index;
384
+ }
385
+
386
+ function getXPathTree(element, stopAtBoundary = true) {
387
+ if (xpathCache.has(element)) return xpathCache.get(element);
388
+
389
+ const segments = [];
390
+ let currentElement = element;
391
+
392
+ while (currentElement && currentElement.nodeType === Node.ELEMENT_NODE) {
393
+ // Stop if we hit a shadow root or iframe
394
+ if (
395
+ stopAtBoundary &&
396
+ (currentElement.parentNode instanceof ShadowRoot || currentElement.parentNode instanceof HTMLIFrameElement)
397
+ ) {
398
+ break;
399
+ }
400
+
401
+ const position = getElementPosition(currentElement);
402
+ const tagName = currentElement.nodeName.toLowerCase();
403
+ const xpathIndex = position > 0 ? `[${position}]` : '';
404
+ segments.unshift(`${tagName}${xpathIndex}`);
405
+
406
+ currentElement = currentElement.parentNode;
407
+ }
408
+
409
+ const result = segments.join('/');
410
+ xpathCache.set(element, result);
411
+ return result;
412
+ }
413
+
414
+ /**
415
+ * Checks if a text node is visible.
416
+ *
417
+ * @param {Text} textNode - The text node to check.
418
+ * @returns {boolean} Whether the text node is visible.
419
+ */
420
+ function isTextNodeVisible(textNode) {
421
+ try {
422
+ // Special case: when viewportExpansion is -1, consider all text nodes as visible
423
+ if (viewportExpansion === -1) {
424
+ // Still check parent visibility for basic filtering
425
+ const parentElement = textNode.parentElement;
426
+ if (!parentElement) return false;
427
+
428
+ try {
429
+ return parentElement.checkVisibility({
430
+ checkOpacity: true,
431
+ checkVisibilityCSS: true,
432
+ });
433
+ } catch (e) {
434
+ // Fallback if checkVisibility is not supported
435
+ const style = window.getComputedStyle(parentElement);
436
+ return style.display !== 'none' && style.visibility !== 'hidden' && style.opacity !== '0';
437
+ }
438
+ }
439
+
440
+ const range = document.createRange();
441
+ range.selectNodeContents(textNode);
442
+ const rects = range.getClientRects(); // Use getClientRects for Range
443
+
444
+ if (!rects || rects.length === 0) {
445
+ return false;
446
+ }
447
+
448
+ let isAnyRectVisible = false;
449
+ let isAnyRectInViewport = false;
450
+
451
+ for (const rect of rects) {
452
+ // Check size
453
+ if (rect.width > 0 && rect.height > 0) {
454
+ isAnyRectVisible = true;
455
+
456
+ // Viewport check for this rect
457
+ if (
458
+ !(
459
+ rect.bottom < -viewportExpansion ||
460
+ rect.top > window.innerHeight + viewportExpansion ||
461
+ rect.right < -viewportExpansion ||
462
+ rect.left > window.innerWidth + viewportExpansion
463
+ )
464
+ ) {
465
+ isAnyRectInViewport = true;
466
+ break; // Found a visible rect in viewport, no need to check others
467
+ }
468
+ }
469
+ }
470
+
471
+ if (!isAnyRectVisible || !isAnyRectInViewport) {
472
+ return false;
473
+ }
474
+
475
+ // Check parent visibility
476
+ const parentElement = textNode.parentElement;
477
+ if (!parentElement) return false;
478
+
479
+ try {
480
+ return parentElement.checkVisibility({
481
+ checkOpacity: true,
482
+ checkVisibilityCSS: true,
483
+ });
484
+ } catch (e) {
485
+ // Fallback if checkVisibility is not supported
486
+ const style = window.getComputedStyle(parentElement);
487
+ return style.display !== 'none' && style.visibility !== 'hidden' && style.opacity !== '0';
488
+ }
489
+ } catch (e) {
490
+ console.warn('Error checking text node visibility:', e);
491
+ return false;
492
+ }
493
+ }
494
+
495
+ /**
496
+ * Checks if an element is accepted.
497
+ *
498
+ * @param {HTMLElement} element - The element to check.
499
+ * @returns {boolean} Whether the element is accepted.
500
+ */
501
+ function isElementAccepted(element) {
502
+ if (!element || !element.tagName) return false;
503
+
504
+ // Always accept body and common container elements
505
+ const alwaysAccept = new Set(['body', 'div', 'main', 'article', 'section', 'nav', 'header', 'footer']);
506
+ const tagName = element.tagName.toLowerCase();
507
+
508
+ if (alwaysAccept.has(tagName)) return true;
509
+
510
+ const leafElementDenyList = new Set(['svg', 'script', 'style', 'link', 'meta', 'noscript', 'template']);
511
+
512
+ return !leafElementDenyList.has(tagName);
513
+ }
514
+
515
+ /**
516
+ * Checks if an element is visible.
517
+ *
518
+ * @param {HTMLElement} element - The element to check.
519
+ * @returns {boolean} Whether the element is visible.
520
+ */
521
+ function isElementVisible(element) {
522
+ const style = getCachedComputedStyle(element);
523
+ return (
524
+ element.offsetWidth > 0 && element.offsetHeight > 0 && style?.visibility !== 'hidden' && style?.display !== 'none'
525
+ );
526
+ }
527
+
528
+ /**
529
+ * Checks if an element is interactive.
530
+ *
531
+ * lots of comments, and uncommented code - to show the logic of what we already tried
532
+ *
533
+ * One of the things we tried at the beginning was also to use event listeners, and other fancy class, style stuff -> what actually worked best was just combining most things with computed cursor style :)
534
+ *
535
+ * @param {HTMLElement} element - The element to check.
536
+ */
537
+ function isInteractiveElement(element) {
538
+ if (!element || element.nodeType !== Node.ELEMENT_NODE) {
539
+ return false;
540
+ }
541
+
542
+ // Cache the tagName and style lookups
543
+ const tagName = element.tagName.toLowerCase();
544
+ const style = getCachedComputedStyle(element);
545
+
546
+ // Define interactive cursors
547
+ const interactiveCursors = new Set([
548
+ 'pointer', // Link/clickable elements
549
+ 'move', // Movable elements
550
+ 'text', // Text selection
551
+ 'grab', // Grabbable elements
552
+ 'grabbing', // Currently grabbing
553
+ 'cell', // Table cell selection
554
+ 'copy', // Copy operation
555
+ 'alias', // Alias creation
556
+ 'all-scroll', // Scrollable content
557
+ 'col-resize', // Column resize
558
+ 'context-menu', // Context menu available
559
+ 'crosshair', // Precise selection
560
+ 'e-resize', // East resize
561
+ 'ew-resize', // East-west resize
562
+ 'help', // Help available
563
+ 'n-resize', // North resize
564
+ 'ne-resize', // Northeast resize
565
+ 'nesw-resize', // Northeast-southwest resize
566
+ 'ns-resize', // North-south resize
567
+ 'nw-resize', // Northwest resize
568
+ 'nwse-resize', // Northwest-southeast resize
569
+ 'row-resize', // Row resize
570
+ 's-resize', // South resize
571
+ 'se-resize', // Southeast resize
572
+ 'sw-resize', // Southwest resize
573
+ 'vertical-text', // Vertical text selection
574
+ 'w-resize', // West resize
575
+ 'zoom-in', // Zoom in
576
+ 'zoom-out', // Zoom out
577
+ ]);
578
+
579
+ // Define non-interactive cursors
580
+ const nonInteractiveCursors = new Set([
581
+ 'not-allowed', // Action not allowed
582
+ 'no-drop', // Drop not allowed
583
+ 'wait', // Processing
584
+ 'progress', // In progress
585
+ 'initial', // Initial value
586
+ 'inherit', // Inherited value
587
+ //? Let's just include all potentially clickable elements that are not specifically blocked
588
+ // 'none', // No cursor
589
+ // 'default', // Default cursor
590
+ // 'auto', // Browser default
591
+ ]);
592
+
593
+ /**
594
+ * Checks if an element has an interactive pointer.
595
+ *
596
+ * @param {HTMLElement} element - The element to check.
597
+ * @returns {boolean} Whether the element has an interactive pointer.
598
+ */
599
+ function doesElementHaveInteractivePointer(element) {
600
+ if (element.tagName.toLowerCase() === 'html') return false;
601
+
602
+ if (style?.cursor && interactiveCursors.has(style.cursor)) return true;
603
+
604
+ return false;
605
+ }
606
+
607
+ let isInteractiveCursor = doesElementHaveInteractivePointer(element);
608
+
609
+ // Genius fix for almost all interactive elements
610
+ if (isInteractiveCursor) {
611
+ return true;
612
+ }
613
+
614
+ const interactiveElements = new Set([
615
+ 'a', // Links
616
+ 'button', // Buttons
617
+ 'input', // All input types (text, checkbox, radio, etc.)
618
+ 'select', // Dropdown menus
619
+ 'textarea', // Text areas
620
+ 'details', // Expandable details
621
+ 'summary', // Summary element (clickable part of details)
622
+ 'label', // Form labels (often clickable)
623
+ 'option', // Select options
624
+ 'optgroup', // Option groups
625
+ 'fieldset', // Form fieldsets (can be interactive with legend)
626
+ 'legend', // Fieldset legends
627
+ ]);
628
+
629
+ // Define explicit disable attributes and properties
630
+ const explicitDisableTags = new Set([
631
+ 'disabled', // Standard disabled attribute
632
+ // 'aria-disabled', // ARIA disabled state
633
+ 'readonly', // Read-only state
634
+ // 'aria-readonly', // ARIA read-only state
635
+ // 'aria-hidden', // Hidden from accessibility
636
+ // 'hidden', // Hidden attribute
637
+ // 'inert', // Inert attribute
638
+ // 'aria-inert', // ARIA inert state
639
+ // 'tabindex="-1"', // Removed from tab order
640
+ // 'aria-hidden="true"' // Hidden from screen readers
641
+ ]);
642
+
643
+ // handle inputs, select, checkbox, radio, textarea, button and make sure they are not cursor style disabled/not-allowed
644
+ if (interactiveElements.has(tagName)) {
645
+ // Check for non-interactive cursor
646
+ if (style?.cursor && nonInteractiveCursors.has(style.cursor)) {
647
+ return false;
648
+ }
649
+
650
+ // Check for explicit disable attributes
651
+ for (const disableTag of explicitDisableTags) {
652
+ if (
653
+ element.hasAttribute(disableTag) ||
654
+ element.getAttribute(disableTag) === 'true' ||
655
+ element.getAttribute(disableTag) === ''
656
+ ) {
657
+ return false;
658
+ }
659
+ }
660
+
661
+ // Check for disabled property on form elements
662
+ if (element.disabled) {
663
+ return false;
664
+ }
665
+
666
+ // Check for readonly property on form elements
667
+ if (element.readOnly) {
668
+ return false;
669
+ }
670
+
671
+ // Check for inert property
672
+ if (element.inert) {
673
+ return false;
674
+ }
675
+
676
+ return true;
677
+ }
678
+
679
+ const role = element.getAttribute('role');
680
+ const ariaRole = element.getAttribute('aria-role');
681
+
682
+ // Check for contenteditable attribute
683
+ if (element.getAttribute('contenteditable') === 'true' || element.isContentEditable) {
684
+ return true;
685
+ }
686
+
687
+ // Added enhancement to capture dropdown interactive elements
688
+ if (
689
+ element.classList &&
690
+ (element.classList.contains('button') ||
691
+ element.classList.contains('dropdown-toggle') ||
692
+ element.getAttribute('data-index') ||
693
+ element.getAttribute('data-toggle') === 'dropdown' ||
694
+ element.getAttribute('aria-haspopup') === 'true')
695
+ ) {
696
+ return true;
697
+ }
698
+
699
+ const interactiveRoles = new Set([
700
+ 'button', // Directly clickable element
701
+ // 'link', // Clickable link
702
+ 'menu', // Menu container (ARIA menus)
703
+ 'menubar', // Menu bar container
704
+ 'menuitem', // Clickable menu item
705
+ 'menuitemradio', // Radio-style menu item (selectable)
706
+ 'menuitemcheckbox', // Checkbox-style menu item (toggleable)
707
+ 'radio', // Radio button (selectable)
708
+ 'checkbox', // Checkbox (toggleable)
709
+ 'tab', // Tab (clickable to switch content)
710
+ 'switch', // Toggle switch (clickable to change state)
711
+ 'slider', // Slider control (draggable)
712
+ 'spinbutton', // Number input with up/down controls
713
+ 'combobox', // Dropdown with text input
714
+ 'searchbox', // Search input field
715
+ 'textbox', // Text input field
716
+ 'listbox', // Selectable list
717
+ 'option', // Selectable option in a list
718
+ 'scrollbar', // Scrollable control
719
+ ]);
720
+
721
+ // Basic role/attribute checks
722
+ const hasInteractiveRole =
723
+ interactiveElements.has(tagName) ||
724
+ (role && interactiveRoles.has(role)) ||
725
+ (ariaRole && interactiveRoles.has(ariaRole));
726
+
727
+ if (hasInteractiveRole) return true;
728
+
729
+ // check whether element has event listeners by window.getEventListeners
730
+ try {
731
+ if (typeof getEventListeners === 'function') {
732
+ const listeners = getEventListeners(element);
733
+ const mouseEvents = ['click', 'mousedown', 'mouseup', 'dblclick'];
734
+ for (const eventType of mouseEvents) {
735
+ if (listeners[eventType] && listeners[eventType].length > 0) {
736
+ return true; // Found a mouse interaction listener
737
+ }
738
+ }
739
+ }
740
+
741
+ const getEventListenersForNode =
742
+ element?.ownerDocument?.defaultView?.getEventListenersForNode || window.getEventListenersForNode;
743
+ if (typeof getEventListenersForNode === 'function') {
744
+ const listeners = getEventListenersForNode(element);
745
+ const interactionEvents = [
746
+ 'click',
747
+ 'mousedown',
748
+ 'mouseup',
749
+ 'keydown',
750
+ 'keyup',
751
+ 'submit',
752
+ 'change',
753
+ 'input',
754
+ 'focus',
755
+ 'blur',
756
+ ];
757
+ for (const eventType of interactionEvents) {
758
+ for (const listener of listeners) {
759
+ if (listener.type === eventType) {
760
+ return true; // Found a common interaction listener
761
+ }
762
+ }
763
+ }
764
+ }
765
+ // Fallback: Check common event attributes if getEventListeners is not available (getEventListeners doesn't work in page.evaluate context)
766
+ const commonMouseAttrs = ['onclick', 'onmousedown', 'onmouseup', 'ondblclick'];
767
+ for (const attr of commonMouseAttrs) {
768
+ if (element.hasAttribute(attr) || typeof element[attr] === 'function') {
769
+ return true;
770
+ }
771
+ }
772
+ } catch (e) {
773
+ // console.warn(`Could not check event listeners for ${element.tagName}:`, e);
774
+ // If checking listeners fails, rely on other checks
775
+ }
776
+
777
+ return false;
778
+ }
779
+
780
+ /**
781
+ * Checks if an element is the topmost element at its position.
782
+ *
783
+ * @param {HTMLElement} element - The element to check.
784
+ * @returns {boolean} Whether the element is the topmost element at its position.
785
+ */
786
+ function isTopElement(element) {
787
+ // Special case: when viewportExpansion is -1, consider all elements as "top" elements
788
+ if (viewportExpansion === -1) {
789
+ return true;
790
+ }
791
+
792
+ const rects = getCachedClientRects(element); // Replace element.getClientRects()
793
+
794
+ if (!rects || rects.length === 0) {
795
+ return false; // No geometry, cannot be top
796
+ }
797
+
798
+ let isAnyRectInViewport = false;
799
+ for (const rect of rects) {
800
+ // Use the same logic as isInExpandedViewport check
801
+ if (
802
+ rect.width > 0 &&
803
+ rect.height > 0 &&
804
+ !(
805
+ // Only check non-empty rects
806
+ (
807
+ rect.bottom < -viewportExpansion ||
808
+ rect.top > window.innerHeight + viewportExpansion ||
809
+ rect.right < -viewportExpansion ||
810
+ rect.left > window.innerWidth + viewportExpansion
811
+ )
812
+ )
813
+ ) {
814
+ isAnyRectInViewport = true;
815
+ break;
816
+ }
817
+ }
818
+
819
+ if (!isAnyRectInViewport) {
820
+ return false; // All rects are outside the viewport area
821
+ }
822
+
823
+ // Find the correct document context and root element
824
+ let doc = element.ownerDocument;
825
+
826
+ // If we're in an iframe, elements are considered top by default
827
+ if (doc !== window.document) {
828
+ return true;
829
+ }
830
+
831
+ // For shadow DOM, we need to check within its own root context
832
+ const shadowRoot = element.getRootNode();
833
+ if (shadowRoot instanceof ShadowRoot) {
834
+ const centerX = rects[Math.floor(rects.length / 2)].left + rects[Math.floor(rects.length / 2)].width / 2;
835
+ const centerY = rects[Math.floor(rects.length / 2)].top + rects[Math.floor(rects.length / 2)].height / 2;
836
+
837
+ try {
838
+ const topEl = shadowRoot.elementFromPoint(centerX, centerY);
839
+ if (!topEl) return false;
840
+
841
+ let current = topEl;
842
+ while (current && current !== shadowRoot) {
843
+ if (current === element) return true;
844
+ current = current.parentElement;
845
+ }
846
+ return false;
847
+ } catch (e) {
848
+ return true;
849
+ }
850
+ }
851
+
852
+ const margin = 5;
853
+ const rect = rects[Math.floor(rects.length / 2)];
854
+
855
+ // For elements in viewport, check if they're topmost. Do the check in the
856
+ // center of the element and at the corners to ensure we catch more cases.
857
+ const checkPoints = [
858
+ // Initially only this was used, but it was not enough
859
+ { x: rect.left + rect.width / 2, y: rect.top + rect.height / 2 },
860
+ { x: rect.left + margin, y: rect.top + margin }, // top left
861
+ // { x: rect.right - margin, y: rect.top + margin }, // top right
862
+ // { x: rect.left + margin, y: rect.bottom - margin }, // bottom left
863
+ { x: rect.right - margin, y: rect.bottom - margin }, // bottom right
864
+ ];
865
+
866
+ return checkPoints.some(({ x, y }) => {
867
+ try {
868
+ const topEl = document.elementFromPoint(x, y);
869
+ if (!topEl) return false;
870
+
871
+ let current = topEl;
872
+ while (current && current !== document.documentElement) {
873
+ if (current === element) return true;
874
+ current = current.parentElement;
875
+ }
876
+ return false;
877
+ } catch (e) {
878
+ return true;
879
+ }
880
+ });
881
+ }
882
+
883
+ /**
884
+ * Checks if an element is within the expanded viewport.
885
+ *
886
+ * @param {HTMLElement} element - The element to check.
887
+ * @param {number} viewportExpansion - The viewport expansion.
888
+ * @returns {boolean} Whether the element is within the expanded viewport.
889
+ */
890
+ function isInExpandedViewport(element, viewportExpansion) {
891
+ if (viewportExpansion === -1) {
892
+ return true;
893
+ }
894
+
895
+ const rects = element.getClientRects(); // Use getClientRects
896
+
897
+ if (!rects || rects.length === 0) {
898
+ // Fallback to getBoundingClientRect if getClientRects is empty,
899
+ // useful for elements like <svg> that might not have client rects but have a bounding box.
900
+ const boundingRect = getCachedBoundingRect(element);
901
+ if (!boundingRect || boundingRect.width === 0 || boundingRect.height === 0) {
902
+ return false;
903
+ }
904
+ return !(
905
+ boundingRect.bottom < -viewportExpansion ||
906
+ boundingRect.top > window.innerHeight + viewportExpansion ||
907
+ boundingRect.right < -viewportExpansion ||
908
+ boundingRect.left > window.innerWidth + viewportExpansion
909
+ );
910
+ }
911
+
912
+ // Check if *any* client rect is within the viewport
913
+ for (const rect of rects) {
914
+ if (rect.width === 0 || rect.height === 0) continue; // Skip empty rects
915
+
916
+ if (
917
+ !(
918
+ rect.bottom < -viewportExpansion ||
919
+ rect.top > window.innerHeight + viewportExpansion ||
920
+ rect.right < -viewportExpansion ||
921
+ rect.left > window.innerWidth + viewportExpansion
922
+ )
923
+ ) {
924
+ return true; // Found at least one rect in the viewport
925
+ }
926
+ }
927
+
928
+ return false; // No rects were found in the viewport
929
+ }
930
+
931
+ // /**
932
+ // * Gets the effective scroll of an element.
933
+ // *
934
+ // * @param {HTMLElement} element - The element to get the effective scroll for.
935
+ // * @returns {Object} The effective scroll of the element.
936
+ // */
937
+ // function getEffectiveScroll(element) {
938
+ // let currentEl = element;
939
+ // let scrollX = 0;
940
+ // let scrollY = 0;
941
+
942
+ // while (currentEl && currentEl !== document.documentElement) {
943
+ // if (currentEl.scrollLeft || currentEl.scrollTop) {
944
+ // scrollX += currentEl.scrollLeft;
945
+ // scrollY += currentEl.scrollTop;
946
+ // }
947
+ // currentEl = currentEl.parentElement;
948
+ // }
949
+
950
+ // scrollX += window.scrollX;
951
+ // scrollY += window.scrollY;
952
+
953
+ // return { scrollX, scrollY };
954
+ // }
955
+
956
+ /**
957
+ * Checks if an element is an interactive candidate.
958
+ *
959
+ * @param {HTMLElement} element - The element to check.
960
+ * @returns {boolean} Whether the element is an interactive candidate.
961
+ */
962
+ function isInteractiveCandidate(element) {
963
+ if (!element || element.nodeType !== Node.ELEMENT_NODE) return false;
964
+
965
+ const tagName = element.tagName.toLowerCase();
966
+
967
+ // Fast-path for common interactive elements
968
+ const interactiveElements = new Set(['a', 'button', 'input', 'select', 'textarea', 'details', 'summary', 'label']);
969
+
970
+ if (interactiveElements.has(tagName)) return true;
971
+
972
+ // Quick attribute checks without getting full lists
973
+ const hasQuickInteractiveAttr =
974
+ element.hasAttribute('onclick') ||
975
+ element.hasAttribute('role') ||
976
+ element.hasAttribute('tabindex') ||
977
+ element.hasAttribute('aria-') ||
978
+ element.hasAttribute('data-action') ||
979
+ element.getAttribute('contenteditable') === 'true';
980
+
981
+ return hasQuickInteractiveAttr;
982
+ }
983
+
984
+ // --- Define constants for distinct interaction check ---
985
+ const DISTINCT_INTERACTIVE_TAGS = new Set([
986
+ 'a',
987
+ 'button',
988
+ 'input',
989
+ 'select',
990
+ 'textarea',
991
+ 'summary',
992
+ 'details',
993
+ 'label',
994
+ 'option',
995
+ ]);
996
+ const INTERACTIVE_ROLES = new Set([
997
+ 'button',
998
+ 'link',
999
+ 'menuitem',
1000
+ 'menuitemradio',
1001
+ 'menuitemcheckbox',
1002
+ 'radio',
1003
+ 'checkbox',
1004
+ 'tab',
1005
+ 'switch',
1006
+ 'slider',
1007
+ 'spinbutton',
1008
+ 'combobox',
1009
+ 'searchbox',
1010
+ 'textbox',
1011
+ 'listbox',
1012
+ 'option',
1013
+ 'scrollbar',
1014
+ ]);
1015
+
1016
+ /**
1017
+ * Heuristically determines if an element should be considered as independently interactive,
1018
+ * even if it's nested inside another interactive container.
1019
+ *
1020
+ * This function helps detect deeply nested actionable elements (e.g., menu items within a button)
1021
+ * that may not be picked up by strict interactivity checks.
1022
+ *
1023
+ * @param {HTMLElement} element - The element to check.
1024
+ * @returns {boolean} Whether the element is heuristically interactive.
1025
+ */
1026
+ function isHeuristicallyInteractive(element) {
1027
+ if (!element || element.nodeType !== Node.ELEMENT_NODE) return false;
1028
+
1029
+ // Skip non-visible elements early for performance
1030
+ if (!isElementVisible(element)) return false;
1031
+
1032
+ // Check for common attributes that often indicate interactivity
1033
+ const hasInteractiveAttributes =
1034
+ element.hasAttribute('role') ||
1035
+ element.hasAttribute('tabindex') ||
1036
+ element.hasAttribute('onclick') ||
1037
+ typeof element.onclick === 'function';
1038
+
1039
+ // Check for semantic class names suggesting interactivity
1040
+ const hasInteractiveClass = /\b(btn|clickable|menu|item|entry|link)\b/i.test(element.className || '');
1041
+
1042
+ // Determine whether the element is inside a known interactive container
1043
+ const isInKnownContainer = Boolean(element.closest('button,a,[role="button"],.menu,.dropdown,.list,.toolbar'));
1044
+
1045
+ // Ensure the element has at least one visible child (to avoid marking empty wrappers)
1046
+ const hasVisibleChildren = [...element.children].some(isElementVisible);
1047
+
1048
+ // Avoid highlighting elements whose parent is <body> (top-level wrappers)
1049
+ const isParentBody = element.parentElement && element.parentElement.isSameNode(document.body);
1050
+
1051
+ return (
1052
+ (isInteractiveElement(element) || hasInteractiveAttributes || hasInteractiveClass) &&
1053
+ hasVisibleChildren &&
1054
+ isInKnownContainer &&
1055
+ !isParentBody
1056
+ );
1057
+ }
1058
+
1059
+ /**
1060
+ * Checks if an element likely represents a distinct interaction
1061
+ * separate from its parent (if the parent is also interactive).
1062
+ *
1063
+ * @param {HTMLElement} element - The element to check.
1064
+ * @returns {boolean} Whether the element is a distinct interaction.
1065
+ */
1066
+ function isElementDistinctInteraction(element) {
1067
+ if (!element || element.nodeType !== Node.ELEMENT_NODE) {
1068
+ return false;
1069
+ }
1070
+
1071
+ const tagName = element.tagName.toLowerCase();
1072
+ const role = element.getAttribute('role');
1073
+
1074
+ // Check if it's an iframe - always distinct boundary
1075
+ if (tagName === 'iframe') {
1076
+ return true;
1077
+ }
1078
+
1079
+ // Check tag name
1080
+ if (DISTINCT_INTERACTIVE_TAGS.has(tagName)) {
1081
+ return true;
1082
+ }
1083
+ // Check interactive roles
1084
+ if (role && INTERACTIVE_ROLES.has(role)) {
1085
+ return true;
1086
+ }
1087
+ // Check contenteditable
1088
+ if (element.isContentEditable || element.getAttribute('contenteditable') === 'true') {
1089
+ return true;
1090
+ }
1091
+ // Check for common testing/automation attributes
1092
+ if (element.hasAttribute('data-testid') || element.hasAttribute('data-cy') || element.hasAttribute('data-test')) {
1093
+ return true;
1094
+ }
1095
+ // Check for explicit onclick handler (attribute or property)
1096
+ if (element.hasAttribute('onclick') || typeof element.onclick === 'function') {
1097
+ return true;
1098
+ }
1099
+
1100
+ // return false
1101
+
1102
+ // Check for other common interaction event listeners
1103
+ try {
1104
+ const getEventListenersForNode =
1105
+ element?.ownerDocument?.defaultView?.getEventListenersForNode || window.getEventListenersForNode;
1106
+ if (typeof getEventListenersForNode === 'function') {
1107
+ const listeners = getEventListenersForNode(element);
1108
+ const interactionEvents = [
1109
+ 'click',
1110
+ 'mousedown',
1111
+ 'mouseup',
1112
+ 'keydown',
1113
+ 'keyup',
1114
+ 'submit',
1115
+ 'change',
1116
+ 'input',
1117
+ 'focus',
1118
+ 'blur',
1119
+ ];
1120
+ for (const eventType of interactionEvents) {
1121
+ for (const listener of listeners) {
1122
+ if (listener.type === eventType) {
1123
+ return true; // Found a common interaction listener
1124
+ }
1125
+ }
1126
+ }
1127
+ }
1128
+ // Fallback: Check common event attributes if getEventListeners is not available (getEventListenersForNode doesn't work in page.evaluate context)
1129
+ const commonEventAttrs = [
1130
+ 'onmousedown',
1131
+ 'onmouseup',
1132
+ 'onkeydown',
1133
+ 'onkeyup',
1134
+ 'onsubmit',
1135
+ 'onchange',
1136
+ 'oninput',
1137
+ 'onfocus',
1138
+ 'onblur',
1139
+ ];
1140
+ if (commonEventAttrs.some(attr => element.hasAttribute(attr))) {
1141
+ return true;
1142
+ }
1143
+ } catch (e) {
1144
+ // console.warn(`Could not check event listeners for ${element.tagName}:`, e);
1145
+ // If checking listeners fails, rely on other checks
1146
+ }
1147
+
1148
+ // if the element is not strictly interactive but appears clickable based on heuristic signals
1149
+ if (isHeuristicallyInteractive(element)) {
1150
+ return true;
1151
+ }
1152
+
1153
+ // Default to false: if it's interactive but doesn't match above,
1154
+ // assume it triggers the same action as the parent.
1155
+ return false;
1156
+ }
1157
+ // --- End distinct interaction check ---
1158
+
1159
+ /**
1160
+ * Handles the logic for deciding whether to highlight an element and performing the highlight.
1161
+ * @param {
1162
+ {
1163
+ tagName: string;
1164
+ attributes: Record<string, string>;
1165
+ xpath: any;
1166
+ children: never[];
1167
+ isVisible?: boolean;
1168
+ isTopElement?: boolean;
1169
+ isInteractive?: boolean;
1170
+ isInViewport?: boolean;
1171
+ highlightIndex?: number;
1172
+ shadowRoot?: boolean;
1173
+ }} nodeData - The node data object.
1174
+ * @param {HTMLElement} node - The node to highlight.
1175
+ * @param {HTMLElement | null} parentIframe - The parent iframe node.
1176
+ * @param {boolean} isParentHighlighted - Whether the parent node is highlighted.
1177
+ * @returns {boolean} Whether the element was highlighted.
1178
+ */
1179
+ function handleHighlighting(nodeData, node, parentIframe, isParentHighlighted) {
1180
+ if (!nodeData.isInteractive) return false; // Not interactive, definitely don't highlight
1181
+
1182
+ let shouldHighlight = false;
1183
+ if (!isParentHighlighted) {
1184
+ // Parent wasn't highlighted, this interactive node can be highlighted.
1185
+ shouldHighlight = true;
1186
+ } else {
1187
+ // Parent *was* highlighted. Only highlight this node if it represents a distinct interaction.
1188
+ if (isElementDistinctInteraction(node)) {
1189
+ shouldHighlight = true;
1190
+ } else {
1191
+ // console.log(`Skipping highlight for ${nodeData.tagName} (parent highlighted)`);
1192
+ shouldHighlight = false;
1193
+ }
1194
+ }
1195
+
1196
+ if (shouldHighlight) {
1197
+ // Check viewport status before assigning index and highlighting
1198
+ nodeData.isInViewport = isInExpandedViewport(node, viewportExpansion);
1199
+
1200
+ // When viewportExpansion is -1, all interactive elements should get a highlight index
1201
+ // regardless of viewport status
1202
+ if (nodeData.isInViewport || viewportExpansion === -1) {
1203
+ nodeData.highlightIndex = highlightIndex++;
1204
+
1205
+ if (doHighlightElements) {
1206
+ if (focusHighlightIndex >= 0) {
1207
+ if (focusHighlightIndex === nodeData.highlightIndex) {
1208
+ highlightElement(node, nodeData.highlightIndex, parentIframe);
1209
+ }
1210
+ } else {
1211
+ highlightElement(node, nodeData.highlightIndex, parentIframe);
1212
+ }
1213
+ return true; // Successfully highlighted
1214
+ }
1215
+ } else {
1216
+ // console.log(`Skipping highlight for ${nodeData.tagName} (outside viewport)`);
1217
+ }
1218
+ }
1219
+
1220
+ return false; // Did not highlight
1221
+ }
1222
+
1223
+ /**
1224
+ * Creates a node data object for a given node and its descendants.
1225
+ *
1226
+ * @param {HTMLElement} node - The node to process.
1227
+ * @param {HTMLElement | null} parentIframe - The parent iframe node.
1228
+ * @param {boolean} isParentHighlighted - Whether the parent node is highlighted.
1229
+ * @returns {string | null} The ID of the node data object, or null if the node is not processed.
1230
+ */
1231
+ const MAX_DEPTH = 100;
1232
+ let visitedNodes;
1233
+
1234
+ function buildDomTree(node, parentIframe = null, isParentHighlighted = false, depth = 0) {
1235
+ // Initialize visited nodes tracking on first call
1236
+ if (!visitedNodes) {
1237
+ visitedNodes = new WeakSet();
1238
+ }
1239
+
1240
+ // Prevent infinite recursion
1241
+ if (depth > MAX_DEPTH) {
1242
+ return null;
1243
+ }
1244
+
1245
+ // Fast rejection checks first
1246
+ if (
1247
+ !node ||
1248
+ node.id === HIGHLIGHT_CONTAINER_ID ||
1249
+ (node.nodeType !== Node.ELEMENT_NODE && node.nodeType !== Node.TEXT_NODE)
1250
+ ) {
1251
+ return null;
1252
+ }
1253
+
1254
+ // Prevent circular references (only for valid nodes)
1255
+ if (node.nodeType === Node.ELEMENT_NODE && visitedNodes.has(node)) {
1256
+ return null;
1257
+ }
1258
+ if (node.nodeType === Node.ELEMENT_NODE) {
1259
+ visitedNodes.add(node);
1260
+ }
1261
+
1262
+ // Special handling for root node (body)
1263
+ if (node === document.body) {
1264
+ const nodeData = {
1265
+ tagName: 'body',
1266
+ attributes: {},
1267
+ xpath: '/body',
1268
+ children: [],
1269
+ };
1270
+
1271
+ // Process children of body
1272
+ for (const child of Array.from(node.childNodes)) {
1273
+ const domElement = buildDomTree(child, parentIframe, false, depth + 1); // Body's children have no highlighted parent initially
1274
+ if (domElement) nodeData.children.push(domElement);
1275
+ }
1276
+
1277
+ const id = `${ID.current++}`;
1278
+ DOM_HASH_MAP[id] = nodeData;
1279
+ return id;
1280
+ }
1281
+
1282
+ // Early bailout for non-element nodes except text
1283
+ if (node.nodeType !== Node.ELEMENT_NODE && node.nodeType !== Node.TEXT_NODE) {
1284
+ return null;
1285
+ }
1286
+
1287
+ // Process text nodes
1288
+ if (node.nodeType === Node.TEXT_NODE) {
1289
+ const textContent = node.textContent?.trim();
1290
+ if (!textContent) {
1291
+ return null;
1292
+ }
1293
+
1294
+ // Only check visibility for text nodes that might be visible
1295
+ const parentElement = node.parentElement;
1296
+ if (!parentElement || parentElement.tagName.toLowerCase() === 'script') {
1297
+ return null;
1298
+ }
1299
+
1300
+ const id = `${ID.current++}`;
1301
+ DOM_HASH_MAP[id] = {
1302
+ type: 'TEXT_NODE',
1303
+ text: textContent,
1304
+ isVisible: isTextNodeVisible(node),
1305
+ };
1306
+ return id;
1307
+ }
1308
+
1309
+ // Quick checks for element nodes
1310
+ if (node.nodeType === Node.ELEMENT_NODE && !isElementAccepted(node)) {
1311
+ return null;
1312
+ }
1313
+
1314
+ // Early viewport check - only filter out elements clearly outside viewport
1315
+ // The getBoundingClientRect() of the Shadow DOM host element may return width/height = 0
1316
+ if (viewportExpansion !== -1 && !node.shadowRoot) {
1317
+ const rect = getCachedBoundingRect(node); // Keep for initial quick check
1318
+ const style = getCachedComputedStyle(node);
1319
+
1320
+ // Skip viewport check for fixed/sticky elements as they may appear anywhere
1321
+ const isFixedOrSticky = style && (style.position === 'fixed' || style.position === 'sticky');
1322
+
1323
+ // Check if element has actual dimensions using offsetWidth/Height (quick check)
1324
+ const hasSize = node.offsetWidth > 0 || node.offsetHeight > 0;
1325
+
1326
+ // Use getBoundingClientRect for the quick OUTSIDE check.
1327
+ // isInExpandedViewport will do the more accurate check later if needed.
1328
+ if (
1329
+ !rect ||
1330
+ (!isFixedOrSticky &&
1331
+ !hasSize &&
1332
+ (rect.bottom < -viewportExpansion ||
1333
+ rect.top > window.innerHeight + viewportExpansion ||
1334
+ rect.right < -viewportExpansion ||
1335
+ rect.left > window.innerWidth + viewportExpansion))
1336
+ ) {
1337
+ // console.log("Skipping node outside viewport (quick check):", node.tagName, rect);
1338
+ return null;
1339
+ }
1340
+ }
1341
+
1342
+ /**
1343
+ * @type {
1344
+ {
1345
+ tagName: string;
1346
+ attributes: Record<string, string | null>;
1347
+ xpath: any;
1348
+ children: never[];
1349
+ isVisible?: boolean;
1350
+ isTopElement?: boolean;
1351
+ isInteractive?: boolean;
1352
+ isInViewport?: boolean;
1353
+ highlightIndex?: number;
1354
+ shadowRoot?: boolean;
1355
+ }
1356
+ } nodeData - The node data object.
1357
+ */
1358
+ const nodeData = {
1359
+ tagName: node.tagName.toLowerCase(),
1360
+ attributes: {},
1361
+ xpath: getXPathTree(node, true),
1362
+ children: [],
1363
+ };
1364
+
1365
+ // Get attributes for interactive elements or potential text containers
1366
+ if (
1367
+ isInteractiveCandidate(node) ||
1368
+ node.tagName.toLowerCase() === 'iframe' ||
1369
+ node.tagName.toLowerCase() === 'body'
1370
+ ) {
1371
+ const attributeNames = node.getAttributeNames?.() || [];
1372
+ for (const name of attributeNames) {
1373
+ const value = node.getAttribute(name);
1374
+ nodeData.attributes[name] = value;
1375
+ }
1376
+ }
1377
+
1378
+ let nodeWasHighlighted = false;
1379
+ // Perform visibility, interactivity, and highlighting checks
1380
+ if (node.nodeType === Node.ELEMENT_NODE) {
1381
+ nodeData.isVisible = isElementVisible(node); // isElementVisible uses offsetWidth/Height, which is fine
1382
+ if (nodeData.isVisible) {
1383
+ nodeData.isTopElement = isTopElement(node);
1384
+
1385
+ // Special handling for ARIA menu containers - check interactivity even if not top element
1386
+ const role = node.getAttribute('role');
1387
+ const isMenuContainer = role === 'menu' || role === 'menubar' || role === 'listbox';
1388
+
1389
+ if (nodeData.isTopElement || isMenuContainer) {
1390
+ nodeData.isInteractive = isInteractiveElement(node);
1391
+ // Call the dedicated highlighting function
1392
+ nodeWasHighlighted = handleHighlighting(nodeData, node, parentIframe, isParentHighlighted);
1393
+ }
1394
+ }
1395
+ }
1396
+
1397
+ // Process children, with special handling for iframes and rich text editors
1398
+ if (node.tagName) {
1399
+ const tagName = node.tagName.toLowerCase();
1400
+
1401
+ // Handle iframes
1402
+ if (tagName === 'iframe') {
1403
+ const rect = getCachedBoundingRect(node);
1404
+ nodeData.attributes['computedHeight'] = String(Math.ceil(rect.height));
1405
+ nodeData.attributes['computedWidth'] = String(Math.ceil(rect.width));
1406
+
1407
+ // Check if iframe should be skipped (invisible tracking/ad iframes)
1408
+ const shouldSkipIframe =
1409
+ // Invisible iframes (1x1 pixel or similar)
1410
+ (rect.width <= 1 && rect.height <= 1) ||
1411
+ // Positioned off-screen
1412
+ rect.left < -1000 ||
1413
+ rect.top < -1000;
1414
+
1415
+ // Early detection for sandboxed iframes
1416
+ const sandbox = node.getAttribute('sandbox');
1417
+ const isSandboxed = sandbox !== null;
1418
+ const isRestrictiveSandbox = isSandboxed && !sandbox.includes('allow-same-origin');
1419
+
1420
+ if (shouldSkipIframe) {
1421
+ // Skip processing invisible/tracking iframes entirely
1422
+ nodeData.attributes['skipped'] = 'invisible-tracking-iframe';
1423
+ } else if (isRestrictiveSandbox) {
1424
+ // Set error directly for sandboxed iframes we know will fail
1425
+ nodeData.attributes['error'] = 'Cross-origin iframe access blocked by sandbox';
1426
+ } else {
1427
+ // Only attempt access for iframes that might succeed
1428
+ try {
1429
+ const iframeDoc = node.contentDocument || node.contentWindow?.document;
1430
+ if (iframeDoc && iframeDoc.childNodes) {
1431
+ for (const child of Array.from(iframeDoc.childNodes)) {
1432
+ const domElement = buildDomTree(child, node, false, depth + 1);
1433
+ if (domElement) nodeData.children.push(domElement);
1434
+ }
1435
+ }
1436
+ } catch (e) {
1437
+ nodeData.attributes['error'] = e.message;
1438
+ // Only log unexpected errors, not predictable ones
1439
+ if (!e.message.includes('cross-origin') && !e.message.includes('origin "null"')) {
1440
+ console.warn('Unable to access iframe:', e);
1441
+ }
1442
+ }
1443
+ }
1444
+ }
1445
+ // Handle rich text editors and contenteditable elements
1446
+ else if (
1447
+ node.isContentEditable ||
1448
+ node.getAttribute('contenteditable') === 'true' ||
1449
+ node.id === 'tinymce' ||
1450
+ node.classList.contains('mce-content-body') ||
1451
+ (tagName === 'body' && node.getAttribute('data-id')?.startsWith('mce_'))
1452
+ ) {
1453
+ // Process all child nodes to capture formatted text
1454
+ for (const child of Array.from(node.childNodes)) {
1455
+ const domElement = buildDomTree(child, parentIframe, nodeWasHighlighted, depth + 1);
1456
+ if (domElement) nodeData.children.push(domElement);
1457
+ }
1458
+ } else {
1459
+ // Handle shadow DOM
1460
+ if (node.shadowRoot) {
1461
+ nodeData.shadowRoot = true;
1462
+ for (const child of Array.from(node.shadowRoot.childNodes)) {
1463
+ const domElement = buildDomTree(child, parentIframe, nodeWasHighlighted, depth + 1);
1464
+ if (domElement) nodeData.children.push(domElement);
1465
+ }
1466
+ }
1467
+ // Handle regular elements
1468
+ for (const child of Array.from(node.childNodes)) {
1469
+ // Pass the highlighted status of the *current* node to its children
1470
+ const passHighlightStatusToChild = nodeWasHighlighted || isParentHighlighted;
1471
+ const domElement = buildDomTree(child, parentIframe, passHighlightStatusToChild, depth + 1);
1472
+ if (domElement) nodeData.children.push(domElement);
1473
+ }
1474
+ }
1475
+ }
1476
+
1477
+ // Skip empty anchor tags only if they have no dimensions and no children
1478
+ if (nodeData.tagName === 'a' && nodeData.children.length === 0 && !nodeData.attributes.href) {
1479
+ // Check if the anchor has actual dimensions
1480
+ const rect = getCachedBoundingRect(node);
1481
+ const hasSize = (rect && rect.width > 0 && rect.height > 0) || node.offsetWidth > 0 || node.offsetHeight > 0;
1482
+
1483
+ if (!hasSize) {
1484
+ return null;
1485
+ }
1486
+ }
1487
+
1488
+ const id = `${ID.current++}`;
1489
+ DOM_HASH_MAP[id] = nodeData;
1490
+ return id;
1491
+ }
1492
+
1493
+ // Reset visited nodes for new DOM tree build
1494
+ visitedNodes = null;
1495
+ const rootId = buildDomTree(document.body);
1496
+
1497
+ // Clear the cache before starting
1498
+ DOM_CACHE.clearCache();
1499
+
1500
+ return { rootId, map: DOM_HASH_MAP };
1501
+ };