@eko-ai/eko 1.3.5 → 2.0.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 (161) hide show
  1. package/README.md +37 -19
  2. package/dist/agent/a2a.d.ts +7 -0
  3. package/dist/agent/a2a.d.ts.map +1 -0
  4. package/dist/agent/base.d.ts +47 -0
  5. package/dist/agent/base.d.ts.map +1 -0
  6. package/dist/agent/browser/browser_base.d.ts +30 -0
  7. package/dist/agent/browser/browser_base.d.ts.map +1 -0
  8. package/dist/agent/browser/browser_labels.d.ts +21 -0
  9. package/dist/agent/browser/browser_labels.d.ts.map +1 -0
  10. package/dist/agent/browser/browser_screen.d.ts +16 -0
  11. package/dist/agent/browser/browser_screen.d.ts.map +1 -0
  12. package/dist/agent/browser/build_dom_tree.d.ts +2 -0
  13. package/dist/agent/browser/build_dom_tree.d.ts.map +1 -0
  14. package/dist/agent/browser/index.d.ts +6 -0
  15. package/dist/agent/browser/index.d.ts.map +1 -0
  16. package/dist/agent/chat.d.ts +7 -0
  17. package/dist/agent/chat.d.ts.map +1 -0
  18. package/dist/agent/computer.d.ts +23 -0
  19. package/dist/agent/computer.d.ts.map +1 -0
  20. package/dist/agent/file.d.ts +14 -0
  21. package/dist/agent/file.d.ts.map +1 -0
  22. package/dist/agent/index.d.ts +9 -0
  23. package/dist/agent/index.d.ts.map +1 -0
  24. package/dist/agent/shell.d.ts +14 -0
  25. package/dist/agent/shell.d.ts.map +1 -0
  26. package/dist/agent/timer.d.ts +5 -0
  27. package/dist/agent/timer.d.ts.map +1 -0
  28. package/dist/common/log.d.ts +43 -2
  29. package/dist/common/log.d.ts.map +1 -0
  30. package/dist/common/utils.d.ts +12 -0
  31. package/dist/common/utils.d.ts.map +1 -0
  32. package/dist/common/xml.d.ts +7 -0
  33. package/dist/common/xml.d.ts.map +1 -0
  34. package/dist/config/index.d.ts +8 -0
  35. package/dist/config/index.d.ts.map +1 -0
  36. package/dist/core/chain.d.ts +45 -0
  37. package/dist/core/chain.d.ts.map +1 -0
  38. package/dist/core/context.d.ts +23 -0
  39. package/dist/core/context.d.ts.map +1 -0
  40. package/dist/core/index.d.ts +18 -0
  41. package/dist/core/index.d.ts.map +1 -0
  42. package/dist/core/plan.d.ts +11 -0
  43. package/dist/core/plan.d.ts.map +1 -0
  44. package/dist/index.cjs.js +18163 -10856
  45. package/dist/index.d.ts +13 -10
  46. package/dist/index.d.ts.map +1 -0
  47. package/dist/index.esm.js +18147 -10888
  48. package/dist/llm/index.d.ts +15 -0
  49. package/dist/llm/index.d.ts.map +1 -0
  50. package/dist/mcp/client.d.ts +28 -0
  51. package/dist/mcp/client.d.ts.map +1 -0
  52. package/dist/mcp/index.d.ts +28 -0
  53. package/dist/mcp/index.d.ts.map +1 -0
  54. package/dist/prompt/agent.d.ts +6 -0
  55. package/dist/prompt/agent.d.ts.map +1 -0
  56. package/dist/prompt/plan.d.ts +4 -0
  57. package/dist/prompt/plan.d.ts.map +1 -0
  58. package/dist/tools/foreach_task.d.ts +12 -0
  59. package/dist/tools/foreach_task.d.ts.map +1 -0
  60. package/dist/tools/human_interact.d.ts +12 -0
  61. package/dist/tools/human_interact.d.ts.map +1 -0
  62. package/dist/tools/index.d.ts +19 -0
  63. package/dist/tools/index.d.ts.map +1 -0
  64. package/dist/tools/task_node_status.d.ts +12 -0
  65. package/dist/tools/task_node_status.d.ts.map +1 -0
  66. package/dist/tools/variable_storage.d.ts +12 -0
  67. package/dist/tools/variable_storage.d.ts.map +1 -0
  68. package/dist/tools/watch_trigger.d.ts +12 -0
  69. package/dist/tools/watch_trigger.d.ts.map +1 -0
  70. package/dist/tools/wrapper.d.ts +12 -0
  71. package/dist/tools/wrapper.d.ts.map +1 -0
  72. package/dist/types/core.types.d.ts +114 -0
  73. package/dist/types/core.types.d.ts.map +1 -0
  74. package/dist/types/index.d.ts +3 -3
  75. package/dist/types/index.d.ts.map +1 -0
  76. package/dist/types/llm.types.d.ts +80 -51
  77. package/dist/types/llm.types.d.ts.map +1 -0
  78. package/dist/types/mcp.types.d.ts +35 -0
  79. package/dist/types/mcp.types.d.ts.map +1 -0
  80. package/dist/types/tools.types.d.ts +56 -152
  81. package/dist/types/tools.types.d.ts.map +1 -0
  82. package/package.json +18 -58
  83. package/dist/common/chrome/proxy.d.ts +0 -24
  84. package/dist/common/context-compressor.d.ts +0 -10
  85. package/dist/common/summarize-workflow.d.ts +0 -2
  86. package/dist/common/tools/cancel_workflow.d.ts +0 -9
  87. package/dist/common/tools/document_agent.d.ts +0 -8
  88. package/dist/common/tools/human.d.ts +0 -30
  89. package/dist/common/tools/index.d.ts +0 -4
  90. package/dist/common/tools/write_context.d.ts +0 -7
  91. package/dist/core/eko.d.ts +0 -30
  92. package/dist/core/tool-registry.d.ts +0 -13
  93. package/dist/extension/content/index.d.ts +0 -1
  94. package/dist/extension/core.d.ts +0 -11
  95. package/dist/extension/index.d.ts +0 -7
  96. package/dist/extension/script/bing.js +0 -25
  97. package/dist/extension/script/build_dom_tree.d.ts +0 -38
  98. package/dist/extension/script/build_dom_tree.js +0 -662
  99. package/dist/extension/script/common.js +0 -212
  100. package/dist/extension/script/duckduckgo.js +0 -25
  101. package/dist/extension/script/google.js +0 -26
  102. package/dist/extension/tools/browser.d.ts +0 -23
  103. package/dist/extension/tools/browser_action.d.ts +0 -20
  104. package/dist/extension/tools/export_file.d.ts +0 -18
  105. package/dist/extension/tools/extract_content.d.ts +0 -18
  106. package/dist/extension/tools/get_all_tabs.d.ts +0 -9
  107. package/dist/extension/tools/html_script.d.ts +0 -10
  108. package/dist/extension/tools/index.d.ts +0 -12
  109. package/dist/extension/tools/open_url.d.ts +0 -19
  110. package/dist/extension/tools/request_login.d.ts +0 -10
  111. package/dist/extension/tools/screenshot.d.ts +0 -18
  112. package/dist/extension/tools/switch_tab.d.ts +0 -8
  113. package/dist/extension/tools/tab_management.d.ts +0 -13
  114. package/dist/extension/tools/tool_returns_screenshot.d.ts +0 -8
  115. package/dist/extension/tools/web_search.d.ts +0 -18
  116. package/dist/extension/utils.d.ts +0 -31
  117. package/dist/extension.cjs.js +0 -2796
  118. package/dist/extension.esm.js +0 -2789
  119. package/dist/extension_content_script.js +0 -1078
  120. package/dist/fellou/computer.d.ts +0 -20
  121. package/dist/fellou/index.d.ts +0 -6
  122. package/dist/fellou/tools/computer_use.d.ts +0 -18
  123. package/dist/fellou.cjs.js +0 -238
  124. package/dist/fellou.esm.js +0 -235
  125. package/dist/models/action.d.ts +0 -33
  126. package/dist/models/workflow.d.ts +0 -25
  127. package/dist/nodejs/core.d.ts +0 -2
  128. package/dist/nodejs/index.d.ts +0 -3
  129. package/dist/nodejs/script/build_dom_tree.d.ts +0 -1
  130. package/dist/nodejs/tools/browser_use.d.ts +0 -28
  131. package/dist/nodejs/tools/command_execute.d.ts +0 -12
  132. package/dist/nodejs/tools/file_read.d.ts +0 -11
  133. package/dist/nodejs/tools/file_write.d.ts +0 -15
  134. package/dist/nodejs/tools/index.d.ts +0 -5
  135. package/dist/nodejs.cjs.js +0 -73507
  136. package/dist/nodejs.esm.js +0 -73504
  137. package/dist/schemas/workflow.schema.d.ts +0 -44
  138. package/dist/services/llm/claude-provider.d.ts +0 -12
  139. package/dist/services/llm/openai-provider.d.ts +0 -12
  140. package/dist/services/llm/provider-factory.d.ts +0 -4
  141. package/dist/services/parser/workflow-parser.d.ts +0 -23
  142. package/dist/services/workflow/generator.d.ts +0 -16
  143. package/dist/services/workflow/templates.d.ts +0 -8
  144. package/dist/types/action.types.d.ts +0 -53
  145. package/dist/types/eko.types.d.ts +0 -37
  146. package/dist/types/parser.types.d.ts +0 -9
  147. package/dist/types/workflow.types.d.ts +0 -57
  148. package/dist/utils/execution-logger.d.ts +0 -69
  149. package/dist/utils/sleep.d.ts +0 -1
  150. package/dist/web/core.d.ts +0 -2
  151. package/dist/web/index.d.ts +0 -5
  152. package/dist/web/script/build_dom_tree.d.ts +0 -10
  153. package/dist/web/tools/browser.d.ts +0 -21
  154. package/dist/web/tools/browser_use.d.ts +0 -19
  155. package/dist/web/tools/export_file.d.ts +0 -18
  156. package/dist/web/tools/extract_content.d.ts +0 -17
  157. package/dist/web/tools/html_script.d.ts +0 -10
  158. package/dist/web/tools/index.d.ts +0 -6
  159. package/dist/web/tools/screenshot.d.ts +0 -18
  160. package/dist/web.cjs.js +0 -9952
  161. package/dist/web.esm.js +0 -9948
@@ -1,662 +0,0 @@
1
- /**
2
- * Get clickable elements on the page
3
- *
4
- * @param {*} doHighlightElements Is highlighted
5
- * @param {*} includeAttributes [attr_names...]
6
- * @returns { element_str, selector_map }
7
- */
8
- function get_clickable_elements(doHighlightElements = true, includeAttributes) {
9
- window.clickable_elements = {};
10
- let page_tree = build_dom_tree(doHighlightElements);
11
- let element_tree = parse_node(page_tree);
12
- let selector_map = create_selector_map(element_tree, true);
13
- let element_str = clickable_elements_to_string(element_tree, includeAttributes);
14
- return { element_str, selector_map };
15
- }
16
-
17
- function get_highlight_element(highlightIndex) {
18
- return window.clickable_elements[highlightIndex];
19
- }
20
-
21
- function remove_highlight() {
22
- let highlight = document.getElementById('playwright-highlight-container');
23
- if (highlight) {
24
- highlight.remove();
25
- }
26
- }
27
-
28
- function clickable_elements_to_string(element_tree, includeAttributes) {
29
- if (!includeAttributes) {
30
- includeAttributes = [
31
- 'id',
32
- 'title',
33
- 'type',
34
- 'name',
35
- 'role',
36
- 'class',
37
- // 'href',
38
- 'tabindex',
39
- 'aria-label',
40
- 'placeholder',
41
- 'value',
42
- 'alt',
43
- 'aria-expanded',
44
- ];
45
- }
46
-
47
- function get_all_text_till_next_clickable_element(element_node) {
48
- let text_parts = [];
49
- function collect_text(node) {
50
- if (node.tagName && node != element_node && node.highlightIndex != null) {
51
- return;
52
- }
53
- if (!node.tagName && node.text) {
54
- text_parts.push(node.text);
55
- } else if (node.tagName) {
56
- for (let i = 0; i < node.children.length; i++) {
57
- collect_text(node.children[i]);
58
- }
59
- }
60
- }
61
- collect_text(element_node);
62
- return text_parts.join('\n').trim().replace(/\n+/g, ' ');
63
- }
64
-
65
- function has_parent_with_highlight_index(node) {
66
- let current = node.parent;
67
- while (current) {
68
- if (current.highlightIndex != null) {
69
- return true;
70
- }
71
- current = current.parent;
72
- }
73
- return false;
74
- }
75
-
76
- let formatted_text = [];
77
- function process_node(node, depth) {
78
- if (node.text == null) {
79
- if (node.highlightIndex != null) {
80
- let attributes_str = '';
81
- if (includeAttributes) {
82
- for (let i = 0; i < includeAttributes.length; i++) {
83
- let key = includeAttributes[i];
84
- let value = node.attributes[key];
85
- if (key && value) {
86
- attributes_str += ` ${key}="${value}"`;
87
- }
88
- }
89
- attributes_str = attributes_str.replace(/\n+/g, ' ');
90
- }
91
- let text = get_all_text_till_next_clickable_element(node);
92
- formatted_text.push(
93
- `[${node.highlightIndex}]:<${node.tagName}${attributes_str}>${text}</${node.tagName}>`
94
- );
95
- }
96
- for (let i = 0; i < node.children.length; i++) {
97
- let child = node.children[i];
98
- process_node(child, depth + 1);
99
- }
100
- } else if (!has_parent_with_highlight_index(node)) {
101
- formatted_text.push(`[]:${node.text}`);
102
- }
103
- }
104
- process_node(element_tree, 0);
105
- return formatted_text.join('\n');
106
- }
107
-
108
- function create_selector_map(element_tree, ignore_element_obj) {
109
- let selector_map = {};
110
- function process_node(node) {
111
- if (node.tagName) {
112
- if (node.highlightIndex != null) {
113
- selector_map[node.highlightIndex] = ignore_element_obj ? { xpath: node.xpath } : node;
114
- }
115
- for (let i = 0; i < node.children.length; i++) {
116
- process_node(node.children[i]);
117
- }
118
- }
119
- }
120
- process_node(element_tree);
121
- return selector_map;
122
- }
123
-
124
- function parse_node(node_data, parent) {
125
- if (!node_data) {
126
- return;
127
- }
128
- if (node_data.type == 'TEXT_NODE') {
129
- return {
130
- text: node_data.text || '',
131
- isVisible: node_data.isVisible || false,
132
- parent: parent,
133
- };
134
- }
135
- let element_node = {
136
- tagName: node_data.tagName,
137
- xpath: node_data.xpath,
138
- highlightIndex: node_data.highlightIndex,
139
- attributes: node_data.attributes || {},
140
- isVisible: node_data.isVisible || false,
141
- isInteractive: node_data.isInteractive || false,
142
- isTopElement: node_data.isTopElement || false,
143
- shadowRoot: node_data.shadowRoot || false,
144
- children: [],
145
- parent: parent,
146
- };
147
- if (node_data.children) {
148
- let children = [];
149
- for (let i = 0; i < node_data.children.length; i++) {
150
- let child = node_data.children[i];
151
- if (child) {
152
- let child_node = parse_node(child, element_node);
153
- if (child_node) {
154
- children.push(child_node);
155
- }
156
- }
157
- }
158
- element_node.children = children;
159
- }
160
- return element_node;
161
- }
162
-
163
- function build_dom_tree(doHighlightElements) {
164
- let highlightIndex = 0; // Reset highlight index
165
-
166
- function highlightElement(element, index, parentIframe = null) {
167
- // Create or get highlight container
168
- let container = document.getElementById('playwright-highlight-container');
169
- if (!container) {
170
- container = document.createElement('div');
171
- container.id = 'playwright-highlight-container';
172
- container.style.position = 'fixed';
173
- container.style.pointerEvents = 'none';
174
- container.style.top = '0';
175
- container.style.left = '0';
176
- container.style.width = '100%';
177
- container.style.height = '100%';
178
- container.style.zIndex = '2147483647'; // Maximum z-index value
179
- document.documentElement.appendChild(container);
180
- }
181
-
182
- // Generate a color based on the index
183
- const colors = [
184
- '#FF0000',
185
- '#00FF00',
186
- '#0000FF',
187
- '#FFA500',
188
- '#800080',
189
- '#008080',
190
- '#FF69B4',
191
- '#4B0082',
192
- '#FF4500',
193
- '#2E8B57',
194
- '#DC143C',
195
- '#4682B4',
196
- ];
197
- const colorIndex = index % colors.length;
198
- const baseColor = colors[colorIndex];
199
- const backgroundColor = `${baseColor}1A`; // 10% opacity version of the color
200
-
201
- // Create highlight overlay
202
- const overlay = document.createElement('div');
203
- overlay.style.position = 'absolute';
204
- overlay.style.border = `2px solid ${baseColor}`;
205
- overlay.style.pointerEvents = 'none';
206
- overlay.style.boxSizing = 'border-box';
207
-
208
- // Position overlay based on element
209
- const rect = element.getBoundingClientRect();
210
- let top = rect.top;
211
- let left = rect.left;
212
-
213
- if (rect.width < window.innerWidth / 2 || rect.height < window.innerHeight / 2) {
214
- overlay.style.backgroundColor = backgroundColor;
215
- }
216
-
217
- // Adjust position if element is inside an iframe
218
- if (parentIframe) {
219
- const iframeRect = parentIframe.getBoundingClientRect();
220
- top += iframeRect.top;
221
- left += iframeRect.left;
222
- }
223
-
224
- overlay.style.top = `${top}px`;
225
- overlay.style.left = `${left}px`;
226
- overlay.style.width = `${rect.width}px`;
227
- overlay.style.height = `${rect.height}px`;
228
-
229
- // Create label
230
- const label = document.createElement('div');
231
- label.className = 'playwright-highlight-label';
232
- label.style.position = 'absolute';
233
- label.style.background = baseColor;
234
- label.style.color = 'white';
235
- label.style.padding = '1px 4px';
236
- label.style.borderRadius = '4px';
237
- label.style.fontSize = `${Math.min(12, Math.max(8, rect.height / 2))}px`; // Responsive font size
238
- label.textContent = index;
239
-
240
- // Calculate label position
241
- const labelWidth = 20; // Approximate width
242
- const labelHeight = 16; // Approximate height
243
-
244
- // Default position (top-right corner inside the box)
245
- let labelTop = top + 2;
246
- let labelLeft = left + rect.width - labelWidth - 2;
247
-
248
- // Adjust if box is too small
249
- if (rect.width < labelWidth + 4 || rect.height < labelHeight + 4) {
250
- // Position outside the box if it's too small
251
- labelTop = top - labelHeight - 2;
252
- labelLeft = left + rect.width - labelWidth;
253
- }
254
-
255
- // Ensure label stays within viewport
256
- if (labelTop < 0) labelTop = top + 2;
257
- if (labelLeft < 0) labelLeft = left + 2;
258
- if (labelLeft + labelWidth > window.innerWidth) {
259
- labelLeft = left + rect.width - labelWidth - 2;
260
- }
261
-
262
- label.style.top = `${labelTop}px`;
263
- label.style.left = `${labelLeft}px`;
264
-
265
- // Add to container
266
- container.appendChild(overlay);
267
- container.appendChild(label);
268
-
269
- // Store reference for cleanup
270
- element.setAttribute('browser-user-highlight-id', `playwright-highlight-${index}`);
271
-
272
- return index + 1;
273
- }
274
-
275
- // Helper function to generate XPath as a tree
276
- function getXPathTree(element, stopAtBoundary = true) {
277
- const segments = [];
278
- let currentElement = element;
279
-
280
- while (currentElement && currentElement.nodeType === Node.ELEMENT_NODE) {
281
- // Stop if we hit a shadow root or iframe
282
- if (
283
- stopAtBoundary &&
284
- (currentElement.parentNode instanceof ShadowRoot ||
285
- currentElement.parentNode instanceof HTMLIFrameElement)
286
- ) {
287
- break;
288
- }
289
-
290
- let index = 0;
291
- let sibling = currentElement.previousSibling;
292
- while (sibling) {
293
- if (
294
- sibling.nodeType === Node.ELEMENT_NODE &&
295
- sibling.nodeName === currentElement.nodeName
296
- ) {
297
- index++;
298
- }
299
- sibling = sibling.previousSibling;
300
- }
301
-
302
- const tagName = currentElement.nodeName.toLowerCase();
303
- const xpathIndex = index > 0 ? `[${index + 1}]` : '';
304
- segments.unshift(`${tagName}${xpathIndex}`);
305
-
306
- currentElement = currentElement.parentNode;
307
- }
308
-
309
- return segments.join('/');
310
- }
311
-
312
- // Helper function to check if element is accepted
313
- function isElementAccepted(element) {
314
- const leafElementDenyList = new Set(['svg', 'script', 'style', 'link', 'meta']);
315
- return !leafElementDenyList.has(element.tagName.toLowerCase());
316
- }
317
-
318
- // Helper function to check if element is interactive
319
- function isInteractiveElement(element) {
320
- // Base interactive elements and roles
321
- const interactiveElements = new Set([
322
- 'a',
323
- 'button',
324
- 'details',
325
- 'embed',
326
- 'input',
327
- 'label',
328
- 'menu',
329
- 'menuitem',
330
- 'object',
331
- 'select',
332
- 'textarea',
333
- 'summary',
334
- ]);
335
-
336
- const interactiveRoles = new Set([
337
- 'button',
338
- 'menu',
339
- 'menuitem',
340
- 'link',
341
- 'checkbox',
342
- 'radio',
343
- 'slider',
344
- 'tab',
345
- 'tabpanel',
346
- 'textbox',
347
- 'combobox',
348
- 'grid',
349
- 'listbox',
350
- 'option',
351
- 'progressbar',
352
- 'scrollbar',
353
- 'searchbox',
354
- 'switch',
355
- 'tree',
356
- 'treeitem',
357
- 'spinbutton',
358
- 'tooltip',
359
- 'a-button-inner',
360
- 'a-dropdown-button',
361
- 'click',
362
- 'menuitemcheckbox',
363
- 'menuitemradio',
364
- 'a-button-text',
365
- 'button-text',
366
- 'button-icon',
367
- 'button-icon-only',
368
- 'button-text-icon-only',
369
- 'dropdown',
370
- 'combobox',
371
- ]);
372
-
373
- const tagName = element.tagName.toLowerCase();
374
- const role = element.getAttribute('role');
375
- const ariaRole = element.getAttribute('aria-role');
376
- const tabIndex = element.getAttribute('tabindex');
377
-
378
- // Basic role/attribute checks
379
- const hasInteractiveRole =
380
- interactiveElements.has(tagName) ||
381
- interactiveRoles.has(role) ||
382
- interactiveRoles.has(ariaRole) ||
383
- (tabIndex !== null && tabIndex !== '-1') ||
384
- element.getAttribute('data-action') === 'a-dropdown-select' ||
385
- element.getAttribute('data-action') === 'a-dropdown-button';
386
-
387
- if (hasInteractiveRole) return true;
388
-
389
- // Get computed style
390
- const style = window.getComputedStyle(element);
391
-
392
- // Check if element has click-like styling
393
- // const hasClickStyling = style.cursor === 'pointer' ||
394
- // element.style.cursor === 'pointer' ||
395
- // style.pointerEvents !== 'none';
396
-
397
- // Check for event listeners
398
- const hasClickHandler =
399
- element.onclick !== null ||
400
- element.getAttribute('onclick') !== null ||
401
- element.hasAttribute('ng-click') ||
402
- element.hasAttribute('@click') ||
403
- element.hasAttribute('v-on:click');
404
-
405
- // Helper function to safely get event listeners
406
- function getEventListeners(el) {
407
- try {
408
- // Try to get listeners using Chrome DevTools API
409
- return window.getEventListeners?.(el) || {};
410
- } catch (e) {
411
- // Fallback: check for common event properties
412
- const listeners = {};
413
-
414
- // List of common event types to check
415
- const eventTypes = [
416
- 'click',
417
- 'mousedown',
418
- 'mouseup',
419
- 'touchstart',
420
- 'touchend',
421
- 'keydown',
422
- 'keyup',
423
- 'focus',
424
- 'blur',
425
- ];
426
-
427
- for (const type of eventTypes) {
428
- const handler = el[`on${type}`];
429
- if (handler) {
430
- listeners[type] = [
431
- {
432
- listener: handler,
433
- useCapture: false,
434
- },
435
- ];
436
- }
437
- }
438
-
439
- return listeners;
440
- }
441
- }
442
-
443
- // Check for click-related events on the element itself
444
- const listeners = getEventListeners(element);
445
- const hasClickListeners =
446
- listeners &&
447
- (listeners.click?.length > 0 ||
448
- listeners.mousedown?.length > 0 ||
449
- listeners.mouseup?.length > 0 ||
450
- listeners.touchstart?.length > 0 ||
451
- listeners.touchend?.length > 0);
452
-
453
- // Check for ARIA properties that suggest interactivity
454
- const hasAriaProps =
455
- element.hasAttribute('aria-expanded') ||
456
- element.hasAttribute('aria-pressed') ||
457
- element.hasAttribute('aria-selected') ||
458
- element.hasAttribute('aria-checked');
459
-
460
- // Check for form-related functionality
461
- const isFormRelated =
462
- element.form !== undefined ||
463
- element.hasAttribute('contenteditable') ||
464
- style.userSelect !== 'none';
465
-
466
- // Check if element is draggable
467
- const isDraggable = element.draggable || element.getAttribute('draggable') === 'true';
468
-
469
- return (
470
- hasAriaProps ||
471
- // hasClickStyling ||
472
- hasClickHandler ||
473
- hasClickListeners ||
474
- // isFormRelated ||
475
- isDraggable
476
- );
477
- }
478
-
479
- // Helper function to check if element is visible
480
- function isElementVisible(element) {
481
- const style = window.getComputedStyle(element);
482
- return (
483
- element.offsetWidth > 0 &&
484
- element.offsetHeight > 0 &&
485
- style.visibility !== 'hidden' &&
486
- style.display !== 'none'
487
- );
488
- }
489
-
490
- // Helper function to check if element is the top element at its position
491
- function isTopElement(element) {
492
- // Find the correct document context and root element
493
- let doc = element.ownerDocument;
494
-
495
- // If we're in an iframe, elements are considered top by default
496
- if (doc !== window.document) {
497
- return true;
498
- }
499
-
500
- // For shadow DOM, we need to check within its own root context
501
- const shadowRoot = element.getRootNode();
502
- if (shadowRoot instanceof ShadowRoot) {
503
- const rect = element.getBoundingClientRect();
504
- const point = { x: rect.left + rect.width / 2, y: rect.top + rect.height / 2 };
505
-
506
- try {
507
- // Use shadow root's elementFromPoint to check within shadow DOM context
508
- const topEl = shadowRoot.elementFromPoint(point.x, point.y);
509
- if (!topEl) return false;
510
-
511
- // Check if the element or any of its parents match our target element
512
- let current = topEl;
513
- while (current && current !== shadowRoot) {
514
- if (current === element) return true;
515
- current = current.parentElement;
516
- }
517
- return false;
518
- } catch (e) {
519
- return true; // If we can't determine, consider it visible
520
- }
521
- }
522
-
523
- // Regular DOM elements
524
- const rect = element.getBoundingClientRect();
525
- const point = { x: rect.left + rect.width / 2, y: rect.top + rect.height / 2 };
526
-
527
- try {
528
- const topEl = document.elementFromPoint(point.x, point.y);
529
- if (!topEl) return false;
530
-
531
- let current = topEl;
532
- while (current && current !== document.documentElement) {
533
- if (current === element) return true;
534
- current = current.parentElement;
535
- }
536
- return false;
537
- } catch (e) {
538
- return true;
539
- }
540
- }
541
-
542
- // Helper function to check if text node is visible
543
- function isTextNodeVisible(textNode) {
544
- const range = document.createRange();
545
- range.selectNodeContents(textNode);
546
- const rect = range.getBoundingClientRect();
547
-
548
- return (
549
- rect.width !== 0 &&
550
- rect.height !== 0 &&
551
- rect.top >= 0 &&
552
- rect.top <= window.innerHeight &&
553
- textNode.parentElement?.checkVisibility({
554
- checkOpacity: true,
555
- checkVisibilityCSS: true,
556
- })
557
- );
558
- }
559
-
560
- // Function to traverse the DOM and create nested JSON
561
- function buildDomTree(node, parentIframe = null) {
562
- if (!node) return null;
563
-
564
- // Special case for text nodes
565
- if (node.nodeType === Node.TEXT_NODE) {
566
- const textContent = node.textContent.trim();
567
- if (textContent && isTextNodeVisible(node)) {
568
- return {
569
- type: 'TEXT_NODE',
570
- text: textContent,
571
- isVisible: true,
572
- };
573
- }
574
- return null;
575
- }
576
-
577
- // Check if element is accepted
578
- if (node.nodeType === Node.ELEMENT_NODE && !isElementAccepted(node)) {
579
- return null;
580
- }
581
-
582
- const nodeData = {
583
- tagName: node.tagName ? node.tagName.toLowerCase() : null,
584
- attributes: {},
585
- xpath: node.nodeType === Node.ELEMENT_NODE ? getXPathTree(node, true) : null,
586
- children: [],
587
- };
588
-
589
- // Copy all attributes if the node is an element
590
- if (node.nodeType === Node.ELEMENT_NODE && node.attributes) {
591
- // Use getAttributeNames() instead of directly iterating attributes
592
- const attributeNames = node.getAttributeNames?.() || [];
593
- for (const name of attributeNames) {
594
- nodeData.attributes[name] = node.getAttribute(name);
595
- }
596
- }
597
-
598
- if (node.nodeType === Node.ELEMENT_NODE) {
599
- const isInteractive = isInteractiveElement(node);
600
- const isVisible = isElementVisible(node);
601
- const isTop = isTopElement(node);
602
-
603
- nodeData.isInteractive = isInteractive;
604
- nodeData.isVisible = isVisible;
605
- nodeData.isTopElement = isTop;
606
-
607
- // Highlight if element meets all criteria and highlighting is enabled
608
- if (isInteractive && isVisible && isTop) {
609
- nodeData.highlightIndex = highlightIndex++;
610
- window.clickable_elements[nodeData.highlightIndex] = node;
611
- if (doHighlightElements) {
612
- highlightElement(node, nodeData.highlightIndex, parentIframe);
613
- }
614
- }
615
- }
616
-
617
- // Only add iframeContext if we're inside an iframe
618
- // if (parentIframe) {
619
- // nodeData.iframeContext = `iframe[src="${parentIframe.src || ''}"]`;
620
- // }
621
-
622
- // Only add shadowRoot field if it exists
623
- if (node.shadowRoot) {
624
- nodeData.shadowRoot = true;
625
- }
626
-
627
- // Handle shadow DOM
628
- if (node.shadowRoot) {
629
- const shadowChildren = Array.from(node.shadowRoot.childNodes).map((child) =>
630
- buildDomTree(child, parentIframe)
631
- );
632
- nodeData.children.push(...shadowChildren);
633
- }
634
-
635
- // Handle iframes
636
- if (node.tagName === 'IFRAME') {
637
- try {
638
- const iframeDoc = node.contentDocument || node.contentWindow.document;
639
- if (iframeDoc) {
640
- const iframeChildren = Array.from(iframeDoc.body.childNodes).map((child) =>
641
- buildDomTree(child, node)
642
- );
643
- nodeData.children.push(...iframeChildren);
644
- }
645
- } catch (e) {
646
- console.warn('Unable to access iframe:', node);
647
- }
648
- } else {
649
- const children = Array.from(node.childNodes).map((child) =>
650
- buildDomTree(child, parentIframe)
651
- );
652
- nodeData.children.push(...children);
653
- }
654
-
655
- return nodeData;
656
- }
657
- return buildDomTree(document.body);
658
- }
659
-
660
- window.get_clickable_elements = get_clickable_elements;
661
- window.get_highlight_element = get_highlight_element;
662
- window.remove_highlight = remove_highlight;