@midscene/shared 0.16.9-beta-20250508125518.0 → 0.16.9
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/dist/es/common.js +2 -0
- package/dist/es/env.d.ts +3 -1
- package/dist/es/env.js +3 -0
- package/dist/es/extractor-debug.js +16 -21
- package/dist/es/extractor.d.ts +47 -2
- package/dist/es/extractor.js +168 -104
- package/dist/es/fs.d.ts +1 -2
- package/dist/es/fs.js +4 -9
- package/dist/es/img.d.ts +1 -1
- package/dist/es/index-305e7a7e.d.ts +32 -0
- package/dist/es/logger.js +2 -0
- package/dist/lib/common.js +2 -0
- package/dist/lib/env.d.ts +3 -1
- package/dist/lib/env.js +4 -0
- package/dist/lib/extractor-debug.js +16 -21
- package/dist/lib/extractor.d.ts +47 -2
- package/dist/lib/extractor.js +169 -107
- package/dist/lib/fs.d.ts +1 -2
- package/dist/lib/fs.js +4 -10
- package/dist/lib/img.d.ts +1 -1
- package/dist/lib/index-305e7a7e.d.ts +32 -0
- package/dist/lib/logger.js +2 -0
- package/dist/script/htmlElement.js +166 -101
- package/dist/script/htmlElementDebug.js +16 -21
- package/dist/types/env.d.ts +3 -1
- package/dist/types/extractor.d.ts +47 -2
- package/dist/types/fs.d.ts +1 -2
- package/dist/types/img.d.ts +1 -1
- package/dist/types/index-305e7a7e.d.ts +32 -0
- package/package.json +2 -6
- package/src/env.ts +3 -0
- package/src/extractor/client-extractor.ts +211 -0
- package/src/extractor/index.ts +3 -5
- package/src/extractor/util.ts +2 -31
- package/src/extractor/web-extractor.ts +19 -2
- package/src/node/fs.ts +4 -12
- package/src/types/index.ts +0 -5
- package/dist/es/index-df4e781b.d.ts +0 -77
- package/dist/es/types.d.ts +0 -2
- package/dist/es/types.js +0 -6
- package/dist/lib/index-df4e781b.d.ts +0 -77
- package/dist/lib/types.d.ts +0 -2
- package/dist/lib/types.js +0 -31
- package/dist/types/index-df4e781b.d.ts +0 -77
- package/dist/types/types.d.ts +0 -2
- package/src/extractor/locator.ts +0 -117
package/src/extractor/locator.ts
DELETED
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
import type { ElementInfo } from '.';
|
|
2
|
-
import { isTextElement } from './dom-util';
|
|
3
|
-
import { getNodeFromCacheList } from './util';
|
|
4
|
-
import { collectElementInfo } from './web-extractor';
|
|
5
|
-
|
|
6
|
-
const getElementIndex = (element: Element): number => {
|
|
7
|
-
let index = 1;
|
|
8
|
-
let prev = element.previousElementSibling;
|
|
9
|
-
|
|
10
|
-
while (prev) {
|
|
11
|
-
if (prev.nodeName.toLowerCase() === element.nodeName.toLowerCase()) {
|
|
12
|
-
index++;
|
|
13
|
-
}
|
|
14
|
-
prev = prev.previousElementSibling;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
return index;
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
// Find the first ancestor with an ID
|
|
21
|
-
const findFirstAncestorWithId = (element: Element): Element | null => {
|
|
22
|
-
let current = element;
|
|
23
|
-
|
|
24
|
-
while (current?.parentElement) {
|
|
25
|
-
if (current.id) {
|
|
26
|
-
return current;
|
|
27
|
-
}
|
|
28
|
-
current = current.parentElement;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
return null;
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
const getElementXPath = (element: Node): string => {
|
|
35
|
-
if (element.nodeType !== Node.ELEMENT_NODE) return '';
|
|
36
|
-
|
|
37
|
-
const el = element as Element;
|
|
38
|
-
|
|
39
|
-
// handle html and body tags
|
|
40
|
-
if (el === document.documentElement) {
|
|
41
|
-
return '/html';
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
if (el === document.body) {
|
|
45
|
-
return '/html/body';
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
// If this element has an ID, return an XPath with the ID
|
|
49
|
-
if (el.id) {
|
|
50
|
-
return `//*[@id="${el.id}"]`;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
const ancestorWithId = findFirstAncestorWithId(el);
|
|
54
|
-
|
|
55
|
-
if (ancestorWithId) {
|
|
56
|
-
// Start from the ancestor with ID
|
|
57
|
-
const ancestorPath = `//*[@id="${ancestorWithId.id}"]`;
|
|
58
|
-
|
|
59
|
-
// Build the path from the ancestor to this element
|
|
60
|
-
let current: Element | null = el;
|
|
61
|
-
const pathParts: string[] = [];
|
|
62
|
-
|
|
63
|
-
while (current && current !== ancestorWithId) {
|
|
64
|
-
const index = getElementIndex(current);
|
|
65
|
-
const tagName = current.nodeName.toLowerCase();
|
|
66
|
-
pathParts.unshift(`${tagName}[${index}]`);
|
|
67
|
-
current = current.parentElement;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
// Combine the ancestor path with the rest of the path
|
|
71
|
-
return pathParts.length > 0
|
|
72
|
-
? `${ancestorPath}/${pathParts.join('/')}`
|
|
73
|
-
: ancestorPath;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// If no parent node, or we need a full path and haven't returned yet,
|
|
77
|
-
// start building the full path
|
|
78
|
-
if (!el.parentNode) {
|
|
79
|
-
return `/${el.nodeName.toLowerCase()}`;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
const index = getElementIndex(el);
|
|
83
|
-
const tagName = el.nodeName.toLowerCase();
|
|
84
|
-
|
|
85
|
-
if (el.parentNode) {
|
|
86
|
-
const parentXPath = getElementXPath(el.parentNode);
|
|
87
|
-
return `${parentXPath}/${tagName}[${index}]`;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
return `/${tagName}[${index}]`;
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
function generateXPaths(node: Node | null): string[] {
|
|
94
|
-
if (!node) return [];
|
|
95
|
-
|
|
96
|
-
const xPath = getElementXPath(node);
|
|
97
|
-
|
|
98
|
-
return [xPath];
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
export function getXpathsById(id: string): string[] | null {
|
|
102
|
-
let node = getNodeFromCacheList(id);
|
|
103
|
-
|
|
104
|
-
if (!node) {
|
|
105
|
-
return null;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
if (isTextElement(node)) {
|
|
109
|
-
node = node.parentElement;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
return generateXPaths(node);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
export function getElementInfoByNode(node: Node): ElementInfo | null {
|
|
116
|
-
return collectElementInfo(node, window, document);
|
|
117
|
-
}
|