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