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