@godscene/web 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.
Files changed (129) hide show
  1. package/README.md +7 -0
  2. package/bin/midscene-playground +3 -0
  3. package/bin/midscene-web +2 -0
  4. package/dist/es/bin.mjs +14 -0
  5. package/dist/es/bridge-mode/agent-cli-side.mjs +135 -0
  6. package/dist/es/bridge-mode/browser.mjs +2 -0
  7. package/dist/es/bridge-mode/common.mjs +41 -0
  8. package/dist/es/bridge-mode/index.mjs +4 -0
  9. package/dist/es/bridge-mode/io-client.mjs +99 -0
  10. package/dist/es/bridge-mode/io-server.mjs +218 -0
  11. package/dist/es/bridge-mode/page-browser-side.mjs +119 -0
  12. package/dist/es/cdp-proxy-constants.mjs +7 -0
  13. package/dist/es/cdp-proxy-manager.mjs +217 -0
  14. package/dist/es/cdp-proxy.mjs +151 -0
  15. package/dist/es/cdp-target-store.mjs +26 -0
  16. package/dist/es/chrome-extension/agent.mjs +8 -0
  17. package/dist/es/chrome-extension/cdpInput.mjs +172 -0
  18. package/dist/es/chrome-extension/cdpInput.mjs.LICENSE.txt +5 -0
  19. package/dist/es/chrome-extension/dynamic-scripts.mjs +36 -0
  20. package/dist/es/chrome-extension/index.mjs +5 -0
  21. package/dist/es/chrome-extension/page.mjs +733 -0
  22. package/dist/es/cli-options.mjs +97 -0
  23. package/dist/es/cli.mjs +26 -0
  24. package/dist/es/common/cache-helper.mjs +26 -0
  25. package/dist/es/common/viewport.mjs +36 -0
  26. package/dist/es/index.mjs +8 -0
  27. package/dist/es/mcp-server.mjs +33 -0
  28. package/dist/es/mcp-tools-cdp.mjs +164 -0
  29. package/dist/es/mcp-tools-puppeteer.mjs +246 -0
  30. package/dist/es/mcp-tools.mjs +81 -0
  31. package/dist/es/platform.mjs +37 -0
  32. package/dist/es/playwright/ai-fixture.mjs +364 -0
  33. package/dist/es/playwright/index.mjs +36 -0
  34. package/dist/es/playwright/page.mjs +42 -0
  35. package/dist/es/playwright/reporter/index.mjs +178 -0
  36. package/dist/es/puppeteer/agent-launcher.mjs +172 -0
  37. package/dist/es/puppeteer/base-page.mjs +830 -0
  38. package/dist/es/puppeteer/index.mjs +35 -0
  39. package/dist/es/puppeteer/page.mjs +7 -0
  40. package/dist/es/static/index.mjs +3 -0
  41. package/dist/es/static/static-agent.mjs +10 -0
  42. package/dist/es/static/static-page.mjs +123 -0
  43. package/dist/es/utils.mjs +6 -0
  44. package/dist/es/web-element.mjs +57 -0
  45. package/dist/es/web-page.mjs +272 -0
  46. package/dist/lib/bin.js +20 -0
  47. package/dist/lib/bridge-mode/agent-cli-side.js +172 -0
  48. package/dist/lib/bridge-mode/browser.js +36 -0
  49. package/dist/lib/bridge-mode/common.js +105 -0
  50. package/dist/lib/bridge-mode/index.js +44 -0
  51. package/dist/lib/bridge-mode/io-client.js +133 -0
  52. package/dist/lib/bridge-mode/io-server.js +255 -0
  53. package/dist/lib/bridge-mode/page-browser-side.js +163 -0
  54. package/dist/lib/cdp-proxy-constants.js +50 -0
  55. package/dist/lib/cdp-proxy-manager.js +273 -0
  56. package/dist/lib/cdp-proxy.js +179 -0
  57. package/dist/lib/cdp-target-store.js +66 -0
  58. package/dist/lib/chrome-extension/agent.js +42 -0
  59. package/dist/lib/chrome-extension/cdpInput.js +206 -0
  60. package/dist/lib/chrome-extension/cdpInput.js.LICENSE.txt +5 -0
  61. package/dist/lib/chrome-extension/dynamic-scripts.js +86 -0
  62. package/dist/lib/chrome-extension/index.js +58 -0
  63. package/dist/lib/chrome-extension/page.js +767 -0
  64. package/dist/lib/cli-options.js +131 -0
  65. package/dist/lib/cli.js +54 -0
  66. package/dist/lib/common/cache-helper.js +66 -0
  67. package/dist/lib/common/viewport.js +88 -0
  68. package/dist/lib/index.js +66 -0
  69. package/dist/lib/mcp-server.js +73 -0
  70. package/dist/lib/mcp-tools-cdp.js +208 -0
  71. package/dist/lib/mcp-tools-puppeteer.js +296 -0
  72. package/dist/lib/mcp-tools.js +115 -0
  73. package/dist/lib/platform.js +71 -0
  74. package/dist/lib/playwright/ai-fixture.js +401 -0
  75. package/dist/lib/playwright/index.js +89 -0
  76. package/dist/lib/playwright/page.js +76 -0
  77. package/dist/lib/playwright/reporter/index.js +212 -0
  78. package/dist/lib/puppeteer/agent-launcher.js +240 -0
  79. package/dist/lib/puppeteer/base-page.js +876 -0
  80. package/dist/lib/puppeteer/index.js +85 -0
  81. package/dist/lib/puppeteer/page.js +41 -0
  82. package/dist/lib/static/index.js +50 -0
  83. package/dist/lib/static/static-agent.js +44 -0
  84. package/dist/lib/static/static-page.js +157 -0
  85. package/dist/lib/utils.js +38 -0
  86. package/dist/lib/web-element.js +94 -0
  87. package/dist/lib/web-page.js +322 -0
  88. package/dist/types/bin.d.ts +1 -0
  89. package/dist/types/bridge-mode/agent-cli-side.d.ts +49 -0
  90. package/dist/types/bridge-mode/browser.d.ts +2 -0
  91. package/dist/types/bridge-mode/common.d.ts +74 -0
  92. package/dist/types/bridge-mode/index.d.ts +4 -0
  93. package/dist/types/bridge-mode/io-client.d.ts +10 -0
  94. package/dist/types/bridge-mode/io-server.d.ts +27 -0
  95. package/dist/types/bridge-mode/page-browser-side.d.ts +21 -0
  96. package/dist/types/cdp-proxy-constants.d.ts +4 -0
  97. package/dist/types/cdp-proxy-manager.d.ts +53 -0
  98. package/dist/types/cdp-proxy.d.ts +37 -0
  99. package/dist/types/cdp-target-store.d.ts +26 -0
  100. package/dist/types/chrome-extension/agent.d.ts +4 -0
  101. package/dist/types/chrome-extension/cdpInput.d.ts +52 -0
  102. package/dist/types/chrome-extension/dynamic-scripts.d.ts +3 -0
  103. package/dist/types/chrome-extension/index.d.ts +5 -0
  104. package/dist/types/chrome-extension/page.d.ts +120 -0
  105. package/dist/types/cli-options.d.ts +8 -0
  106. package/dist/types/cli.d.ts +1 -0
  107. package/dist/types/common/cache-helper.d.ts +20 -0
  108. package/dist/types/common/viewport.d.ts +17 -0
  109. package/dist/types/index.d.ts +9 -0
  110. package/dist/types/mcp-server.d.ts +26 -0
  111. package/dist/types/mcp-tools-cdp.d.ts +23 -0
  112. package/dist/types/mcp-tools-puppeteer.d.ts +23 -0
  113. package/dist/types/mcp-tools.d.ts +14 -0
  114. package/dist/types/platform.d.ts +10 -0
  115. package/dist/types/playwright/ai-fixture.d.ts +133 -0
  116. package/dist/types/playwright/index.d.ts +13 -0
  117. package/dist/types/playwright/page.d.ts +11 -0
  118. package/dist/types/playwright/reporter/index.d.ts +28 -0
  119. package/dist/types/puppeteer/agent-launcher.d.ts +59 -0
  120. package/dist/types/puppeteer/base-page.d.ts +123 -0
  121. package/dist/types/puppeteer/index.d.ts +11 -0
  122. package/dist/types/puppeteer/page.d.ts +6 -0
  123. package/dist/types/static/index.d.ts +2 -0
  124. package/dist/types/static/static-agent.d.ts +5 -0
  125. package/dist/types/static/static-page.d.ts +46 -0
  126. package/dist/types/utils.d.ts +6 -0
  127. package/dist/types/web-element.d.ts +48 -0
  128. package/dist/types/web-page.d.ts +69 -0
  129. package/package.json +173 -0
@@ -0,0 +1,767 @@
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 ("u" > 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("@godscene/shared/extractor");
31
+ const img_namespaceObject = require("@godscene/shared/img");
32
+ const logger_namespaceObject = require("@godscene/shared/logger");
33
+ const utils_namespaceObject = require("@godscene/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 = async ()=>{
195
+ const tree = await 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
+ awaitPromise: true
208
+ });
209
+ if (!returnValue.result.value) {
210
+ const errorDescription = returnValue.exceptionDetails?.exception?.description || '';
211
+ if (!errorDescription) console.error('returnValue from cdp', returnValue);
212
+ throw new Error(`Failed to get page content from page, error: ${errorDescription}`);
213
+ }
214
+ return returnValue.result.value;
215
+ }
216
+ async evaluateJavaScript(script) {
217
+ return this.sendCommandToDebugger('Runtime.evaluate', {
218
+ expression: script,
219
+ awaitPromise: true
220
+ });
221
+ }
222
+ async beforeInvokeAction() {
223
+ try {
224
+ await this.waitUntilNetworkIdle();
225
+ } catch (error) {}
226
+ }
227
+ async waitUntilNetworkIdle() {
228
+ const timeout = 10000;
229
+ const startTime = Date.now();
230
+ let lastReadyState = '';
231
+ while(Date.now() - startTime < timeout){
232
+ const result = await this.sendCommandToDebugger('Runtime.evaluate', {
233
+ expression: 'document.readyState'
234
+ });
235
+ lastReadyState = result.result.value;
236
+ if ('complete' === lastReadyState) return void await new Promise((resolve)=>setTimeout(resolve, 300));
237
+ await new Promise((resolve)=>setTimeout(resolve, 300));
238
+ }
239
+ throw new Error(`Failed to wait until network idle, last readyState: ${lastReadyState}`);
240
+ }
241
+ async getElementsInfo() {
242
+ const tree = await this.getElementsNodeTree();
243
+ return (0, extractor_namespaceObject.treeToList)(tree);
244
+ }
245
+ async getXpathsByPoint(point, isOrderSensitive = false) {
246
+ const script = await (0, external_dynamic_scripts_js_namespaceObject.getHtmlElementScript)();
247
+ await this.sendCommandToDebugger('Runtime.evaluate', {
248
+ expression: script
249
+ });
250
+ const result = await this.sendCommandToDebugger('Runtime.evaluate', {
251
+ expression: `window.midscene_element_inspector.getXpathsByPoint({left: ${point.left}, top: ${point.top}}, ${isOrderSensitive})`,
252
+ returnByValue: true
253
+ });
254
+ return result.result.value;
255
+ }
256
+ async getElementFromPoint(args) {
257
+ const tabId = await this.getTabIdOrConnectToCurrentTab();
258
+ await chrome.scripting.executeScript({
259
+ files: [
260
+ "./scripts/htmlElement.js"
261
+ ],
262
+ world: 'MAIN',
263
+ target: {
264
+ allFrames: true,
265
+ tabId
266
+ }
267
+ });
268
+ const result = await this.sendCommandToDebugger('Runtime.evaluate', {
269
+ expression: `window.midscene_element_inspector.webGetElementFromPoint({x:${args.x},y:${args.y}})`,
270
+ returnByValue: true,
271
+ awaitPromise: true
272
+ });
273
+ return result.result.value;
274
+ }
275
+ async getElementInfoByXpath(xpath) {
276
+ const script = await (0, external_dynamic_scripts_js_namespaceObject.getHtmlElementScript)();
277
+ await this.sendCommandToDebugger('Runtime.evaluate', {
278
+ expression: script
279
+ });
280
+ const result = await this.sendCommandToDebugger('Runtime.evaluate', {
281
+ expression: `window.midscene_element_inspector.getElementInfoByXpath(${JSON.stringify(xpath)})`,
282
+ returnByValue: true
283
+ });
284
+ return result.result.value;
285
+ }
286
+ async cacheFeatureForPoint(center, options) {
287
+ const point = {
288
+ left: center[0],
289
+ top: center[1]
290
+ };
291
+ try {
292
+ const isOrderSensitive = await (0, cache_helper_js_namespaceObject.judgeOrderSensitive)(options, debug);
293
+ const xpaths = await this.getXpathsByPoint(point, isOrderSensitive);
294
+ return {
295
+ xpaths: (0, cache_helper_js_namespaceObject.sanitizeXpaths)(xpaths)
296
+ };
297
+ } catch (error) {
298
+ debug('cacheFeatureForPoint failed: %O', error);
299
+ return {
300
+ xpaths: []
301
+ };
302
+ }
303
+ }
304
+ async rectMatchesCacheFeature(feature) {
305
+ const xpaths = (0, cache_helper_js_namespaceObject.sanitizeXpaths)(feature.xpaths);
306
+ for (const xpath of xpaths)try {
307
+ const elementInfo = await this.getElementInfoByXpath(xpath);
308
+ if (elementInfo?.rect) return (0, cache_helper_js_namespaceObject.buildRectFromElementInfo)(elementInfo);
309
+ } catch (error) {
310
+ debug('rectMatchesCacheFeature failed for xpath %s: %O', xpath, error);
311
+ }
312
+ throw new Error(`No matching element rect found for cache feature (tried ${xpaths.length} xpath(s))`);
313
+ }
314
+ async getElementsNodeTree() {
315
+ await this.hideMousePointer();
316
+ const content = await this.getPageContentByCDP();
317
+ if (content?.size) this.viewportSize = content.size;
318
+ return content?.tree || {
319
+ node: null,
320
+ children: []
321
+ };
322
+ }
323
+ async size() {
324
+ if (this.viewportSize) return this.viewportSize;
325
+ const result = await this.sendCommandToDebugger('Runtime.evaluate', {
326
+ expression: '({width: window.innerWidth, height: window.innerHeight})',
327
+ returnByValue: true
328
+ });
329
+ const sizeInfo = result.result.value;
330
+ console.log('sizeInfo', sizeInfo);
331
+ this.viewportSize = sizeInfo;
332
+ return sizeInfo;
333
+ }
334
+ async screenshotBase64() {
335
+ await this.hideMousePointer();
336
+ const format = 'jpeg';
337
+ const base64 = await this.sendCommandToDebugger('Page.captureScreenshot', {
338
+ format,
339
+ quality: 90
340
+ });
341
+ return (0, img_namespaceObject.createImgBase64ByFormat)(format, base64.data);
342
+ }
343
+ async url() {
344
+ const tabId = await this.getTabIdOrConnectToCurrentTab();
345
+ const url = await chrome.tabs.get(tabId).then((tab)=>tab.url);
346
+ return url || '';
347
+ }
348
+ async navigate(url) {
349
+ const tabId = await this.getTabIdOrConnectToCurrentTab();
350
+ await chrome.tabs.update(tabId, {
351
+ url
352
+ });
353
+ await this.waitUntilNetworkIdle();
354
+ }
355
+ async reload() {
356
+ const tabId = await this.getTabIdOrConnectToCurrentTab();
357
+ await chrome.tabs.reload(tabId);
358
+ await this.waitUntilNetworkIdle();
359
+ }
360
+ async goBack() {
361
+ const tabId = await this.getTabIdOrConnectToCurrentTab();
362
+ await chrome.tabs.goBack(tabId);
363
+ await this.waitUntilNetworkIdle();
364
+ }
365
+ async goForward() {
366
+ const tabId = await this.getTabIdOrConnectToCurrentTab();
367
+ await chrome.tabs.goForward(tabId);
368
+ await this.waitUntilNetworkIdle();
369
+ }
370
+ async stopLoading() {
371
+ await this.sendCommandToDebugger('Page.stopLoading', {});
372
+ }
373
+ async navigationState() {
374
+ const tabId = await this.getTabIdOrConnectToCurrentTab();
375
+ const tab = await chrome.tabs.get(tabId);
376
+ return {
377
+ isLoading: 'loading' === tab.status
378
+ };
379
+ }
380
+ async scrollUntilTop(startingPoint) {
381
+ if (startingPoint) await this.mouse.move(startingPoint.left, startingPoint.top);
382
+ return this.mouse.wheel(0, -9999999);
383
+ }
384
+ async scrollUntilBottom(startingPoint) {
385
+ if (startingPoint) await this.mouse.move(startingPoint.left, startingPoint.top);
386
+ return this.mouse.wheel(0, 9999999);
387
+ }
388
+ async scrollUntilLeft(startingPoint) {
389
+ if (startingPoint) await this.mouse.move(startingPoint.left, startingPoint.top);
390
+ return this.mouse.wheel(-9999999, 0);
391
+ }
392
+ async scrollUntilRight(startingPoint) {
393
+ if (startingPoint) await this.mouse.move(startingPoint.left, startingPoint.top);
394
+ return this.mouse.wheel(9999999, 0);
395
+ }
396
+ async scrollUp(distance, startingPoint) {
397
+ const { height } = await this.size();
398
+ const scrollDistance = distance || 0.7 * height;
399
+ return this.mouse.wheel(0, -scrollDistance, startingPoint?.left, startingPoint?.top);
400
+ }
401
+ async scrollDown(distance, startingPoint) {
402
+ const { height } = await this.size();
403
+ const scrollDistance = distance || 0.7 * height;
404
+ return this.mouse.wheel(0, scrollDistance, startingPoint?.left, startingPoint?.top);
405
+ }
406
+ async scrollLeft(distance, startingPoint) {
407
+ const { width } = await this.size();
408
+ const scrollDistance = distance || 0.7 * width;
409
+ return this.mouse.wheel(-scrollDistance, 0, startingPoint?.left, startingPoint?.top);
410
+ }
411
+ async scrollRight(distance, startingPoint) {
412
+ const { width } = await this.size();
413
+ const scrollDistance = distance || 0.7 * width;
414
+ return this.mouse.wheel(scrollDistance, 0, startingPoint?.left, startingPoint?.top);
415
+ }
416
+ async clearInput(element) {
417
+ if (!element) return void console.warn('No element to clear input');
418
+ await this.mouse.click(element.center[0], element.center[1]);
419
+ await this.sendCommandToDebugger('Input.dispatchKeyEvent', {
420
+ type: 'keyDown',
421
+ commands: [
422
+ 'selectAll'
423
+ ]
424
+ });
425
+ await this.sendCommandToDebugger('Input.dispatchKeyEvent', {
426
+ type: 'keyUp',
427
+ commands: [
428
+ 'selectAll'
429
+ ]
430
+ });
431
+ await sleep(100);
432
+ await this.keyboard.press({
433
+ key: 'Backspace'
434
+ });
435
+ }
436
+ async destroy() {
437
+ this.destroyed = true;
438
+ const tabIdToDetach = this.activeTabId;
439
+ this.activeTabId = null;
440
+ if (tabIdToDetach) await this.detachDebugger(tabIdToDetach);
441
+ }
442
+ async longPress(x, y, duration) {
443
+ duration = duration || 500;
444
+ const LONG_PRESS_THRESHOLD = 600;
445
+ const MIN_PRESS_THRESHOLD = 300;
446
+ if (duration > LONG_PRESS_THRESHOLD) duration = LONG_PRESS_THRESHOLD;
447
+ if (duration < MIN_PRESS_THRESHOLD) duration = MIN_PRESS_THRESHOLD;
448
+ await this.mouse.move(x, y);
449
+ if (null === this.isMobileEmulation) {
450
+ const result = await this.sendCommandToDebugger('Runtime.evaluate', {
451
+ expression: `(() => {
452
+ return /Android|iPhone|iPad|iPod|Mobile/i.test(navigator.userAgent);
453
+ })()`,
454
+ returnByValue: true
455
+ });
456
+ this.isMobileEmulation = result?.result?.value;
457
+ }
458
+ if (this.isMobileEmulation) {
459
+ const touchPoints = [
460
+ {
461
+ x: Math.round(x),
462
+ y: Math.round(y)
463
+ }
464
+ ];
465
+ await this.sendCommandToDebugger('Input.dispatchTouchEvent', {
466
+ type: 'touchStart',
467
+ touchPoints,
468
+ modifiers: 0
469
+ });
470
+ await new Promise((res)=>setTimeout(res, duration));
471
+ await this.sendCommandToDebugger('Input.dispatchTouchEvent', {
472
+ type: 'touchEnd',
473
+ touchPoints: [],
474
+ modifiers: 0
475
+ });
476
+ } else {
477
+ await this.sendCommandToDebugger('Input.dispatchMouseEvent', {
478
+ type: 'mousePressed',
479
+ x,
480
+ y,
481
+ button: 'left',
482
+ clickCount: 1
483
+ });
484
+ await new Promise((res)=>setTimeout(res, duration));
485
+ await this.sendCommandToDebugger('Input.dispatchMouseEvent', {
486
+ type: 'mouseReleased',
487
+ x,
488
+ y,
489
+ button: 'left',
490
+ clickCount: 1
491
+ });
492
+ }
493
+ this.latestMouseX = x;
494
+ this.latestMouseY = y;
495
+ }
496
+ async swipe(from, to, duration) {
497
+ const LONG_PRESS_THRESHOLD = 500;
498
+ const MIN_PRESS_THRESHOLD = 150;
499
+ duration = duration || 300;
500
+ if (duration < MIN_PRESS_THRESHOLD) duration = MIN_PRESS_THRESHOLD;
501
+ if (duration > LONG_PRESS_THRESHOLD) duration = LONG_PRESS_THRESHOLD;
502
+ if (null === this.isMobileEmulation) {
503
+ const result = await this.sendCommandToDebugger('Runtime.evaluate', {
504
+ expression: `(() => {
505
+ return /Android|iPhone|iPad|iPod|Mobile/i.test(navigator.userAgent);
506
+ })()`,
507
+ returnByValue: true
508
+ });
509
+ this.isMobileEmulation = result?.result?.value;
510
+ }
511
+ const steps = 30;
512
+ const delay = duration / steps;
513
+ if (this.isMobileEmulation) {
514
+ await this.sendCommandToDebugger('Input.dispatchTouchEvent', {
515
+ type: 'touchStart',
516
+ touchPoints: [
517
+ {
518
+ x: Math.round(from.x),
519
+ y: Math.round(from.y)
520
+ }
521
+ ],
522
+ modifiers: 0
523
+ });
524
+ for(let i = 1; i <= steps; i++){
525
+ const x = from.x + (to.x - from.x) * (i / steps);
526
+ const y = from.y + (to.y - from.y) * (i / steps);
527
+ await this.sendCommandToDebugger('Input.dispatchTouchEvent', {
528
+ type: 'touchMove',
529
+ touchPoints: [
530
+ {
531
+ x: Math.round(x),
532
+ y: Math.round(y)
533
+ }
534
+ ],
535
+ modifiers: 0
536
+ });
537
+ await new Promise((res)=>setTimeout(res, delay));
538
+ }
539
+ await this.sendCommandToDebugger('Input.dispatchTouchEvent', {
540
+ type: 'touchEnd',
541
+ touchPoints: [],
542
+ modifiers: 0
543
+ });
544
+ } else {
545
+ await this.mouse.move(from.x, from.y);
546
+ await this.sendCommandToDebugger('Input.dispatchMouseEvent', {
547
+ type: 'mousePressed',
548
+ x: from.x,
549
+ y: from.y,
550
+ button: 'left',
551
+ clickCount: 1
552
+ });
553
+ for(let i = 1; i <= steps; i++){
554
+ const x = from.x + (to.x - from.x) * (i / steps);
555
+ const y = from.y + (to.y - from.y) * (i / steps);
556
+ await this.mouse.move(x, y);
557
+ await new Promise((res)=>setTimeout(res, delay));
558
+ }
559
+ await this.sendCommandToDebugger('Input.dispatchMouseEvent', {
560
+ type: 'mouseReleased',
561
+ x: to.x,
562
+ y: to.y,
563
+ button: 'left',
564
+ clickCount: 1
565
+ });
566
+ }
567
+ this.latestMouseX = to.x;
568
+ this.latestMouseY = to.y;
569
+ }
570
+ async pinch(centerX, centerY, startDistance, endDistance, duration = 500) {
571
+ const steps = 30;
572
+ const delay = duration / steps;
573
+ const halfStart = startDistance / 2;
574
+ const halfEnd = endDistance / 2;
575
+ await this.sendCommandToDebugger('Input.dispatchTouchEvent', {
576
+ type: 'touchStart',
577
+ touchPoints: [
578
+ {
579
+ x: Math.round(centerX),
580
+ y: Math.round(centerY - halfStart),
581
+ id: 0
582
+ },
583
+ {
584
+ x: Math.round(centerX),
585
+ y: Math.round(centerY + halfStart),
586
+ id: 1
587
+ }
588
+ ],
589
+ modifiers: 0
590
+ });
591
+ for(let i = 1; i <= steps; i++){
592
+ const progress = i / steps;
593
+ const currentHalf = halfStart + (halfEnd - halfStart) * progress;
594
+ await this.sendCommandToDebugger('Input.dispatchTouchEvent', {
595
+ type: 'touchMove',
596
+ touchPoints: [
597
+ {
598
+ x: Math.round(centerX),
599
+ y: Math.round(centerY - currentHalf),
600
+ id: 0
601
+ },
602
+ {
603
+ x: Math.round(centerX),
604
+ y: Math.round(centerY + currentHalf),
605
+ id: 1
606
+ }
607
+ ],
608
+ modifiers: 0
609
+ });
610
+ await new Promise((res)=>setTimeout(res, delay));
611
+ }
612
+ await this.sendCommandToDebugger('Input.dispatchTouchEvent', {
613
+ type: 'touchEnd',
614
+ touchPoints: [],
615
+ modifiers: 0
616
+ });
617
+ }
618
+ constructor(forceSameTabNavigation){
619
+ _define_property(this, "interfaceType", 'chrome-extension-proxy');
620
+ _define_property(this, "forceSameTabNavigation", void 0);
621
+ _define_property(this, "viewportSize", void 0);
622
+ _define_property(this, "activeTabId", null);
623
+ _define_property(this, "destroyed", false);
624
+ _define_property(this, "isMobileEmulation", null);
625
+ _define_property(this, "_continueWhenFailedToAttachDebugger", false);
626
+ _define_property(this, "latestMouseX", 100);
627
+ _define_property(this, "latestMouseY", 100);
628
+ _define_property(this, "mouse", {
629
+ click: async (x, y, options)=>{
630
+ const { button = 'left', count = 1 } = options || {};
631
+ await this.mouse.move(x, y);
632
+ if (null === this.isMobileEmulation) {
633
+ const result = await this.sendCommandToDebugger('Runtime.evaluate', {
634
+ expression: `(() => {
635
+ return /Android|iPhone|iPad|iPod|Mobile/i.test(navigator.userAgent);
636
+ })()`,
637
+ returnByValue: true
638
+ });
639
+ this.isMobileEmulation = result?.result?.value;
640
+ }
641
+ if (this.isMobileEmulation && 'left' === button) {
642
+ const touchPoints = [
643
+ {
644
+ x: Math.round(x),
645
+ y: Math.round(y)
646
+ }
647
+ ];
648
+ await this.sendCommandToDebugger('Input.dispatchTouchEvent', {
649
+ type: 'touchStart',
650
+ touchPoints,
651
+ modifiers: 0
652
+ });
653
+ await this.sendCommandToDebugger('Input.dispatchTouchEvent', {
654
+ type: 'touchEnd',
655
+ touchPoints: [],
656
+ modifiers: 0
657
+ });
658
+ } else for(let i = 0; i < count; i++){
659
+ const clickCount = i + 1;
660
+ await this.sendCommandToDebugger('Input.dispatchMouseEvent', {
661
+ type: 'mousePressed',
662
+ x,
663
+ y,
664
+ button,
665
+ clickCount
666
+ });
667
+ await this.sendCommandToDebugger('Input.dispatchMouseEvent', {
668
+ type: 'mouseReleased',
669
+ x,
670
+ y,
671
+ button,
672
+ clickCount
673
+ });
674
+ if (i < count - 1) await sleep(50);
675
+ }
676
+ },
677
+ wheel: async (deltaX, deltaY, startX, startY)=>{
678
+ const finalX = startX || this.latestMouseX;
679
+ const finalY = startY || this.latestMouseY;
680
+ await this.showMousePointer(finalX, finalY);
681
+ await this.sendCommandToDebugger('Input.dispatchMouseEvent', {
682
+ type: 'mouseWheel',
683
+ x: finalX,
684
+ y: finalY,
685
+ deltaX,
686
+ deltaY
687
+ });
688
+ this.latestMouseX = finalX;
689
+ this.latestMouseY = finalY;
690
+ },
691
+ move: async (x, y)=>{
692
+ await this.showMousePointer(x, y);
693
+ await this.sendCommandToDebugger('Input.dispatchMouseEvent', {
694
+ type: 'mouseMoved',
695
+ x,
696
+ y
697
+ });
698
+ this.latestMouseX = x;
699
+ this.latestMouseY = y;
700
+ },
701
+ drag: async (from, to)=>{
702
+ await this.mouse.move(from.x, from.y);
703
+ await sleep(200);
704
+ await this.sendCommandToDebugger('Input.dispatchMouseEvent', {
705
+ type: 'mousePressed',
706
+ x: from.x,
707
+ y: from.y,
708
+ button: 'left',
709
+ clickCount: 1
710
+ });
711
+ await sleep(300);
712
+ await this.sendCommandToDebugger('Input.dispatchMouseEvent', {
713
+ type: 'mouseMoved',
714
+ x: to.x,
715
+ y: to.y
716
+ });
717
+ await sleep(500);
718
+ await this.sendCommandToDebugger('Input.dispatchMouseEvent', {
719
+ type: 'mouseReleased',
720
+ x: to.x,
721
+ y: to.y,
722
+ button: 'left',
723
+ clickCount: 1
724
+ });
725
+ await sleep(200);
726
+ await this.mouse.move(to.x, to.y);
727
+ }
728
+ });
729
+ _define_property(this, "keyboard", {
730
+ type: async (text)=>{
731
+ const cdpKeyboard = new external_cdpInput_js_namespaceObject.CdpKeyboard({
732
+ send: this.sendCommandToDebugger.bind(this)
733
+ });
734
+ await cdpKeyboard.type(text, {
735
+ delay: 0
736
+ });
737
+ },
738
+ press: async (action)=>{
739
+ const cdpKeyboard = new external_cdpInput_js_namespaceObject.CdpKeyboard({
740
+ send: this.sendCommandToDebugger.bind(this)
741
+ });
742
+ const keys = Array.isArray(action) ? action : [
743
+ action
744
+ ];
745
+ for (const k of keys){
746
+ const commands = k.command ? [
747
+ k.command
748
+ ] : [];
749
+ await cdpKeyboard.down(k.key, {
750
+ commands
751
+ });
752
+ }
753
+ for (const k of [
754
+ ...keys
755
+ ].reverse())await cdpKeyboard.up(k.key);
756
+ }
757
+ });
758
+ this.forceSameTabNavigation = forceSameTabNavigation;
759
+ }
760
+ }
761
+ exports["default"] = __webpack_exports__["default"];
762
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
763
+ "default"
764
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
765
+ Object.defineProperty(exports, '__esModule', {
766
+ value: true
767
+ });