@godscene/shared 1.7.11
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/README.md +9 -0
- package/dist/es/baseDB.mjs +109 -0
- package/dist/es/cli/cli-args.mjs +95 -0
- package/dist/es/cli/cli-error.mjs +24 -0
- package/dist/es/cli/cli-runner.mjs +122 -0
- package/dist/es/cli/index.mjs +4 -0
- package/dist/es/common.mjs +37 -0
- package/dist/es/constants/example-code.mjs +227 -0
- package/dist/es/constants/index.mjs +124 -0
- package/dist/es/env/basic.mjs +6 -0
- package/dist/es/env/constants.mjs +110 -0
- package/dist/es/env/global-config-manager.mjs +94 -0
- package/dist/es/env/helper.mjs +43 -0
- package/dist/es/env/index.mjs +5 -0
- package/dist/es/env/init-debug.mjs +18 -0
- package/dist/es/env/model-config-manager.mjs +79 -0
- package/dist/es/env/parse-model-config.mjs +165 -0
- package/dist/es/env/types.mjs +232 -0
- package/dist/es/env/utils.mjs +18 -0
- package/dist/es/extractor/constants.mjs +2 -0
- package/dist/es/extractor/cs_postmessage.mjs +61 -0
- package/dist/es/extractor/customLocator.mjs +641 -0
- package/dist/es/extractor/debug.mjs +6 -0
- package/dist/es/extractor/dom-util.mjs +96 -0
- package/dist/es/extractor/index.mjs +5 -0
- package/dist/es/extractor/locator.mjs +250 -0
- package/dist/es/extractor/tree.mjs +78 -0
- package/dist/es/extractor/util.mjs +245 -0
- package/dist/es/extractor/web-extractor.mjs +393 -0
- package/dist/es/img/box-select.mjs +824 -0
- package/dist/es/img/canvas-fallback.mjs +238 -0
- package/dist/es/img/get-photon.mjs +45 -0
- package/dist/es/img/get-sharp.mjs +11 -0
- package/dist/es/img/index.mjs +4 -0
- package/dist/es/img/info.mjs +35 -0
- package/dist/es/img/transform.mjs +275 -0
- package/dist/es/index.mjs +2 -0
- package/dist/es/key-alias-utils.mjs +19 -0
- package/dist/es/logger.mjs +64 -0
- package/dist/es/mcp/base-server.mjs +282 -0
- package/dist/es/mcp/base-tools.mjs +159 -0
- package/dist/es/mcp/chrome-path.mjs +35 -0
- package/dist/es/mcp/cli-report-session.mjs +78 -0
- package/dist/es/mcp/error-formatter.mjs +19 -0
- package/dist/es/mcp/index.mjs +9 -0
- package/dist/es/mcp/init-arg-utils.mjs +38 -0
- package/dist/es/mcp/inject-report-html-plugin.mjs +53 -0
- package/dist/es/mcp/launcher-helper.mjs +52 -0
- package/dist/es/mcp/tool-generator.mjs +419 -0
- package/dist/es/mcp/types.mjs +3 -0
- package/dist/es/node/fs.mjs +44 -0
- package/dist/es/node/index.mjs +2 -0
- package/dist/es/node/port.mjs +24 -0
- package/dist/es/polyfills/async-hooks.mjs +2 -0
- package/dist/es/polyfills/index.mjs +1 -0
- package/dist/es/types/index.mjs +3 -0
- package/dist/es/us-keyboard-layout.mjs +1414 -0
- package/dist/es/us-keyboard-layout.mjs.LICENSE.txt +5 -0
- package/dist/es/utils.mjs +72 -0
- package/dist/es/zod-schema-utils.mjs +54 -0
- package/dist/lib/baseDB.js +149 -0
- package/dist/lib/cli/cli-args.js +138 -0
- package/dist/lib/cli/cli-error.js +61 -0
- package/dist/lib/cli/cli-runner.js +181 -0
- package/dist/lib/cli/index.js +53 -0
- package/dist/lib/common.js +93 -0
- package/dist/lib/constants/example-code.js +264 -0
- package/dist/lib/constants/index.js +221 -0
- package/dist/lib/env/basic.js +40 -0
- package/dist/lib/env/constants.js +153 -0
- package/dist/lib/env/global-config-manager.js +128 -0
- package/dist/lib/env/helper.js +80 -0
- package/dist/lib/env/index.js +90 -0
- package/dist/lib/env/init-debug.js +52 -0
- package/dist/lib/env/model-config-manager.js +113 -0
- package/dist/lib/env/parse-model-config.js +211 -0
- package/dist/lib/env/types.js +572 -0
- package/dist/lib/env/utils.js +61 -0
- package/dist/lib/extractor/constants.js +42 -0
- package/dist/lib/extractor/cs_postmessage.js +98 -0
- package/dist/lib/extractor/customLocator.js +693 -0
- package/dist/lib/extractor/debug.js +12 -0
- package/dist/lib/extractor/dom-util.js +157 -0
- package/dist/lib/extractor/index.js +87 -0
- package/dist/lib/extractor/locator.js +296 -0
- package/dist/lib/extractor/tree.js +124 -0
- package/dist/lib/extractor/util.js +336 -0
- package/dist/lib/extractor/web-extractor.js +442 -0
- package/dist/lib/img/box-select.js +875 -0
- package/dist/lib/img/canvas-fallback.js +305 -0
- package/dist/lib/img/get-photon.js +82 -0
- package/dist/lib/img/get-sharp.js +45 -0
- package/dist/lib/img/index.js +95 -0
- package/dist/lib/img/info.js +92 -0
- package/dist/lib/img/transform.js +364 -0
- package/dist/lib/index.js +36 -0
- package/dist/lib/key-alias-utils.js +62 -0
- package/dist/lib/logger.js +114 -0
- package/dist/lib/mcp/base-server.js +332 -0
- package/dist/lib/mcp/base-tools.js +193 -0
- package/dist/lib/mcp/chrome-path.js +72 -0
- package/dist/lib/mcp/cli-report-session.js +121 -0
- package/dist/lib/mcp/error-formatter.js +53 -0
- package/dist/lib/mcp/index.js +114 -0
- package/dist/lib/mcp/init-arg-utils.js +78 -0
- package/dist/lib/mcp/inject-report-html-plugin.js +98 -0
- package/dist/lib/mcp/launcher-helper.js +86 -0
- package/dist/lib/mcp/tool-generator.js +456 -0
- package/dist/lib/mcp/types.js +40 -0
- package/dist/lib/node/fs.js +97 -0
- package/dist/lib/node/index.js +65 -0
- package/dist/lib/node/port.js +61 -0
- package/dist/lib/polyfills/async-hooks.js +36 -0
- package/dist/lib/polyfills/index.js +58 -0
- package/dist/lib/types/index.js +37 -0
- package/dist/lib/us-keyboard-layout.js +1457 -0
- package/dist/lib/us-keyboard-layout.js.LICENSE.txt +5 -0
- package/dist/lib/utils.js +148 -0
- package/dist/lib/zod-schema-utils.js +97 -0
- package/dist/types/baseDB.d.ts +25 -0
- package/dist/types/cli/cli-args.d.ts +8 -0
- package/dist/types/cli/cli-error.d.ts +5 -0
- package/dist/types/cli/cli-runner.d.ts +19 -0
- package/dist/types/cli/index.d.ts +4 -0
- package/dist/types/common.d.ts +12 -0
- package/dist/types/constants/example-code.d.ts +2 -0
- package/dist/types/constants/index.d.ts +61 -0
- package/dist/types/env/basic.d.ts +6 -0
- package/dist/types/env/constants.d.ts +50 -0
- package/dist/types/env/global-config-manager.d.ts +32 -0
- package/dist/types/env/helper.d.ts +4 -0
- package/dist/types/env/index.d.ts +4 -0
- package/dist/types/env/init-debug.d.ts +1 -0
- package/dist/types/env/model-config-manager.d.ts +25 -0
- package/dist/types/env/parse-model-config.d.ts +31 -0
- package/dist/types/env/types.d.ts +339 -0
- package/dist/types/env/utils.d.ts +7 -0
- package/dist/types/extractor/constants.d.ts +1 -0
- package/dist/types/extractor/cs_postmessage.d.ts +2 -0
- package/dist/types/extractor/customLocator.d.ts +69 -0
- package/dist/types/extractor/debug.d.ts +1 -0
- package/dist/types/extractor/dom-util.d.ts +57 -0
- package/dist/types/extractor/index.d.ts +33 -0
- package/dist/types/extractor/locator.d.ts +9 -0
- package/dist/types/extractor/tree.d.ts +6 -0
- package/dist/types/extractor/util.d.ts +47 -0
- package/dist/types/extractor/web-extractor.d.ts +24 -0
- package/dist/types/img/box-select.d.ts +26 -0
- package/dist/types/img/canvas-fallback.d.ts +105 -0
- package/dist/types/img/get-photon.d.ts +19 -0
- package/dist/types/img/get-sharp.d.ts +3 -0
- package/dist/types/img/index.d.ts +3 -0
- package/dist/types/img/info.d.ts +34 -0
- package/dist/types/img/transform.d.ts +98 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/key-alias-utils.d.ts +9 -0
- package/dist/types/logger.d.ts +5 -0
- package/dist/types/mcp/base-server.d.ts +93 -0
- package/dist/types/mcp/base-tools.d.ts +148 -0
- package/dist/types/mcp/chrome-path.d.ts +2 -0
- package/dist/types/mcp/cli-report-session.d.ts +12 -0
- package/dist/types/mcp/error-formatter.d.ts +12 -0
- package/dist/types/mcp/index.d.ts +9 -0
- package/dist/types/mcp/init-arg-utils.d.ts +13 -0
- package/dist/types/mcp/inject-report-html-plugin.d.ts +18 -0
- package/dist/types/mcp/launcher-helper.d.ts +94 -0
- package/dist/types/mcp/tool-generator.d.ts +10 -0
- package/dist/types/mcp/types.d.ts +113 -0
- package/dist/types/node/fs.d.ts +15 -0
- package/dist/types/node/index.d.ts +2 -0
- package/dist/types/node/port.d.ts +8 -0
- package/dist/types/polyfills/async-hooks.d.ts +6 -0
- package/dist/types/polyfills/index.d.ts +4 -0
- package/dist/types/types/index.d.ts +36 -0
- package/dist/types/us-keyboard-layout.d.ts +32 -0
- package/dist/types/utils.d.ts +34 -0
- package/dist/types/zod-schema-utils.d.ts +23 -0
- package/package.json +125 -0
- package/src/baseDB.ts +158 -0
- package/src/cli/cli-args.ts +173 -0
- package/src/cli/cli-error.ts +24 -0
- package/src/cli/cli-runner.ts +230 -0
- package/src/cli/index.ts +4 -0
- package/src/common.ts +67 -0
- package/src/constants/example-code.ts +227 -0
- package/src/constants/index.ts +139 -0
- package/src/env/basic.ts +12 -0
- package/src/env/constants.ts +303 -0
- package/src/env/global-config-manager.ts +191 -0
- package/src/env/helper.ts +58 -0
- package/src/env/index.ts +4 -0
- package/src/env/init-debug.ts +34 -0
- package/src/env/model-config-manager.ts +149 -0
- package/src/env/parse-model-config.ts +357 -0
- package/src/env/types.ts +583 -0
- package/src/env/utils.ts +39 -0
- package/src/extractor/constants.ts +5 -0
- package/src/extractor/cs_postmessage.ts +136 -0
- package/src/extractor/customLocator.ts +1245 -0
- package/src/extractor/debug.ts +10 -0
- package/src/extractor/dom-util.ts +231 -0
- package/src/extractor/index.ts +50 -0
- package/src/extractor/locator.ts +469 -0
- package/src/extractor/tree.ts +179 -0
- package/src/extractor/util.ts +482 -0
- package/src/extractor/web-extractor.ts +617 -0
- package/src/img/box-select.ts +588 -0
- package/src/img/canvas-fallback.ts +393 -0
- package/src/img/get-photon.ts +108 -0
- package/src/img/get-sharp.ts +18 -0
- package/src/img/index.ts +27 -0
- package/src/img/info.ts +102 -0
- package/src/img/transform.ts +553 -0
- package/src/index.ts +1 -0
- package/src/key-alias-utils.ts +23 -0
- package/src/logger.ts +96 -0
- package/src/mcp/base-server.ts +500 -0
- package/src/mcp/base-tools.ts +391 -0
- package/src/mcp/chrome-path.ts +48 -0
- package/src/mcp/cli-report-session.ts +130 -0
- package/src/mcp/error-formatter.ts +52 -0
- package/src/mcp/index.ts +9 -0
- package/src/mcp/init-arg-utils.ts +105 -0
- package/src/mcp/inject-report-html-plugin.ts +119 -0
- package/src/mcp/launcher-helper.ts +200 -0
- package/src/mcp/tool-generator.ts +658 -0
- package/src/mcp/types.ts +131 -0
- package/src/node/fs.ts +84 -0
- package/src/node/index.ts +2 -0
- package/src/node/port.ts +37 -0
- package/src/polyfills/async-hooks.ts +6 -0
- package/src/polyfills/index.ts +4 -0
- package/src/types/index.ts +54 -0
- package/src/us-keyboard-layout.ts +723 -0
- package/src/utils.ts +149 -0
- package/src/zod-schema-utils.ts +133 -0
|
@@ -0,0 +1,393 @@
|
|
|
1
|
+
import { CONTAINER_MINI_HEIGHT, CONTAINER_MINI_WIDTH, NodeType } from "../constants/index.mjs";
|
|
2
|
+
import { isAElement, isButtonElement, isContainerElement, isFormElement, isImgElement, isTextElement } from "./dom-util.mjs";
|
|
3
|
+
import { descriptionOfTree } from "./tree.mjs";
|
|
4
|
+
import { elementRect, getNodeAttributes, getPseudoElementContent, getRect, getTopDocument, logger, midsceneGenerateHash, setDebugMode } from "./util.mjs";
|
|
5
|
+
import { onWindowMessage, postWindowMessage } from "./cs_postmessage.mjs";
|
|
6
|
+
import { getContainerPath, getLocators } from "./customLocator.mjs";
|
|
7
|
+
let indexId = 0;
|
|
8
|
+
function tagNameOfNode(node) {
|
|
9
|
+
let tagName = '';
|
|
10
|
+
if (node instanceof HTMLElement) tagName = node.tagName?.toLowerCase();
|
|
11
|
+
else {
|
|
12
|
+
const parentElement = node.parentElement;
|
|
13
|
+
if (parentElement && parentElement instanceof HTMLElement) tagName = parentElement.tagName?.toLowerCase();
|
|
14
|
+
}
|
|
15
|
+
return tagName ? `<${tagName}>` : '';
|
|
16
|
+
}
|
|
17
|
+
function collectElementInfo(node, currentWindow, currentDocument, baseZoom = 1, basePoint = {
|
|
18
|
+
left: 0,
|
|
19
|
+
top: 0
|
|
20
|
+
}, isContainer = false) {
|
|
21
|
+
const rect = elementRect(node, currentWindow, currentDocument, baseZoom);
|
|
22
|
+
if (!rect) return null;
|
|
23
|
+
if (rect.width < CONTAINER_MINI_WIDTH || rect.height < CONTAINER_MINI_HEIGHT) return null;
|
|
24
|
+
if (0 !== basePoint.left || 0 !== basePoint.top) {
|
|
25
|
+
rect.left += basePoint.left;
|
|
26
|
+
rect.top += basePoint.top;
|
|
27
|
+
}
|
|
28
|
+
if (rect.height >= window.innerHeight && rect.width >= window.innerWidth) return null;
|
|
29
|
+
if (isFormElement(node)) {
|
|
30
|
+
const attributes = getNodeAttributes(node, currentWindow);
|
|
31
|
+
let valueContent = attributes.value || attributes.placeholder || node.textContent || '';
|
|
32
|
+
const nodeHashId = midsceneGenerateHash(node, valueContent, rect);
|
|
33
|
+
const tagName = node.tagName.toLowerCase();
|
|
34
|
+
if ('select' === node.tagName.toLowerCase()) {
|
|
35
|
+
const selectedOption = node.options[node.selectedIndex];
|
|
36
|
+
valueContent = selectedOption?.textContent || '';
|
|
37
|
+
}
|
|
38
|
+
if (('input' === node.tagName.toLowerCase() || 'textarea' === node.tagName.toLowerCase()) && node.value) valueContent = node.value;
|
|
39
|
+
const elementInfo = {
|
|
40
|
+
id: nodeHashId,
|
|
41
|
+
nodeHashId,
|
|
42
|
+
nodeType: NodeType.FORM_ITEM,
|
|
43
|
+
indexId: indexId++,
|
|
44
|
+
attributes: {
|
|
45
|
+
...attributes,
|
|
46
|
+
htmlTagName: `<${tagName}>`,
|
|
47
|
+
nodeType: NodeType.FORM_ITEM
|
|
48
|
+
},
|
|
49
|
+
content: valueContent.trim(),
|
|
50
|
+
rect,
|
|
51
|
+
center: [
|
|
52
|
+
Math.round(rect.left + rect.width / 2),
|
|
53
|
+
Math.round(rect.top + rect.height / 2)
|
|
54
|
+
],
|
|
55
|
+
zoom: rect.zoom,
|
|
56
|
+
isVisible: rect.isVisible
|
|
57
|
+
};
|
|
58
|
+
return elementInfo;
|
|
59
|
+
}
|
|
60
|
+
if (isButtonElement(node)) {
|
|
61
|
+
const rect = mergeElementAndChildrenRects(node, currentWindow, currentDocument, baseZoom);
|
|
62
|
+
if (!rect) return null;
|
|
63
|
+
const attributes = getNodeAttributes(node, currentWindow);
|
|
64
|
+
const pseudo = getPseudoElementContent(node, currentWindow);
|
|
65
|
+
const content = node.innerText || pseudo.before || pseudo.after || '';
|
|
66
|
+
const nodeHashId = midsceneGenerateHash(node, content, rect);
|
|
67
|
+
const elementInfo = {
|
|
68
|
+
id: nodeHashId,
|
|
69
|
+
indexId: indexId++,
|
|
70
|
+
nodeHashId,
|
|
71
|
+
nodeType: NodeType.BUTTON,
|
|
72
|
+
attributes: {
|
|
73
|
+
...attributes,
|
|
74
|
+
htmlTagName: tagNameOfNode(node),
|
|
75
|
+
nodeType: NodeType.BUTTON
|
|
76
|
+
},
|
|
77
|
+
content,
|
|
78
|
+
rect,
|
|
79
|
+
center: [
|
|
80
|
+
Math.round(rect.left + rect.width / 2),
|
|
81
|
+
Math.round(rect.top + rect.height / 2)
|
|
82
|
+
],
|
|
83
|
+
zoom: rect.zoom,
|
|
84
|
+
isVisible: rect.isVisible
|
|
85
|
+
};
|
|
86
|
+
return elementInfo;
|
|
87
|
+
}
|
|
88
|
+
if (isImgElement(node)) {
|
|
89
|
+
const attributes = getNodeAttributes(node, currentWindow);
|
|
90
|
+
const nodeHashId = midsceneGenerateHash(node, '', rect);
|
|
91
|
+
const elementInfo = {
|
|
92
|
+
id: nodeHashId,
|
|
93
|
+
indexId: indexId++,
|
|
94
|
+
nodeHashId,
|
|
95
|
+
attributes: {
|
|
96
|
+
...attributes,
|
|
97
|
+
...node.nodeName?.toLowerCase() === 'svg' ? {
|
|
98
|
+
svgContent: 'true'
|
|
99
|
+
} : {},
|
|
100
|
+
nodeType: NodeType.IMG,
|
|
101
|
+
htmlTagName: tagNameOfNode(node)
|
|
102
|
+
},
|
|
103
|
+
nodeType: NodeType.IMG,
|
|
104
|
+
content: '',
|
|
105
|
+
rect,
|
|
106
|
+
center: [
|
|
107
|
+
Math.round(rect.left + rect.width / 2),
|
|
108
|
+
Math.round(rect.top + rect.height / 2)
|
|
109
|
+
],
|
|
110
|
+
zoom: rect.zoom,
|
|
111
|
+
isVisible: rect.isVisible
|
|
112
|
+
};
|
|
113
|
+
return elementInfo;
|
|
114
|
+
}
|
|
115
|
+
if (isTextElement(node)) {
|
|
116
|
+
const text = node.textContent?.trim().replace(/\n+/g, ' ');
|
|
117
|
+
if (!text) return null;
|
|
118
|
+
const attributes = getNodeAttributes(node, currentWindow);
|
|
119
|
+
const attributeKeys = Object.keys(attributes);
|
|
120
|
+
if (!text.trim() && 0 === attributeKeys.length) return null;
|
|
121
|
+
const nodeHashId = midsceneGenerateHash(node, text, rect);
|
|
122
|
+
const elementInfo = {
|
|
123
|
+
id: nodeHashId,
|
|
124
|
+
indexId: indexId++,
|
|
125
|
+
nodeHashId,
|
|
126
|
+
nodeType: NodeType.TEXT,
|
|
127
|
+
attributes: {
|
|
128
|
+
...attributes,
|
|
129
|
+
nodeType: NodeType.TEXT,
|
|
130
|
+
htmlTagName: tagNameOfNode(node)
|
|
131
|
+
},
|
|
132
|
+
center: [
|
|
133
|
+
Math.round(rect.left + rect.width / 2),
|
|
134
|
+
Math.round(rect.top + rect.height / 2)
|
|
135
|
+
],
|
|
136
|
+
content: text,
|
|
137
|
+
rect,
|
|
138
|
+
zoom: rect.zoom,
|
|
139
|
+
isVisible: rect.isVisible
|
|
140
|
+
};
|
|
141
|
+
return elementInfo;
|
|
142
|
+
}
|
|
143
|
+
if (isAElement(node)) {
|
|
144
|
+
const attributes = getNodeAttributes(node, currentWindow);
|
|
145
|
+
const pseudo = getPseudoElementContent(node, currentWindow);
|
|
146
|
+
const content = node.innerText || pseudo.before || pseudo.after || '';
|
|
147
|
+
const nodeHashId = midsceneGenerateHash(node, content, rect);
|
|
148
|
+
const elementInfo = {
|
|
149
|
+
id: nodeHashId,
|
|
150
|
+
indexId: indexId++,
|
|
151
|
+
nodeHashId,
|
|
152
|
+
nodeType: NodeType.A,
|
|
153
|
+
attributes: {
|
|
154
|
+
...attributes,
|
|
155
|
+
htmlTagName: tagNameOfNode(node),
|
|
156
|
+
nodeType: NodeType.A
|
|
157
|
+
},
|
|
158
|
+
content,
|
|
159
|
+
rect,
|
|
160
|
+
center: [
|
|
161
|
+
Math.round(rect.left + rect.width / 2),
|
|
162
|
+
Math.round(rect.top + rect.height / 2)
|
|
163
|
+
],
|
|
164
|
+
zoom: rect.zoom,
|
|
165
|
+
isVisible: rect.isVisible
|
|
166
|
+
};
|
|
167
|
+
return elementInfo;
|
|
168
|
+
}
|
|
169
|
+
if (isContainerElement(node) || isContainer) {
|
|
170
|
+
const attributes = getNodeAttributes(node, currentWindow);
|
|
171
|
+
const nodeHashId = midsceneGenerateHash(node, '', rect);
|
|
172
|
+
const elementInfo = {
|
|
173
|
+
id: nodeHashId,
|
|
174
|
+
nodeHashId,
|
|
175
|
+
indexId: indexId++,
|
|
176
|
+
nodeType: NodeType.CONTAINER,
|
|
177
|
+
attributes: {
|
|
178
|
+
...attributes,
|
|
179
|
+
nodeType: NodeType.CONTAINER,
|
|
180
|
+
htmlTagName: tagNameOfNode(node)
|
|
181
|
+
},
|
|
182
|
+
content: '',
|
|
183
|
+
rect,
|
|
184
|
+
center: [
|
|
185
|
+
Math.round(rect.left + rect.width / 2),
|
|
186
|
+
Math.round(rect.top + rect.height / 2)
|
|
187
|
+
],
|
|
188
|
+
zoom: rect.zoom,
|
|
189
|
+
isVisible: rect.isVisible
|
|
190
|
+
};
|
|
191
|
+
return elementInfo;
|
|
192
|
+
}
|
|
193
|
+
return null;
|
|
194
|
+
}
|
|
195
|
+
async function extractTextWithPosition(initNode, debugMode = false) {
|
|
196
|
+
const elementNode = await extractTreeNode(initNode, debugMode);
|
|
197
|
+
const elementInfoArray = [];
|
|
198
|
+
function dfsTopChildren(node) {
|
|
199
|
+
if (node.node) elementInfoArray.push(node.node);
|
|
200
|
+
for(let i = 0; i < node.children.length; i++)dfsTopChildren(node.children[i]);
|
|
201
|
+
}
|
|
202
|
+
dfsTopChildren({
|
|
203
|
+
children: elementNode.children,
|
|
204
|
+
node: elementNode.node
|
|
205
|
+
});
|
|
206
|
+
return elementInfoArray;
|
|
207
|
+
}
|
|
208
|
+
async function extractTreeNodeAsString(initNode, visibleOnly = false, debugMode = false) {
|
|
209
|
+
const elementNode = await extractTreeNode(initNode, debugMode);
|
|
210
|
+
return descriptionOfTree(elementNode, void 0, false, visibleOnly);
|
|
211
|
+
}
|
|
212
|
+
async function extractTreeNode(initNode, debugMode = false, basePoint = {
|
|
213
|
+
left: 0,
|
|
214
|
+
top: 0
|
|
215
|
+
}, baseZoom = 1) {
|
|
216
|
+
setDebugMode(debugMode);
|
|
217
|
+
indexId = 0;
|
|
218
|
+
const topDocument = getTopDocument();
|
|
219
|
+
const startNode = initNode || topDocument;
|
|
220
|
+
const topChildren = [];
|
|
221
|
+
function dfs(node, currentWindow, currentDocument, baseZoom = 1, basePoint = {
|
|
222
|
+
left: 0,
|
|
223
|
+
top: 0
|
|
224
|
+
}) {
|
|
225
|
+
if (!node) return null;
|
|
226
|
+
if (node.nodeType && 10 === node.nodeType) return null;
|
|
227
|
+
const elementInfo = collectElementInfo(node, currentWindow, currentDocument, baseZoom, basePoint, false);
|
|
228
|
+
if (node instanceof currentWindow.HTMLIFrameElement) {
|
|
229
|
+
if (node.contentWindow && node.contentWindow) return null;
|
|
230
|
+
}
|
|
231
|
+
const nodeInfo = {
|
|
232
|
+
node: elementInfo,
|
|
233
|
+
children: []
|
|
234
|
+
};
|
|
235
|
+
if (elementInfo?.nodeType === NodeType.BUTTON || elementInfo?.nodeType === NodeType.IMG || elementInfo?.nodeType === NodeType.TEXT || elementInfo?.nodeType === NodeType.FORM_ITEM || elementInfo?.nodeType === NodeType.CONTAINER) return nodeInfo;
|
|
236
|
+
const rect = getRect(node, baseZoom, currentWindow);
|
|
237
|
+
for(let i = 0; i < node.childNodes.length; i++){
|
|
238
|
+
logger('will dfs', node.childNodes[i]);
|
|
239
|
+
const childNodeInfo = dfs(node.childNodes[i], currentWindow, currentDocument, rect.zoom, basePoint);
|
|
240
|
+
if (Array.isArray(childNodeInfo)) nodeInfo.children.push(...childNodeInfo);
|
|
241
|
+
else if (childNodeInfo) nodeInfo.children.push(childNodeInfo);
|
|
242
|
+
}
|
|
243
|
+
if (null === nodeInfo.node) {
|
|
244
|
+
if (0 === nodeInfo.children.length) return null;
|
|
245
|
+
return nodeInfo.children;
|
|
246
|
+
}
|
|
247
|
+
return nodeInfo;
|
|
248
|
+
}
|
|
249
|
+
function getCssScale(ele) {
|
|
250
|
+
try {
|
|
251
|
+
const { width, height } = ele.getBoundingClientRect();
|
|
252
|
+
return {
|
|
253
|
+
cssScaleX: Number((width / ele.offsetWidth).toFixed(1)),
|
|
254
|
+
cssScaleY: Number((height / ele.offsetHeight).toFixed(1))
|
|
255
|
+
};
|
|
256
|
+
} catch (error) {
|
|
257
|
+
return {
|
|
258
|
+
cssScaleX: 1,
|
|
259
|
+
cssScaleY: 1
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
const rootNodeInfo = dfs(startNode, window, document, baseZoom, basePoint);
|
|
264
|
+
if (Array.isArray(rootNodeInfo)) topChildren.push(...rootNodeInfo);
|
|
265
|
+
else if (rootNodeInfo) topChildren.push(rootNodeInfo);
|
|
266
|
+
if (startNode === topDocument) {
|
|
267
|
+
const iframes = document.querySelectorAll('iframe');
|
|
268
|
+
for(let i = 0; i < iframes.length; i++){
|
|
269
|
+
const iframe = iframes[i];
|
|
270
|
+
const iframeInfo = collectElementInfo(iframe, window, document, 1);
|
|
271
|
+
const baseZoom = getCssScale(iframe).cssScaleX;
|
|
272
|
+
if (iframeInfo) {
|
|
273
|
+
const iframeChildren = await postWindowMessage(iframe.contentWindow, window, {
|
|
274
|
+
action: 'extractTreeNode',
|
|
275
|
+
data: {
|
|
276
|
+
basePoint: {
|
|
277
|
+
left: iframeInfo.rect.left,
|
|
278
|
+
top: iframeInfo.rect.top
|
|
279
|
+
},
|
|
280
|
+
baseZoom
|
|
281
|
+
}
|
|
282
|
+
});
|
|
283
|
+
if (Array.isArray(iframeChildren)) topChildren.push(...iframeChildren);
|
|
284
|
+
else if (iframeChildren) topChildren.push(iframeChildren);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
return {
|
|
289
|
+
node: null,
|
|
290
|
+
children: topChildren
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
function mergeElementAndChildrenRects(node, currentWindow, currentDocument, baseZoom = 1) {
|
|
294
|
+
const selfRect = elementRect(node, currentWindow, currentDocument, baseZoom);
|
|
295
|
+
if (!selfRect) return null;
|
|
296
|
+
let minLeft = selfRect.left;
|
|
297
|
+
let minTop = selfRect.top;
|
|
298
|
+
let maxRight = selfRect.left + selfRect.width;
|
|
299
|
+
let maxBottom = selfRect.top + selfRect.height;
|
|
300
|
+
function traverse(child) {
|
|
301
|
+
for(let i = 0; i < child.childNodes.length; i++){
|
|
302
|
+
const sub = child.childNodes[i];
|
|
303
|
+
if (1 === sub.nodeType) {
|
|
304
|
+
const rect = elementRect(sub, currentWindow, currentDocument, baseZoom);
|
|
305
|
+
if (rect) {
|
|
306
|
+
minLeft = Math.min(minLeft, rect.left);
|
|
307
|
+
minTop = Math.min(minTop, rect.top);
|
|
308
|
+
maxRight = Math.max(maxRight, rect.left + rect.width);
|
|
309
|
+
maxBottom = Math.max(maxBottom, rect.top + rect.height);
|
|
310
|
+
}
|
|
311
|
+
traverse(sub);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
traverse(node);
|
|
316
|
+
return {
|
|
317
|
+
...selfRect,
|
|
318
|
+
left: minLeft,
|
|
319
|
+
top: minTop,
|
|
320
|
+
width: maxRight - minLeft,
|
|
321
|
+
height: maxBottom - minTop
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
if ("u" > typeof window) onWindowMessage(window, async ({ action, data }, { source })=>{
|
|
325
|
+
if ('extractTreeNode' === action) try {
|
|
326
|
+
return await extractTreeNode(document.body, false, data.basePoint, data.baseZoom);
|
|
327
|
+
} catch (error) {
|
|
328
|
+
return {
|
|
329
|
+
error: 'error message'
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
if ('getElementFromPoint' === action) try {
|
|
333
|
+
return await getElementFromPoint({
|
|
334
|
+
x: data.x,
|
|
335
|
+
y: data.y,
|
|
336
|
+
containerPaths: data.containerPaths
|
|
337
|
+
});
|
|
338
|
+
} catch (error) {
|
|
339
|
+
return {
|
|
340
|
+
error: 'error message'
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
});
|
|
344
|
+
const getElementOffset = (element)=>{
|
|
345
|
+
const pLeft = Number.parseInt(window.getComputedStyle(element, null).getPropertyValue('padding-left'), 10) || 0;
|
|
346
|
+
const pTop = Number.parseInt(window.getComputedStyle(element, null).getPropertyValue('padding-top'), 10) || 0;
|
|
347
|
+
const bLeft = Number.parseInt(window.getComputedStyle(element, null).getPropertyValue('border-left-width'), 10) || 0;
|
|
348
|
+
const bTop = Number.parseInt(window.getComputedStyle(element, null).getPropertyValue('border-top-width'), 10) || 0;
|
|
349
|
+
return {
|
|
350
|
+
x: pLeft + bLeft,
|
|
351
|
+
y: pTop + bTop
|
|
352
|
+
};
|
|
353
|
+
};
|
|
354
|
+
const web_extractor_getCssScale = (element)=>{
|
|
355
|
+
try {
|
|
356
|
+
const { width, height } = element.getBoundingClientRect();
|
|
357
|
+
return {
|
|
358
|
+
cssScaleX: Number((width / element.offsetWidth).toFixed(1)),
|
|
359
|
+
cssScaleY: Number((height / element.offsetHeight).toFixed(1))
|
|
360
|
+
};
|
|
361
|
+
} catch (error) {
|
|
362
|
+
return {
|
|
363
|
+
cssScaleX: 1,
|
|
364
|
+
cssScaleY: 1
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
};
|
|
368
|
+
const getElementFromPoint = async (args)=>{
|
|
369
|
+
if (!args.containerPaths) args.containerPaths = [];
|
|
370
|
+
const dom = document.elementFromPoint(args.x, args.y);
|
|
371
|
+
if (dom?.nodeName === 'IFRAME') {
|
|
372
|
+
const containerPath = getContainerPath(dom);
|
|
373
|
+
const bounding = dom.getBoundingClientRect();
|
|
374
|
+
const offset = getElementOffset(dom);
|
|
375
|
+
const { cssScaleX, cssScaleY } = web_extractor_getCssScale(dom);
|
|
376
|
+
return await postWindowMessage(dom.contentWindow, window, {
|
|
377
|
+
action: 'getElementFromPoint',
|
|
378
|
+
data: {
|
|
379
|
+
x: (args.x - bounding.x - offset.x) / cssScaleX,
|
|
380
|
+
y: (args.y - bounding.y - offset.y) / cssScaleY,
|
|
381
|
+
containerPaths: args.containerPaths.concat(containerPath)
|
|
382
|
+
}
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
{
|
|
386
|
+
const elementInfo = {
|
|
387
|
+
allPaths: getLocators(dom),
|
|
388
|
+
containerPaths: args.containerPaths
|
|
389
|
+
};
|
|
390
|
+
return elementInfo;
|
|
391
|
+
}
|
|
392
|
+
};
|
|
393
|
+
export { collectElementInfo, extractTextWithPosition, extractTreeNode, extractTreeNodeAsString, getElementFromPoint, mergeElementAndChildrenRects };
|