@donggui/web 1.5.5-donggui.3

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 (166) hide show
  1. package/README.md +9 -0
  2. package/bin/midscene-playground +3 -0
  3. package/bin/midscene-web +2 -0
  4. package/dist/es/bin.mjs +23 -0
  5. package/dist/es/bin.mjs.map +1 -0
  6. package/dist/es/bridge-mode/agent-cli-side.mjs +137 -0
  7. package/dist/es/bridge-mode/agent-cli-side.mjs.map +1 -0
  8. package/dist/es/bridge-mode/browser.mjs +2 -0
  9. package/dist/es/bridge-mode/common.mjs +43 -0
  10. package/dist/es/bridge-mode/common.mjs.map +1 -0
  11. package/dist/es/bridge-mode/index.mjs +4 -0
  12. package/dist/es/bridge-mode/io-client.mjs +101 -0
  13. package/dist/es/bridge-mode/io-client.mjs.map +1 -0
  14. package/dist/es/bridge-mode/io-server.mjs +210 -0
  15. package/dist/es/bridge-mode/io-server.mjs.map +1 -0
  16. package/dist/es/bridge-mode/page-browser-side.mjs +118 -0
  17. package/dist/es/bridge-mode/page-browser-side.mjs.map +1 -0
  18. package/dist/es/chrome-extension/agent.mjs +9 -0
  19. package/dist/es/chrome-extension/agent.mjs.map +1 -0
  20. package/dist/es/chrome-extension/cdpInput.mjs +174 -0
  21. package/dist/es/chrome-extension/cdpInput.mjs.LICENSE.txt +5 -0
  22. package/dist/es/chrome-extension/cdpInput.mjs.map +1 -0
  23. package/dist/es/chrome-extension/dynamic-scripts.mjs +38 -0
  24. package/dist/es/chrome-extension/dynamic-scripts.mjs.map +1 -0
  25. package/dist/es/chrome-extension/index.mjs +5 -0
  26. package/dist/es/chrome-extension/page.mjs +651 -0
  27. package/dist/es/chrome-extension/page.mjs.map +1 -0
  28. package/dist/es/cli.mjs +16 -0
  29. package/dist/es/cli.mjs.map +1 -0
  30. package/dist/es/common/cache-helper.mjs +28 -0
  31. package/dist/es/common/cache-helper.mjs.map +1 -0
  32. package/dist/es/index.mjs +6 -0
  33. package/dist/es/mcp-server.mjs +35 -0
  34. package/dist/es/mcp-server.mjs.map +1 -0
  35. package/dist/es/mcp-tools-puppeteer.mjs +215 -0
  36. package/dist/es/mcp-tools-puppeteer.mjs.map +1 -0
  37. package/dist/es/mcp-tools.mjs +78 -0
  38. package/dist/es/mcp-tools.mjs.map +1 -0
  39. package/dist/es/playwright/ai-fixture.mjs +367 -0
  40. package/dist/es/playwright/ai-fixture.mjs.map +1 -0
  41. package/dist/es/playwright/index.mjs +40 -0
  42. package/dist/es/playwright/index.mjs.map +1 -0
  43. package/dist/es/playwright/page.mjs +44 -0
  44. package/dist/es/playwright/page.mjs.map +1 -0
  45. package/dist/es/playwright/reporter/index.mjs +216 -0
  46. package/dist/es/playwright/reporter/index.mjs.map +1 -0
  47. package/dist/es/puppeteer/agent-launcher.mjs +185 -0
  48. package/dist/es/puppeteer/agent-launcher.mjs.map +1 -0
  49. package/dist/es/puppeteer/base-page.mjs +564 -0
  50. package/dist/es/puppeteer/base-page.mjs.map +1 -0
  51. package/dist/es/puppeteer/index.mjs +34 -0
  52. package/dist/es/puppeteer/index.mjs.map +1 -0
  53. package/dist/es/puppeteer/page.mjs +9 -0
  54. package/dist/es/puppeteer/page.mjs.map +1 -0
  55. package/dist/es/static/index.mjs +3 -0
  56. package/dist/es/static/static-agent.mjs +12 -0
  57. package/dist/es/static/static-agent.mjs.map +1 -0
  58. package/dist/es/static/static-page.mjs +122 -0
  59. package/dist/es/static/static-page.mjs.map +1 -0
  60. package/dist/es/utils.mjs +8 -0
  61. package/dist/es/utils.mjs.map +1 -0
  62. package/dist/es/web-element.mjs +59 -0
  63. package/dist/es/web-element.mjs.map +1 -0
  64. package/dist/es/web-page.mjs +260 -0
  65. package/dist/es/web-page.mjs.map +1 -0
  66. package/dist/lib/bin.js +29 -0
  67. package/dist/lib/bin.js.map +1 -0
  68. package/dist/lib/bridge-mode/agent-cli-side.js +174 -0
  69. package/dist/lib/bridge-mode/agent-cli-side.js.map +1 -0
  70. package/dist/lib/bridge-mode/browser.js +38 -0
  71. package/dist/lib/bridge-mode/browser.js.map +1 -0
  72. package/dist/lib/bridge-mode/common.js +107 -0
  73. package/dist/lib/bridge-mode/common.js.map +1 -0
  74. package/dist/lib/bridge-mode/index.js +46 -0
  75. package/dist/lib/bridge-mode/index.js.map +1 -0
  76. package/dist/lib/bridge-mode/io-client.js +135 -0
  77. package/dist/lib/bridge-mode/io-client.js.map +1 -0
  78. package/dist/lib/bridge-mode/io-server.js +247 -0
  79. package/dist/lib/bridge-mode/io-server.js.map +1 -0
  80. package/dist/lib/bridge-mode/page-browser-side.js +162 -0
  81. package/dist/lib/bridge-mode/page-browser-side.js.map +1 -0
  82. package/dist/lib/chrome-extension/agent.js +43 -0
  83. package/dist/lib/chrome-extension/agent.js.map +1 -0
  84. package/dist/lib/chrome-extension/cdpInput.js +208 -0
  85. package/dist/lib/chrome-extension/cdpInput.js.LICENSE.txt +5 -0
  86. package/dist/lib/chrome-extension/cdpInput.js.map +1 -0
  87. package/dist/lib/chrome-extension/dynamic-scripts.js +88 -0
  88. package/dist/lib/chrome-extension/dynamic-scripts.js.map +1 -0
  89. package/dist/lib/chrome-extension/index.js +60 -0
  90. package/dist/lib/chrome-extension/index.js.map +1 -0
  91. package/dist/lib/chrome-extension/page.js +685 -0
  92. package/dist/lib/chrome-extension/page.js.map +1 -0
  93. package/dist/lib/cli.js +22 -0
  94. package/dist/lib/cli.js.map +1 -0
  95. package/dist/lib/common/cache-helper.js +68 -0
  96. package/dist/lib/common/cache-helper.js.map +1 -0
  97. package/dist/lib/index.js +60 -0
  98. package/dist/lib/index.js.map +1 -0
  99. package/dist/lib/mcp-server.js +75 -0
  100. package/dist/lib/mcp-server.js.map +1 -0
  101. package/dist/lib/mcp-tools-puppeteer.js +259 -0
  102. package/dist/lib/mcp-tools-puppeteer.js.map +1 -0
  103. package/dist/lib/mcp-tools.js +112 -0
  104. package/dist/lib/mcp-tools.js.map +1 -0
  105. package/dist/lib/playwright/ai-fixture.js +404 -0
  106. package/dist/lib/playwright/ai-fixture.js.map +1 -0
  107. package/dist/lib/playwright/index.js +93 -0
  108. package/dist/lib/playwright/index.js.map +1 -0
  109. package/dist/lib/playwright/page.js +78 -0
  110. package/dist/lib/playwright/page.js.map +1 -0
  111. package/dist/lib/playwright/reporter/index.js +250 -0
  112. package/dist/lib/playwright/reporter/index.js.map +1 -0
  113. package/dist/lib/puppeteer/agent-launcher.js +253 -0
  114. package/dist/lib/puppeteer/agent-launcher.js.map +1 -0
  115. package/dist/lib/puppeteer/base-page.js +607 -0
  116. package/dist/lib/puppeteer/base-page.js.map +1 -0
  117. package/dist/lib/puppeteer/index.js +84 -0
  118. package/dist/lib/puppeteer/index.js.map +1 -0
  119. package/dist/lib/puppeteer/page.js +43 -0
  120. package/dist/lib/puppeteer/page.js.map +1 -0
  121. package/dist/lib/static/index.js +52 -0
  122. package/dist/lib/static/index.js.map +1 -0
  123. package/dist/lib/static/static-agent.js +46 -0
  124. package/dist/lib/static/static-agent.js.map +1 -0
  125. package/dist/lib/static/static-page.js +156 -0
  126. package/dist/lib/static/static-page.js.map +1 -0
  127. package/dist/lib/utils.js +40 -0
  128. package/dist/lib/utils.js.map +1 -0
  129. package/dist/lib/web-element.js +96 -0
  130. package/dist/lib/web-element.js.map +1 -0
  131. package/dist/lib/web-page.js +310 -0
  132. package/dist/lib/web-page.js.map +1 -0
  133. package/dist/types/bin.d.ts +1 -0
  134. package/dist/types/bridge-mode/agent-cli-side.d.ts +49 -0
  135. package/dist/types/bridge-mode/browser.d.ts +2 -0
  136. package/dist/types/bridge-mode/common.d.ts +74 -0
  137. package/dist/types/bridge-mode/index.d.ts +4 -0
  138. package/dist/types/bridge-mode/io-client.d.ts +10 -0
  139. package/dist/types/bridge-mode/io-server.d.ts +27 -0
  140. package/dist/types/bridge-mode/page-browser-side.d.ts +21 -0
  141. package/dist/types/chrome-extension/agent.d.ts +5 -0
  142. package/dist/types/chrome-extension/cdpInput.d.ts +52 -0
  143. package/dist/types/chrome-extension/dynamic-scripts.d.ts +3 -0
  144. package/dist/types/chrome-extension/index.d.ts +5 -0
  145. package/dist/types/chrome-extension/page.d.ts +110 -0
  146. package/dist/types/cli.d.ts +1 -0
  147. package/dist/types/common/cache-helper.d.ts +20 -0
  148. package/dist/types/index.d.ts +7 -0
  149. package/dist/types/mcp-server.d.ts +26 -0
  150. package/dist/types/mcp-tools-puppeteer.d.ts +13 -0
  151. package/dist/types/mcp-tools.d.ts +12 -0
  152. package/dist/types/playwright/ai-fixture.d.ts +131 -0
  153. package/dist/types/playwright/index.d.ts +13 -0
  154. package/dist/types/playwright/page.d.ts +11 -0
  155. package/dist/types/playwright/reporter/index.d.ts +42 -0
  156. package/dist/types/puppeteer/agent-launcher.d.ts +61 -0
  157. package/dist/types/puppeteer/base-page.d.ts +106 -0
  158. package/dist/types/puppeteer/index.d.ts +10 -0
  159. package/dist/types/puppeteer/page.d.ts +6 -0
  160. package/dist/types/static/index.d.ts +2 -0
  161. package/dist/types/static/static-agent.d.ts +5 -0
  162. package/dist/types/static/static-page.d.ts +42 -0
  163. package/dist/types/utils.d.ts +6 -0
  164. package/dist/types/web-element.d.ts +48 -0
  165. package/dist/types/web-page.d.ts +62 -0
  166. package/package.json +166 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chrome-extension/index.js","sources":["webpack/runtime/compat_get_default_export","webpack/runtime/define_property_getters","webpack/runtime/has_own_property","webpack/runtime/make_namespace_object"],"sourcesContent":["// getDefaultExport function for compatibility with non-ESM modules\n__webpack_require__.n = (module) => {\n\tvar getter = module && module.__esModule ?\n\t\t() => (module['default']) :\n\t\t() => (module);\n\t__webpack_require__.d(getter, { a: getter });\n\treturn getter;\n};\n","__webpack_require__.d = (exports, definition) => {\n\tfor(var key in definition) {\n if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {\n Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });\n }\n }\n};","__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))","// define __esModule on exports\n__webpack_require__.r = (exports) => {\n\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\n\t\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n\t}\n\tObject.defineProperty(exports, '__esModule', { value: true });\n};"],"names":["__webpack_require__","module","getter","definition","key","Object","obj","prop","Symbol"],"mappings":";;;IACAA,oBAAoB,CAAC,GAAG,CAACC;QACxB,IAAIC,SAASD,UAAUA,OAAO,UAAU,GACvC,IAAOA,MAAM,CAAC,UAAU,GACxB,IAAOA;QACRD,oBAAoB,CAAC,CAACE,QAAQ;YAAE,GAAGA;QAAO;QAC1C,OAAOA;IACR;;;ICPAF,oBAAoB,CAAC,GAAG,CAAC,UAASG;QACjC,IAAI,IAAIC,OAAOD,WACR,IAAGH,oBAAoB,CAAC,CAACG,YAAYC,QAAQ,CAACJ,oBAAoB,CAAC,CAAC,UAASI,MACzEC,OAAO,cAAc,CAAC,UAASD,KAAK;YAAE,YAAY;YAAM,KAAKD,UAAU,CAACC,IAAI;QAAC;IAGzF;;;ICNAJ,oBAAoB,CAAC,GAAG,CAACM,KAAKC,OAAUF,OAAO,SAAS,CAAC,cAAc,CAAC,IAAI,CAACC,KAAKC;;;ICClFP,oBAAoB,CAAC,GAAG,CAAC;QACxB,IAAG,AAAkB,eAAlB,OAAOQ,UAA0BA,OAAO,WAAW,EACrDH,OAAO,cAAc,CAAC,UAASG,OAAO,WAAW,EAAE;YAAE,OAAO;QAAS;QAEtEH,OAAO,cAAc,CAAC,UAAS,cAAc;YAAE,OAAO;QAAK;IAC5D"}
@@ -0,0 +1,685 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, definition)=>{
5
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
6
+ enumerable: true,
7
+ get: definition[key]
8
+ });
9
+ };
10
+ })();
11
+ (()=>{
12
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
13
+ })();
14
+ (()=>{
15
+ __webpack_require__.r = (exports1)=>{
16
+ if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
17
+ value: 'Module'
18
+ });
19
+ Object.defineProperty(exports1, '__esModule', {
20
+ value: true
21
+ });
22
+ };
23
+ })();
24
+ var __webpack_exports__ = {};
25
+ __webpack_require__.r(__webpack_exports__);
26
+ __webpack_require__.d(__webpack_exports__, {
27
+ default: ()=>ChromeExtensionProxyPage
28
+ });
29
+ const external_web_element_js_namespaceObject = require("../web-element.js");
30
+ const extractor_namespaceObject = require("@midscene/shared/extractor");
31
+ const img_namespaceObject = require("@midscene/shared/img");
32
+ const logger_namespaceObject = require("@midscene/shared/logger");
33
+ const utils_namespaceObject = require("@midscene/shared/utils");
34
+ const cache_helper_js_namespaceObject = require("../common/cache-helper.js");
35
+ const external_web_page_js_namespaceObject = require("../web-page.js");
36
+ const external_cdpInput_js_namespaceObject = require("./cdpInput.js");
37
+ const external_dynamic_scripts_js_namespaceObject = require("./dynamic-scripts.js");
38
+ function _define_property(obj, key, value) {
39
+ if (key in obj) Object.defineProperty(obj, key, {
40
+ value: value,
41
+ enumerable: true,
42
+ configurable: true,
43
+ writable: true
44
+ });
45
+ else obj[key] = value;
46
+ return obj;
47
+ }
48
+ const debug = (0, logger_namespaceObject.getDebug)('web:chrome-extension:page');
49
+ function sleep(ms) {
50
+ return new Promise((resolve)=>setTimeout(resolve, ms));
51
+ }
52
+ class ChromeExtensionProxyPage {
53
+ actionSpace() {
54
+ return (0, external_web_page_js_namespaceObject.commonWebActionsForWebPage)(this);
55
+ }
56
+ async setActiveTabId(tabId) {
57
+ if (this.activeTabId) throw new Error(`Active tab id is already set, which is ${this.activeTabId}, cannot set it to ${tabId}`);
58
+ await chrome.tabs.update(tabId, {
59
+ active: true
60
+ });
61
+ this.activeTabId = tabId;
62
+ }
63
+ async getActiveTabId() {
64
+ return this.activeTabId;
65
+ }
66
+ async getBrowserTabList() {
67
+ const tabs = await chrome.tabs.query({
68
+ currentWindow: true
69
+ });
70
+ return tabs.map((tab)=>({
71
+ id: `${tab.id}`,
72
+ title: tab.title,
73
+ url: tab.url,
74
+ currentActiveTab: tab.active
75
+ })).filter((tab)=>tab.id && tab.title && tab.url);
76
+ }
77
+ async getTabIdOrConnectToCurrentTab() {
78
+ if (this.activeTabId) return this.activeTabId;
79
+ const tabId = await chrome.tabs.query({
80
+ active: true,
81
+ currentWindow: true
82
+ }).then((tabs)=>tabs[0]?.id);
83
+ this.activeTabId = tabId || 0;
84
+ return this.activeTabId;
85
+ }
86
+ async ensureDebuggerAttached() {
87
+ (0, utils_namespaceObject.assert)(!this.destroyed, 'Page is destroyed');
88
+ const url = await this.url();
89
+ if (url.startsWith('chrome://')) throw new Error('Cannot attach debugger to chrome:// pages, please use Midscene in a normal page with http://, https:// or file://');
90
+ const tabId = await this.getTabIdOrConnectToCurrentTab();
91
+ try {
92
+ await chrome.debugger.attach({
93
+ tabId
94
+ }, '1.3');
95
+ console.log('Debugger attached to tab:', tabId);
96
+ } catch (error) {
97
+ const errorMsg = error?.message || '';
98
+ if (errorMsg.includes('Another debugger is already attached')) return void console.log('Debugger already attached to tab:', tabId);
99
+ if (this._continueWhenFailedToAttachDebugger) return void console.warn('Failed to attach debugger, but continuing due to _continueWhenFailedToAttachDebugger flag', error);
100
+ throw error;
101
+ }
102
+ await sleep(500);
103
+ await this.enableWaterFlowAnimation();
104
+ }
105
+ async showMousePointer(x, y) {
106
+ const pointerScript = `(() => {
107
+ if(typeof window.midsceneWaterFlowAnimation !== 'undefined') {
108
+ window.midsceneWaterFlowAnimation.enable();
109
+ window.midsceneWaterFlowAnimation.showMousePointer(${x}, ${y});
110
+ } else {
111
+ console.log('midsceneWaterFlowAnimation is not defined');
112
+ }
113
+ })()`;
114
+ await this.sendCommandToDebugger('Runtime.evaluate', {
115
+ expression: `${pointerScript}`
116
+ });
117
+ }
118
+ async hideMousePointer() {
119
+ await this.sendCommandToDebugger('Runtime.evaluate', {
120
+ expression: `(() => {
121
+ if(typeof window.midsceneWaterFlowAnimation !== 'undefined') {
122
+ window.midsceneWaterFlowAnimation.hideMousePointer();
123
+ }
124
+ })()`
125
+ });
126
+ }
127
+ async detachDebugger(tabId) {
128
+ const tabIdToDetach = tabId || await this.getTabIdOrConnectToCurrentTab();
129
+ console.log('detaching debugger from tab:', tabIdToDetach);
130
+ try {
131
+ await this.disableWaterFlowAnimation(tabIdToDetach);
132
+ await sleep(200);
133
+ } catch (error) {
134
+ console.warn('Failed to disable water flow animation', error);
135
+ }
136
+ try {
137
+ await chrome.debugger.detach({
138
+ tabId: tabIdToDetach
139
+ });
140
+ console.log('Debugger detached successfully from tab:', tabIdToDetach);
141
+ } catch (error) {
142
+ console.warn('Failed to detach debugger (may already be detached):', error);
143
+ }
144
+ }
145
+ async enableWaterFlowAnimation() {
146
+ const tabId = await this.getTabIdOrConnectToCurrentTab();
147
+ if (this.forceSameTabNavigation) await chrome.debugger.sendCommand({
148
+ tabId
149
+ }, 'Runtime.evaluate', {
150
+ expression: external_web_element_js_namespaceObject.limitOpenNewTabScript
151
+ });
152
+ const script = await (0, external_dynamic_scripts_js_namespaceObject.injectWaterFlowAnimation)();
153
+ await chrome.debugger.sendCommand({
154
+ tabId
155
+ }, 'Runtime.evaluate', {
156
+ expression: script
157
+ });
158
+ }
159
+ async disableWaterFlowAnimation(tabId) {
160
+ const script = await (0, external_dynamic_scripts_js_namespaceObject.injectStopWaterFlowAnimation)();
161
+ await chrome.debugger.sendCommand({
162
+ tabId
163
+ }, 'Runtime.evaluate', {
164
+ expression: script
165
+ });
166
+ }
167
+ async sendCommandToDebugger(command, params, retryCount = 0) {
168
+ const MAX_RETRIES = 2;
169
+ const tabId = await this.getTabIdOrConnectToCurrentTab();
170
+ try {
171
+ const result = await chrome.debugger.sendCommand({
172
+ tabId
173
+ }, command, params);
174
+ this.enableWaterFlowAnimation().catch((err)=>{
175
+ console.warn('Failed to enable water flow animation:', err);
176
+ });
177
+ return result;
178
+ } catch (error) {
179
+ const errorMsg = error?.message || '';
180
+ const isDetachError = errorMsg.includes('Debugger is not attached') || errorMsg.includes('Cannot access a Target') || errorMsg.includes('No target with given id');
181
+ if (isDetachError && retryCount < MAX_RETRIES) {
182
+ console.log(`Debugger not attached for command "${command}", attempting to attach (retry ${retryCount + 1}/${MAX_RETRIES})`);
183
+ await this.ensureDebuggerAttached();
184
+ return this.sendCommandToDebugger(command, params, retryCount + 1);
185
+ }
186
+ throw error;
187
+ }
188
+ }
189
+ async getPageContentByCDP() {
190
+ const script = await (0, external_dynamic_scripts_js_namespaceObject.getHtmlElementScript)();
191
+ await this.sendCommandToDebugger('Runtime.evaluate', {
192
+ expression: script
193
+ });
194
+ const expression = ()=>{
195
+ const tree = window.midscene_element_inspector.webExtractNodeTree();
196
+ return {
197
+ tree,
198
+ size: {
199
+ width: document.documentElement.clientWidth,
200
+ height: document.documentElement.clientHeight
201
+ }
202
+ };
203
+ };
204
+ const returnValue = await this.sendCommandToDebugger('Runtime.evaluate', {
205
+ expression: `(${expression.toString()})()`,
206
+ returnByValue: true
207
+ });
208
+ if (!returnValue.result.value) {
209
+ const errorDescription = returnValue.exceptionDetails?.exception?.description || '';
210
+ if (!errorDescription) console.error('returnValue from cdp', returnValue);
211
+ throw new Error(`Failed to get page content from page, error: ${errorDescription}`);
212
+ }
213
+ return returnValue.result.value;
214
+ }
215
+ async evaluateJavaScript(script) {
216
+ return this.sendCommandToDebugger('Runtime.evaluate', {
217
+ expression: script,
218
+ awaitPromise: true
219
+ });
220
+ }
221
+ async beforeInvokeAction() {
222
+ try {
223
+ await this.waitUntilNetworkIdle();
224
+ } catch (error) {}
225
+ }
226
+ async waitUntilNetworkIdle() {
227
+ const timeout = 10000;
228
+ const startTime = Date.now();
229
+ let lastReadyState = '';
230
+ while(Date.now() - startTime < timeout){
231
+ const result = await this.sendCommandToDebugger('Runtime.evaluate', {
232
+ expression: 'document.readyState'
233
+ });
234
+ lastReadyState = result.result.value;
235
+ if ('complete' === lastReadyState) return void await new Promise((resolve)=>setTimeout(resolve, 300));
236
+ await new Promise((resolve)=>setTimeout(resolve, 300));
237
+ }
238
+ throw new Error(`Failed to wait until network idle, last readyState: ${lastReadyState}`);
239
+ }
240
+ async getElementsInfo() {
241
+ const tree = await this.getElementsNodeTree();
242
+ return (0, extractor_namespaceObject.treeToList)(tree);
243
+ }
244
+ async getXpathsByPoint(point, isOrderSensitive = false) {
245
+ const script = await (0, external_dynamic_scripts_js_namespaceObject.getHtmlElementScript)();
246
+ await this.sendCommandToDebugger('Runtime.evaluate', {
247
+ expression: script
248
+ });
249
+ const result = await this.sendCommandToDebugger('Runtime.evaluate', {
250
+ expression: `window.midscene_element_inspector.getXpathsByPoint({left: ${point.left}, top: ${point.top}}, ${isOrderSensitive})`,
251
+ returnByValue: true
252
+ });
253
+ return result.result.value;
254
+ }
255
+ async getElementInfoByXpath(xpath) {
256
+ const script = await (0, external_dynamic_scripts_js_namespaceObject.getHtmlElementScript)();
257
+ await this.sendCommandToDebugger('Runtime.evaluate', {
258
+ expression: script
259
+ });
260
+ const result = await this.sendCommandToDebugger('Runtime.evaluate', {
261
+ expression: `window.midscene_element_inspector.getElementInfoByXpath(${JSON.stringify(xpath)})`,
262
+ returnByValue: true
263
+ });
264
+ return result.result.value;
265
+ }
266
+ async cacheFeatureForPoint(center, options) {
267
+ const point = {
268
+ left: center[0],
269
+ top: center[1]
270
+ };
271
+ try {
272
+ const isOrderSensitive = await (0, cache_helper_js_namespaceObject.judgeOrderSensitive)(options, debug);
273
+ const xpaths = await this.getXpathsByPoint(point, isOrderSensitive);
274
+ return {
275
+ xpaths: (0, cache_helper_js_namespaceObject.sanitizeXpaths)(xpaths)
276
+ };
277
+ } catch (error) {
278
+ debug('cacheFeatureForPoint failed: %O', error);
279
+ return {
280
+ xpaths: []
281
+ };
282
+ }
283
+ }
284
+ async rectMatchesCacheFeature(feature) {
285
+ const xpaths = (0, cache_helper_js_namespaceObject.sanitizeXpaths)(feature.xpaths);
286
+ for (const xpath of xpaths)try {
287
+ const elementInfo = await this.getElementInfoByXpath(xpath);
288
+ if (elementInfo?.rect) return (0, cache_helper_js_namespaceObject.buildRectFromElementInfo)(elementInfo);
289
+ } catch (error) {
290
+ debug('rectMatchesCacheFeature failed for xpath %s: %O', xpath, error);
291
+ }
292
+ throw new Error(`No matching element rect found for cache feature (tried ${xpaths.length} xpath(s))`);
293
+ }
294
+ async getElementsNodeTree() {
295
+ await this.hideMousePointer();
296
+ const content = await this.getPageContentByCDP();
297
+ if (content?.size) this.viewportSize = content.size;
298
+ return content?.tree || {
299
+ node: null,
300
+ children: []
301
+ };
302
+ }
303
+ async size() {
304
+ if (this.viewportSize) return this.viewportSize;
305
+ const result = await this.sendCommandToDebugger('Runtime.evaluate', {
306
+ expression: '({width: window.innerWidth, height: window.innerHeight})',
307
+ returnByValue: true
308
+ });
309
+ const sizeInfo = result.result.value;
310
+ console.log('sizeInfo', sizeInfo);
311
+ this.viewportSize = sizeInfo;
312
+ return sizeInfo;
313
+ }
314
+ async screenshotBase64() {
315
+ await this.hideMousePointer();
316
+ const format = 'jpeg';
317
+ const base64 = await this.sendCommandToDebugger('Page.captureScreenshot', {
318
+ format,
319
+ quality: 90
320
+ });
321
+ return (0, img_namespaceObject.createImgBase64ByFormat)(format, base64.data);
322
+ }
323
+ async url() {
324
+ const tabId = await this.getTabIdOrConnectToCurrentTab();
325
+ const url = await chrome.tabs.get(tabId).then((tab)=>tab.url);
326
+ return url || '';
327
+ }
328
+ async navigate(url) {
329
+ const tabId = await this.getTabIdOrConnectToCurrentTab();
330
+ await chrome.tabs.update(tabId, {
331
+ url
332
+ });
333
+ await this.waitUntilNetworkIdle();
334
+ }
335
+ async reload() {
336
+ const tabId = await this.getTabIdOrConnectToCurrentTab();
337
+ await chrome.tabs.reload(tabId);
338
+ await this.waitUntilNetworkIdle();
339
+ }
340
+ async goBack() {
341
+ const tabId = await this.getTabIdOrConnectToCurrentTab();
342
+ await chrome.tabs.goBack(tabId);
343
+ await this.waitUntilNetworkIdle();
344
+ }
345
+ async scrollUntilTop(startingPoint) {
346
+ if (startingPoint) await this.mouse.move(startingPoint.left, startingPoint.top);
347
+ return this.mouse.wheel(0, -9999999);
348
+ }
349
+ async scrollUntilBottom(startingPoint) {
350
+ if (startingPoint) await this.mouse.move(startingPoint.left, startingPoint.top);
351
+ return this.mouse.wheel(0, 9999999);
352
+ }
353
+ async scrollUntilLeft(startingPoint) {
354
+ if (startingPoint) await this.mouse.move(startingPoint.left, startingPoint.top);
355
+ return this.mouse.wheel(-9999999, 0);
356
+ }
357
+ async scrollUntilRight(startingPoint) {
358
+ if (startingPoint) await this.mouse.move(startingPoint.left, startingPoint.top);
359
+ return this.mouse.wheel(9999999, 0);
360
+ }
361
+ async scrollUp(distance, startingPoint) {
362
+ const { height } = await this.size();
363
+ const scrollDistance = distance || 0.7 * height;
364
+ return this.mouse.wheel(0, -scrollDistance, startingPoint?.left, startingPoint?.top);
365
+ }
366
+ async scrollDown(distance, startingPoint) {
367
+ const { height } = await this.size();
368
+ const scrollDistance = distance || 0.7 * height;
369
+ return this.mouse.wheel(0, scrollDistance, startingPoint?.left, startingPoint?.top);
370
+ }
371
+ async scrollLeft(distance, startingPoint) {
372
+ const { width } = await this.size();
373
+ const scrollDistance = distance || 0.7 * width;
374
+ return this.mouse.wheel(-scrollDistance, 0, startingPoint?.left, startingPoint?.top);
375
+ }
376
+ async scrollRight(distance, startingPoint) {
377
+ const { width } = await this.size();
378
+ const scrollDistance = distance || 0.7 * width;
379
+ return this.mouse.wheel(scrollDistance, 0, startingPoint?.left, startingPoint?.top);
380
+ }
381
+ async clearInput(element) {
382
+ if (!element) return void console.warn('No element to clear input');
383
+ await this.mouse.click(element.center[0], element.center[1]);
384
+ await this.sendCommandToDebugger('Input.dispatchKeyEvent', {
385
+ type: 'keyDown',
386
+ commands: [
387
+ 'selectAll'
388
+ ]
389
+ });
390
+ await this.sendCommandToDebugger('Input.dispatchKeyEvent', {
391
+ type: 'keyUp',
392
+ commands: [
393
+ 'selectAll'
394
+ ]
395
+ });
396
+ await sleep(100);
397
+ await this.keyboard.press({
398
+ key: 'Backspace'
399
+ });
400
+ }
401
+ async destroy() {
402
+ this.destroyed = true;
403
+ const tabIdToDetach = this.activeTabId;
404
+ this.activeTabId = null;
405
+ if (tabIdToDetach) await this.detachDebugger(tabIdToDetach);
406
+ }
407
+ async longPress(x, y, duration) {
408
+ duration = duration || 500;
409
+ const LONG_PRESS_THRESHOLD = 600;
410
+ const MIN_PRESS_THRESHOLD = 300;
411
+ if (duration > LONG_PRESS_THRESHOLD) duration = LONG_PRESS_THRESHOLD;
412
+ if (duration < MIN_PRESS_THRESHOLD) duration = MIN_PRESS_THRESHOLD;
413
+ await this.mouse.move(x, y);
414
+ if (null === this.isMobileEmulation) {
415
+ const result = await this.sendCommandToDebugger('Runtime.evaluate', {
416
+ expression: `(() => {
417
+ return /Android|iPhone|iPad|iPod|Mobile/i.test(navigator.userAgent);
418
+ })()`,
419
+ returnByValue: true
420
+ });
421
+ this.isMobileEmulation = result?.result?.value;
422
+ }
423
+ if (this.isMobileEmulation) {
424
+ const touchPoints = [
425
+ {
426
+ x: Math.round(x),
427
+ y: Math.round(y)
428
+ }
429
+ ];
430
+ await this.sendCommandToDebugger('Input.dispatchTouchEvent', {
431
+ type: 'touchStart',
432
+ touchPoints,
433
+ modifiers: 0
434
+ });
435
+ await new Promise((res)=>setTimeout(res, duration));
436
+ await this.sendCommandToDebugger('Input.dispatchTouchEvent', {
437
+ type: 'touchEnd',
438
+ touchPoints: [],
439
+ modifiers: 0
440
+ });
441
+ } else {
442
+ await this.sendCommandToDebugger('Input.dispatchMouseEvent', {
443
+ type: 'mousePressed',
444
+ x,
445
+ y,
446
+ button: 'left',
447
+ clickCount: 1
448
+ });
449
+ await new Promise((res)=>setTimeout(res, duration));
450
+ await this.sendCommandToDebugger('Input.dispatchMouseEvent', {
451
+ type: 'mouseReleased',
452
+ x,
453
+ y,
454
+ button: 'left',
455
+ clickCount: 1
456
+ });
457
+ }
458
+ this.latestMouseX = x;
459
+ this.latestMouseY = y;
460
+ }
461
+ async swipe(from, to, duration) {
462
+ const LONG_PRESS_THRESHOLD = 500;
463
+ const MIN_PRESS_THRESHOLD = 150;
464
+ duration = duration || 300;
465
+ if (duration < MIN_PRESS_THRESHOLD) duration = MIN_PRESS_THRESHOLD;
466
+ if (duration > LONG_PRESS_THRESHOLD) duration = LONG_PRESS_THRESHOLD;
467
+ if (null === this.isMobileEmulation) {
468
+ const result = await this.sendCommandToDebugger('Runtime.evaluate', {
469
+ expression: `(() => {
470
+ return /Android|iPhone|iPad|iPod|Mobile/i.test(navigator.userAgent);
471
+ })()`,
472
+ returnByValue: true
473
+ });
474
+ this.isMobileEmulation = result?.result?.value;
475
+ }
476
+ const steps = 30;
477
+ const delay = duration / steps;
478
+ if (this.isMobileEmulation) {
479
+ await this.sendCommandToDebugger('Input.dispatchTouchEvent', {
480
+ type: 'touchStart',
481
+ touchPoints: [
482
+ {
483
+ x: Math.round(from.x),
484
+ y: Math.round(from.y)
485
+ }
486
+ ],
487
+ modifiers: 0
488
+ });
489
+ for(let i = 1; i <= steps; i++){
490
+ const x = from.x + (to.x - from.x) * (i / steps);
491
+ const y = from.y + (to.y - from.y) * (i / steps);
492
+ await this.sendCommandToDebugger('Input.dispatchTouchEvent', {
493
+ type: 'touchMove',
494
+ touchPoints: [
495
+ {
496
+ x: Math.round(x),
497
+ y: Math.round(y)
498
+ }
499
+ ],
500
+ modifiers: 0
501
+ });
502
+ await new Promise((res)=>setTimeout(res, delay));
503
+ }
504
+ await this.sendCommandToDebugger('Input.dispatchTouchEvent', {
505
+ type: 'touchEnd',
506
+ touchPoints: [],
507
+ modifiers: 0
508
+ });
509
+ } else {
510
+ await this.mouse.move(from.x, from.y);
511
+ await this.sendCommandToDebugger('Input.dispatchMouseEvent', {
512
+ type: 'mousePressed',
513
+ x: from.x,
514
+ y: from.y,
515
+ button: 'left',
516
+ clickCount: 1
517
+ });
518
+ for(let i = 1; i <= steps; i++){
519
+ const x = from.x + (to.x - from.x) * (i / steps);
520
+ const y = from.y + (to.y - from.y) * (i / steps);
521
+ await this.mouse.move(x, y);
522
+ await new Promise((res)=>setTimeout(res, delay));
523
+ }
524
+ await this.sendCommandToDebugger('Input.dispatchMouseEvent', {
525
+ type: 'mouseReleased',
526
+ x: to.x,
527
+ y: to.y,
528
+ button: 'left',
529
+ clickCount: 1
530
+ });
531
+ }
532
+ this.latestMouseX = to.x;
533
+ this.latestMouseY = to.y;
534
+ }
535
+ constructor(forceSameTabNavigation){
536
+ _define_property(this, "interfaceType", 'chrome-extension-proxy');
537
+ _define_property(this, "forceSameTabNavigation", void 0);
538
+ _define_property(this, "viewportSize", void 0);
539
+ _define_property(this, "activeTabId", null);
540
+ _define_property(this, "destroyed", false);
541
+ _define_property(this, "isMobileEmulation", null);
542
+ _define_property(this, "_continueWhenFailedToAttachDebugger", false);
543
+ _define_property(this, "latestMouseX", 100);
544
+ _define_property(this, "latestMouseY", 100);
545
+ _define_property(this, "mouse", {
546
+ click: async (x, y, options)=>{
547
+ const { button = 'left', count = 1 } = options || {};
548
+ await this.mouse.move(x, y);
549
+ if (null === this.isMobileEmulation) {
550
+ const result = await this.sendCommandToDebugger('Runtime.evaluate', {
551
+ expression: `(() => {
552
+ return /Android|iPhone|iPad|iPod|Mobile/i.test(navigator.userAgent);
553
+ })()`,
554
+ returnByValue: true
555
+ });
556
+ this.isMobileEmulation = result?.result?.value;
557
+ }
558
+ if (this.isMobileEmulation && 'left' === button) {
559
+ const touchPoints = [
560
+ {
561
+ x: Math.round(x),
562
+ y: Math.round(y)
563
+ }
564
+ ];
565
+ await this.sendCommandToDebugger('Input.dispatchTouchEvent', {
566
+ type: 'touchStart',
567
+ touchPoints,
568
+ modifiers: 0
569
+ });
570
+ await this.sendCommandToDebugger('Input.dispatchTouchEvent', {
571
+ type: 'touchEnd',
572
+ touchPoints: [],
573
+ modifiers: 0
574
+ });
575
+ } else for(let i = 0; i < count; i++){
576
+ await this.sendCommandToDebugger('Input.dispatchMouseEvent', {
577
+ type: 'mousePressed',
578
+ x,
579
+ y,
580
+ button,
581
+ clickCount: 1
582
+ });
583
+ await this.sendCommandToDebugger('Input.dispatchMouseEvent', {
584
+ type: 'mouseReleased',
585
+ x,
586
+ y,
587
+ button,
588
+ clickCount: 1
589
+ });
590
+ await sleep(50);
591
+ }
592
+ },
593
+ wheel: async (deltaX, deltaY, startX, startY)=>{
594
+ const finalX = startX || this.latestMouseX;
595
+ const finalY = startY || this.latestMouseY;
596
+ await this.showMousePointer(finalX, finalY);
597
+ await this.sendCommandToDebugger('Input.dispatchMouseEvent', {
598
+ type: 'mouseWheel',
599
+ x: finalX,
600
+ y: finalY,
601
+ deltaX,
602
+ deltaY
603
+ });
604
+ this.latestMouseX = finalX;
605
+ this.latestMouseY = finalY;
606
+ },
607
+ move: async (x, y)=>{
608
+ await this.showMousePointer(x, y);
609
+ await this.sendCommandToDebugger('Input.dispatchMouseEvent', {
610
+ type: 'mouseMoved',
611
+ x,
612
+ y
613
+ });
614
+ this.latestMouseX = x;
615
+ this.latestMouseY = y;
616
+ },
617
+ drag: async (from, to)=>{
618
+ await this.mouse.move(from.x, from.y);
619
+ await sleep(200);
620
+ await this.sendCommandToDebugger('Input.dispatchMouseEvent', {
621
+ type: 'mousePressed',
622
+ x: from.x,
623
+ y: from.y,
624
+ button: 'left',
625
+ clickCount: 1
626
+ });
627
+ await sleep(300);
628
+ await this.sendCommandToDebugger('Input.dispatchMouseEvent', {
629
+ type: 'mouseMoved',
630
+ x: to.x,
631
+ y: to.y
632
+ });
633
+ await sleep(500);
634
+ await this.sendCommandToDebugger('Input.dispatchMouseEvent', {
635
+ type: 'mouseReleased',
636
+ x: to.x,
637
+ y: to.y,
638
+ button: 'left',
639
+ clickCount: 1
640
+ });
641
+ await sleep(200);
642
+ await this.mouse.move(to.x, to.y);
643
+ }
644
+ });
645
+ _define_property(this, "keyboard", {
646
+ type: async (text)=>{
647
+ const cdpKeyboard = new external_cdpInput_js_namespaceObject.CdpKeyboard({
648
+ send: this.sendCommandToDebugger.bind(this)
649
+ });
650
+ await cdpKeyboard.type(text, {
651
+ delay: 0
652
+ });
653
+ },
654
+ press: async (action)=>{
655
+ const cdpKeyboard = new external_cdpInput_js_namespaceObject.CdpKeyboard({
656
+ send: this.sendCommandToDebugger.bind(this)
657
+ });
658
+ const keys = Array.isArray(action) ? action : [
659
+ action
660
+ ];
661
+ for (const k of keys){
662
+ const commands = k.command ? [
663
+ k.command
664
+ ] : [];
665
+ await cdpKeyboard.down(k.key, {
666
+ commands
667
+ });
668
+ }
669
+ for (const k of [
670
+ ...keys
671
+ ].reverse())await cdpKeyboard.up(k.key);
672
+ }
673
+ });
674
+ this.forceSameTabNavigation = forceSameTabNavigation;
675
+ }
676
+ }
677
+ exports["default"] = __webpack_exports__["default"];
678
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
679
+ "default"
680
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
681
+ Object.defineProperty(exports, '__esModule', {
682
+ value: true
683
+ });
684
+
685
+ //# sourceMappingURL=page.js.map