misoai-web 1.0.1 → 1.0.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 (82) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +8 -8
  3. package/bin/midscene-playground +2 -2
  4. package/package.json +23 -24
  5. package/dist/es/agent.js +0 -2451
  6. package/dist/es/agent.js.map +0 -1
  7. package/dist/es/bridge-mode-browser.js +0 -908
  8. package/dist/es/bridge-mode-browser.js.map +0 -1
  9. package/dist/es/bridge-mode.js +0 -2812
  10. package/dist/es/bridge-mode.js.map +0 -1
  11. package/dist/es/chrome-extension.js +0 -3152
  12. package/dist/es/chrome-extension.js.map +0 -1
  13. package/dist/es/index.js +0 -3052
  14. package/dist/es/index.js.map +0 -1
  15. package/dist/es/midscene-playground.js +0 -2781
  16. package/dist/es/midscene-playground.js.map +0 -1
  17. package/dist/es/midscene-server.js +0 -247
  18. package/dist/es/midscene-server.js.map +0 -1
  19. package/dist/es/playground.js +0 -2552
  20. package/dist/es/playground.js.map +0 -1
  21. package/dist/es/playwright-report.js +0 -120
  22. package/dist/es/playwright-report.js.map +0 -1
  23. package/dist/es/playwright.js +0 -2997
  24. package/dist/es/playwright.js.map +0 -1
  25. package/dist/es/puppeteer-agent-launcher.js +0 -2947
  26. package/dist/es/puppeteer-agent-launcher.js.map +0 -1
  27. package/dist/es/puppeteer.js +0 -2794
  28. package/dist/es/puppeteer.js.map +0 -1
  29. package/dist/es/ui-utils.js +0 -106
  30. package/dist/es/ui-utils.js.map +0 -1
  31. package/dist/es/utils.js +0 -197
  32. package/dist/es/utils.js.map +0 -1
  33. package/dist/es/yaml.js +0 -333
  34. package/dist/es/yaml.js.map +0 -1
  35. package/dist/lib/agent.js +0 -2466
  36. package/dist/lib/agent.js.map +0 -1
  37. package/dist/lib/bridge-mode-browser.js +0 -942
  38. package/dist/lib/bridge-mode-browser.js.map +0 -1
  39. package/dist/lib/bridge-mode.js +0 -2832
  40. package/dist/lib/bridge-mode.js.map +0 -1
  41. package/dist/lib/chrome-extension.js +0 -3169
  42. package/dist/lib/chrome-extension.js.map +0 -1
  43. package/dist/lib/index.js +0 -3071
  44. package/dist/lib/index.js.map +0 -1
  45. package/dist/lib/midscene-playground.js +0 -2785
  46. package/dist/lib/midscene-playground.js.map +0 -1
  47. package/dist/lib/midscene-server.js +0 -273
  48. package/dist/lib/midscene-server.js.map +0 -1
  49. package/dist/lib/playground.js +0 -2571
  50. package/dist/lib/playground.js.map +0 -1
  51. package/dist/lib/playwright-report.js +0 -148
  52. package/dist/lib/playwright-report.js.map +0 -1
  53. package/dist/lib/playwright.js +0 -3017
  54. package/dist/lib/playwright.js.map +0 -1
  55. package/dist/lib/puppeteer-agent-launcher.js +0 -2963
  56. package/dist/lib/puppeteer-agent-launcher.js.map +0 -1
  57. package/dist/lib/puppeteer.js +0 -2808
  58. package/dist/lib/puppeteer.js.map +0 -1
  59. package/dist/lib/ui-utils.js +0 -137
  60. package/dist/lib/ui-utils.js.map +0 -1
  61. package/dist/lib/utils.js +0 -235
  62. package/dist/lib/utils.js.map +0 -1
  63. package/dist/lib/yaml.js +0 -372
  64. package/dist/lib/yaml.js.map +0 -1
  65. package/dist/types/agent.d.ts +0 -254
  66. package/dist/types/bridge-mode-browser.d.ts +0 -9
  67. package/dist/types/bridge-mode.d.ts +0 -40
  68. package/dist/types/browser-d447695b.d.ts +0 -37
  69. package/dist/types/chrome-extension.d.ts +0 -18
  70. package/dist/types/index.d.ts +0 -16
  71. package/dist/types/midscene-playground.d.ts +0 -2
  72. package/dist/types/midscene-server.d.ts +0 -31
  73. package/dist/types/page-b8ada1f3.d.ts +0 -322
  74. package/dist/types/playground.d.ts +0 -17
  75. package/dist/types/playwright-report.d.ts +0 -11
  76. package/dist/types/playwright.d.ts +0 -87
  77. package/dist/types/puppeteer-agent-launcher.d.ts +0 -40
  78. package/dist/types/puppeteer.d.ts +0 -17
  79. package/dist/types/ui-utils.d.ts +0 -14
  80. package/dist/types/utils-badc824e.d.ts +0 -34
  81. package/dist/types/utils.d.ts +0 -8
  82. package/dist/types/yaml.d.ts +0 -15
@@ -1,942 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
- var __accessCheck = (obj, member, msg) => {
30
- if (!member.has(obj))
31
- throw TypeError("Cannot " + msg);
32
- };
33
- var __privateGet = (obj, member, getter) => {
34
- __accessCheck(obj, member, "read from private field");
35
- return getter ? getter.call(obj) : member.get(obj);
36
- };
37
- var __privateAdd = (obj, member, value) => {
38
- if (member.has(obj))
39
- throw TypeError("Cannot add the same private member more than once");
40
- member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
41
- };
42
- var __privateSet = (obj, member, value, setter) => {
43
- __accessCheck(obj, member, "write to private field");
44
- setter ? setter.call(obj, value) : member.set(obj, value);
45
- return value;
46
- };
47
- var __privateMethod = (obj, member, method) => {
48
- __accessCheck(obj, member, "access private method");
49
- return method;
50
- };
51
-
52
- // src/bridge-mode/browser.ts
53
- var browser_exports = {};
54
- __export(browser_exports, {
55
- ExtensionBridgePageBrowserSide: () => ExtensionBridgePageBrowserSide
56
- });
57
- module.exports = __toCommonJS(browser_exports);
58
-
59
- // src/bridge-mode/page-browser-side.ts
60
- var import_utils5 = require("misoai-shared/utils");
61
-
62
- // src/common/ui-utils.ts
63
- var limitOpenNewTabScript = `
64
- if (!window.__MIDSCENE_NEW_TAB_INTERCEPTOR_INITIALIZED__) {
65
- window.__MIDSCENE_NEW_TAB_INTERCEPTOR_INITIALIZED__ = true;
66
-
67
- // Intercept the window.open method (only once)
68
- window.open = function(url) {
69
- console.log('Blocked window.open:', url);
70
- window.location.href = url;
71
- return null;
72
- };
73
-
74
- // Block all a tag clicks with target="_blank" (only once)
75
- document.addEventListener('click', function(e) {
76
- const target = e.target.closest('a');
77
- if (target && target.target === '_blank') {
78
- e.preventDefault();
79
- console.log('Blocked new tab:', target.href);
80
- window.location.href = target.href;
81
- target.removeAttribute('target');
82
- }
83
- }, true);
84
- }
85
- `;
86
-
87
- // src/chrome-extension/page.ts
88
- var import_extractor = require("misoai-shared/extractor");
89
- var import_utils3 = require("misoai-shared/utils");
90
-
91
- // src/chrome-extension/cdpInput.ts
92
- var import_keyboard_layout = require("misoai-shared/keyboard-layout");
93
- var import_utils = require("misoai-shared/utils");
94
- var _pressedKeys, _client, _modifierBit, modifierBit_fn, _keyDescriptionForString, keyDescriptionForString_fn;
95
- var CdpKeyboard = class {
96
- constructor(client) {
97
- __privateAdd(this, _modifierBit);
98
- __privateAdd(this, _keyDescriptionForString);
99
- __privateAdd(this, _pressedKeys, /* @__PURE__ */ new Set());
100
- __privateAdd(this, _client, void 0);
101
- this._modifiers = 0;
102
- __privateSet(this, _client, client);
103
- }
104
- updateClient(client) {
105
- __privateSet(this, _client, client);
106
- }
107
- async down(key, options = {
108
- text: void 0,
109
- commands: []
110
- }) {
111
- const description = __privateMethod(this, _keyDescriptionForString, keyDescriptionForString_fn).call(this, key);
112
- const autoRepeat = __privateGet(this, _pressedKeys).has(description.code);
113
- __privateGet(this, _pressedKeys).add(description.code);
114
- this._modifiers |= __privateMethod(this, _modifierBit, modifierBit_fn).call(this, description.key);
115
- const text = options.text === void 0 ? description.text : options.text;
116
- await __privateGet(this, _client).send("Input.dispatchKeyEvent", {
117
- type: text ? "keyDown" : "rawKeyDown",
118
- modifiers: this._modifiers,
119
- windowsVirtualKeyCode: description.keyCode,
120
- code: description.code,
121
- key: description.key,
122
- text,
123
- unmodifiedText: text,
124
- autoRepeat,
125
- location: description.location,
126
- isKeypad: description.location === 3,
127
- commands: options.commands
128
- });
129
- }
130
- async up(key) {
131
- const description = __privateMethod(this, _keyDescriptionForString, keyDescriptionForString_fn).call(this, key);
132
- this._modifiers &= ~__privateMethod(this, _modifierBit, modifierBit_fn).call(this, description.key);
133
- __privateGet(this, _pressedKeys).delete(description.code);
134
- await __privateGet(this, _client).send("Input.dispatchKeyEvent", {
135
- type: "keyUp",
136
- modifiers: this._modifiers,
137
- key: description.key,
138
- windowsVirtualKeyCode: description.keyCode,
139
- code: description.code,
140
- location: description.location
141
- });
142
- }
143
- async sendCharacter(char) {
144
- await __privateGet(this, _client).send("Input.insertText", { text: char });
145
- }
146
- charIsKey(char) {
147
- return !!import_keyboard_layout._keyDefinitions[char];
148
- }
149
- async type(text, options = {}) {
150
- const delay = options.delay || void 0;
151
- for (const char of text) {
152
- if (this.charIsKey(char)) {
153
- await this.press(char, { delay });
154
- } else {
155
- if (delay) {
156
- await new Promise((f) => {
157
- return setTimeout(f, delay);
158
- });
159
- }
160
- await this.sendCharacter(char);
161
- }
162
- }
163
- }
164
- async press(key, options = {}) {
165
- const { delay = null } = options;
166
- const keys = Array.isArray(key) ? key : [key];
167
- for (const k of keys) {
168
- await this.down(k, options);
169
- }
170
- if (delay) {
171
- await new Promise((f) => {
172
- return setTimeout(f, options.delay);
173
- });
174
- }
175
- for (const k of [...keys].reverse()) {
176
- await this.up(k);
177
- }
178
- }
179
- };
180
- _pressedKeys = new WeakMap();
181
- _client = new WeakMap();
182
- _modifierBit = new WeakSet();
183
- modifierBit_fn = function(key) {
184
- if (key === "Alt") {
185
- return 1;
186
- }
187
- if (key === "Control") {
188
- return 2;
189
- }
190
- if (key === "Meta") {
191
- return 4;
192
- }
193
- if (key === "Shift") {
194
- return 8;
195
- }
196
- return 0;
197
- };
198
- _keyDescriptionForString = new WeakSet();
199
- keyDescriptionForString_fn = function(keyString) {
200
- const shift = this._modifiers & 8;
201
- const description = {
202
- key: "",
203
- keyCode: 0,
204
- code: "",
205
- text: "",
206
- location: 0
207
- };
208
- const definition = import_keyboard_layout._keyDefinitions[keyString];
209
- (0, import_utils.assert)(definition, `Unknown key: "${keyString}"`);
210
- if (definition.key) {
211
- description.key = definition.key;
212
- }
213
- if (shift && definition.shiftKey) {
214
- description.key = definition.shiftKey;
215
- }
216
- if (definition.keyCode) {
217
- description.keyCode = definition.keyCode;
218
- }
219
- if (shift && definition.shiftKeyCode) {
220
- description.keyCode = definition.shiftKeyCode;
221
- }
222
- if (definition.code) {
223
- description.code = definition.code;
224
- }
225
- if (definition.location) {
226
- description.location = definition.location;
227
- }
228
- if (description.key.length === 1) {
229
- description.text = description.key;
230
- }
231
- if (definition.text) {
232
- description.text = definition.text;
233
- }
234
- if (shift && definition.shiftText) {
235
- description.text = definition.shiftText;
236
- }
237
- if (this._modifiers & ~8) {
238
- description.text = "";
239
- }
240
- return description;
241
- };
242
-
243
- // src/chrome-extension/dynamic-scripts.ts
244
- var import_node_fs = __toESM(require("fs"));
245
- var import_utils2 = require("misoai-shared/utils");
246
- var scriptFileContentCache = null;
247
- var getHtmlElementScript = async () => {
248
- const scriptFileToRetrieve = chrome.runtime.getURL("scripts/htmlElement.js");
249
- if (scriptFileContentCache)
250
- return scriptFileContentCache;
251
- if (import_utils2.ifInBrowser) {
252
- const script = await fetch(scriptFileToRetrieve);
253
- scriptFileContentCache = await script.text();
254
- return scriptFileContentCache;
255
- }
256
- return import_node_fs.default.readFileSync(scriptFileToRetrieve, "utf8");
257
- };
258
- var waterFlowScriptFileContentCache = null;
259
- var injectWaterFlowAnimation = async () => {
260
- const waterFlowScriptFileToRetrieve = chrome.runtime.getURL(
261
- "scripts/water-flow.js"
262
- );
263
- if (waterFlowScriptFileContentCache)
264
- return waterFlowScriptFileContentCache;
265
- if (import_utils2.ifInBrowser) {
266
- const script = await fetch(waterFlowScriptFileToRetrieve);
267
- waterFlowScriptFileContentCache = await script.text();
268
- return waterFlowScriptFileContentCache;
269
- }
270
- return import_node_fs.default.readFileSync(waterFlowScriptFileToRetrieve, "utf8");
271
- };
272
- var stopWaterFlowScriptFileContentCache = null;
273
- var injectStopWaterFlowAnimation = async () => {
274
- const stopWaterFlowScriptFileToRetrieve = chrome.runtime.getURL(
275
- "scripts/stop-water-flow.js"
276
- );
277
- if (stopWaterFlowScriptFileContentCache)
278
- return stopWaterFlowScriptFileContentCache;
279
- if (import_utils2.ifInBrowser) {
280
- const script = await fetch(stopWaterFlowScriptFileToRetrieve);
281
- stopWaterFlowScriptFileContentCache = await script.text();
282
- return stopWaterFlowScriptFileContentCache;
283
- }
284
- return import_node_fs.default.readFileSync(stopWaterFlowScriptFileToRetrieve, "utf8");
285
- };
286
-
287
- // src/chrome-extension/page.ts
288
- function sleep(ms) {
289
- return new Promise((resolve) => setTimeout(resolve, ms));
290
- }
291
- var ChromeExtensionProxyPage = class {
292
- constructor(forceSameTabNavigation) {
293
- this.pageType = "chrome-extension-proxy";
294
- this.version = "1.0.0";
295
- this.activeTabId = null;
296
- this.tabIdOfDebuggerAttached = null;
297
- this.attachingDebugger = null;
298
- this.destroyed = false;
299
- this.latestMouseX = 100;
300
- this.latestMouseY = 100;
301
- this.mouse = {
302
- click: async (x, y) => {
303
- await this.mouse.move(x, y);
304
- await this.sendCommandToDebugger("Input.dispatchMouseEvent", {
305
- type: "mousePressed",
306
- x,
307
- y,
308
- button: "left",
309
- clickCount: 1
310
- });
311
- await this.sendCommandToDebugger("Input.dispatchMouseEvent", {
312
- type: "mouseReleased",
313
- x,
314
- y,
315
- button: "left",
316
- clickCount: 1
317
- });
318
- },
319
- wheel: async (deltaX, deltaY, startX, startY) => {
320
- const finalX = startX || this.latestMouseX;
321
- const finalY = startY || this.latestMouseY;
322
- await this.showMousePointer(finalX, finalY);
323
- await this.sendCommandToDebugger("Input.dispatchMouseEvent", {
324
- type: "mouseWheel",
325
- x: finalX,
326
- y: finalY,
327
- deltaX,
328
- deltaY
329
- });
330
- this.latestMouseX = finalX;
331
- this.latestMouseY = finalY;
332
- },
333
- move: async (x, y) => {
334
- await this.showMousePointer(x, y);
335
- await this.sendCommandToDebugger("Input.dispatchMouseEvent", {
336
- type: "mouseMoved",
337
- x,
338
- y
339
- });
340
- this.latestMouseX = x;
341
- this.latestMouseY = y;
342
- },
343
- drag: async (from, to) => {
344
- await this.mouse.move(from.x, from.y);
345
- await this.sendCommandToDebugger("Input.dispatchMouseEvent", {
346
- type: "mousePressed",
347
- x: from.x,
348
- y: from.y,
349
- button: "left",
350
- clickCount: 1
351
- });
352
- await this.mouse.move(to.x, to.y);
353
- await this.sendCommandToDebugger("Input.dispatchMouseEvent", {
354
- type: "mouseReleased",
355
- x: to.x,
356
- y: to.y,
357
- button: "left",
358
- clickCount: 1
359
- });
360
- }
361
- };
362
- this.keyboard = {
363
- type: async (text) => {
364
- const cdpKeyboard = new CdpKeyboard({
365
- send: this.sendCommandToDebugger.bind(this)
366
- });
367
- await cdpKeyboard.type(text, { delay: 0 });
368
- },
369
- press: async (action) => {
370
- const cdpKeyboard = new CdpKeyboard({
371
- send: this.sendCommandToDebugger.bind(this)
372
- });
373
- const keys = Array.isArray(action) ? action : [action];
374
- for (const k of keys) {
375
- const commands = k.command ? [k.command] : [];
376
- await cdpKeyboard.down(k.key, { commands });
377
- }
378
- for (const k of [...keys].reverse()) {
379
- await cdpKeyboard.up(k.key);
380
- }
381
- }
382
- };
383
- this.forceSameTabNavigation = forceSameTabNavigation;
384
- }
385
- async setActiveTabId(tabId) {
386
- if (this.activeTabId) {
387
- throw new Error(
388
- `Active tab id is already set, which is ${this.activeTabId}, cannot set it to ${tabId}`
389
- );
390
- }
391
- await chrome.tabs.update(tabId, { active: true });
392
- this.activeTabId = tabId;
393
- }
394
- async getActiveTabId() {
395
- return this.activeTabId;
396
- }
397
- /**
398
- * Get a list of current tabs
399
- * @returns {Promise<Array<{id: number, title: string, url: string}>>}
400
- */
401
- async getBrowserTabList() {
402
- const tabs = await chrome.tabs.query({ currentWindow: true });
403
- return tabs.map((tab) => ({
404
- id: `${tab.id}`,
405
- title: tab.title,
406
- url: tab.url,
407
- currentActiveTab: tab.active
408
- })).filter((tab) => tab.id && tab.title && tab.url);
409
- }
410
- async getTabIdOrConnectToCurrentTab() {
411
- if (this.activeTabId) {
412
- return this.activeTabId;
413
- }
414
- const tabId = await chrome.tabs.query({ active: true, currentWindow: true }).then((tabs) => tabs[0]?.id);
415
- this.activeTabId = tabId || 0;
416
- return this.activeTabId;
417
- }
418
- async attachDebugger() {
419
- (0, import_utils3.assert)(!this.destroyed, "Page is destroyed");
420
- if (this.attachingDebugger) {
421
- await this.attachingDebugger;
422
- return;
423
- }
424
- this.attachingDebugger = (async () => {
425
- const url = await this.url();
426
- let error = null;
427
- if (url.startsWith("chrome://")) {
428
- throw new Error(
429
- "Cannot attach debugger to chrome:// pages, please use Midscene in a normal page with http://, https:// or file://"
430
- );
431
- }
432
- try {
433
- const currentTabId = await this.getTabIdOrConnectToCurrentTab();
434
- if (this.tabIdOfDebuggerAttached === currentTabId) {
435
- return;
436
- }
437
- if (this.tabIdOfDebuggerAttached && this.tabIdOfDebuggerAttached !== currentTabId) {
438
- console.log(
439
- "detach the previous tab",
440
- this.tabIdOfDebuggerAttached,
441
- "->",
442
- currentTabId
443
- );
444
- try {
445
- await this.detachDebugger(this.tabIdOfDebuggerAttached);
446
- } catch (error2) {
447
- console.error("Failed to detach debugger", error2);
448
- }
449
- }
450
- console.log("attaching debugger", currentTabId);
451
- await chrome.debugger.attach({ tabId: currentTabId }, "1.3");
452
- await sleep(500);
453
- this.tabIdOfDebuggerAttached = currentTabId;
454
- await this.enableWaterFlowAnimation();
455
- } catch (e) {
456
- console.error("Failed to attach debugger", e);
457
- error = e;
458
- } finally {
459
- this.attachingDebugger = null;
460
- }
461
- if (error) {
462
- throw error;
463
- }
464
- })();
465
- await this.attachingDebugger;
466
- }
467
- async showMousePointer(x, y) {
468
- const pointerScript = `(() => {
469
- if(typeof window.midsceneWaterFlowAnimation !== 'undefined') {
470
- window.midsceneWaterFlowAnimation.enable();
471
- window.midsceneWaterFlowAnimation.showMousePointer(${x}, ${y});
472
- } else {
473
- console.log('midsceneWaterFlowAnimation is not defined');
474
- }
475
- })()`;
476
- await this.sendCommandToDebugger("Runtime.evaluate", {
477
- expression: `${pointerScript}`
478
- });
479
- }
480
- async hideMousePointer() {
481
- await this.sendCommandToDebugger("Runtime.evaluate", {
482
- expression: `(() => {
483
- if(typeof window.midsceneWaterFlowAnimation !== 'undefined') {
484
- window.midsceneWaterFlowAnimation.hideMousePointer();
485
- }
486
- })()`
487
- });
488
- }
489
- async detachDebugger(tabId) {
490
- const tabIdToDetach = tabId || this.tabIdOfDebuggerAttached;
491
- console.log("detaching debugger", tabIdToDetach);
492
- if (!tabIdToDetach) {
493
- console.warn("No tab id to detach");
494
- return;
495
- }
496
- try {
497
- await this.disableWaterFlowAnimation(tabIdToDetach);
498
- await sleep(200);
499
- } catch (error) {
500
- console.warn("Failed to disable water flow animation", error);
501
- }
502
- try {
503
- await chrome.debugger.detach({ tabId: tabIdToDetach });
504
- } catch (error) {
505
- console.warn("Failed to detach debugger", error);
506
- }
507
- this.tabIdOfDebuggerAttached = null;
508
- }
509
- async enableWaterFlowAnimation() {
510
- if (this.forceSameTabNavigation) {
511
- await chrome.debugger.sendCommand(
512
- { tabId: this.tabIdOfDebuggerAttached },
513
- "Runtime.evaluate",
514
- {
515
- expression: limitOpenNewTabScript
516
- }
517
- );
518
- }
519
- const script = await injectWaterFlowAnimation();
520
- await chrome.debugger.sendCommand(
521
- { tabId: this.tabIdOfDebuggerAttached },
522
- "Runtime.evaluate",
523
- {
524
- expression: script
525
- }
526
- );
527
- }
528
- async disableWaterFlowAnimation(tabId) {
529
- const script = await injectStopWaterFlowAnimation();
530
- await chrome.debugger.sendCommand({ tabId }, "Runtime.evaluate", {
531
- expression: script
532
- });
533
- }
534
- async sendCommandToDebugger(command, params) {
535
- await this.attachDebugger();
536
- (0, import_utils3.assert)(this.tabIdOfDebuggerAttached, "Debugger is not attached");
537
- this.enableWaterFlowAnimation();
538
- return await chrome.debugger.sendCommand(
539
- { tabId: this.tabIdOfDebuggerAttached },
540
- command,
541
- params
542
- );
543
- }
544
- async getPageContentByCDP() {
545
- const script = await getHtmlElementScript();
546
- await this.sendCommandToDebugger("Runtime.evaluate", {
547
- expression: script
548
- });
549
- const expression = () => {
550
- return {
551
- tree: window.midscene_element_inspector.webExtractNodeTree(),
552
- size: {
553
- width: document.documentElement.clientWidth,
554
- height: document.documentElement.clientHeight,
555
- dpr: window.devicePixelRatio
556
- }
557
- };
558
- };
559
- const returnValue = await this.sendCommandToDebugger("Runtime.evaluate", {
560
- expression: `(${expression.toString()})()`,
561
- returnByValue: true
562
- });
563
- if (!returnValue.result.value) {
564
- const errorDescription = returnValue.exceptionDetails?.exception?.description || "";
565
- if (!errorDescription) {
566
- console.error("returnValue from cdp", returnValue);
567
- }
568
- throw new Error(
569
- `Failed to get page content from page, error: ${errorDescription}`
570
- );
571
- }
572
- return returnValue.result.value;
573
- }
574
- async evaluateJavaScript(script) {
575
- return this.sendCommandToDebugger("Runtime.evaluate", {
576
- expression: script
577
- });
578
- }
579
- // current implementation is wait until domReadyState is complete
580
- async waitUntilNetworkIdle() {
581
- const timeout = 1e4;
582
- const startTime = Date.now();
583
- let lastReadyState = "";
584
- while (Date.now() - startTime < timeout) {
585
- const result = await this.sendCommandToDebugger("Runtime.evaluate", {
586
- expression: "document.readyState"
587
- });
588
- lastReadyState = result.result.value;
589
- if (lastReadyState === "complete") {
590
- await new Promise((resolve) => setTimeout(resolve, 300));
591
- return;
592
- }
593
- await new Promise((resolve) => setTimeout(resolve, 300));
594
- }
595
- throw new Error(
596
- `Failed to wait until network idle, last readyState: ${lastReadyState}`
597
- );
598
- }
599
- async getElementsInfo() {
600
- const tree = await this.getElementsNodeTree();
601
- return (0, import_extractor.treeToList)(tree);
602
- }
603
- async getElementsNodeTree() {
604
- await this.hideMousePointer();
605
- const content = await this.getPageContentByCDP();
606
- if (content?.size) {
607
- this.viewportSize = content.size;
608
- }
609
- return content?.tree || { node: null, children: [] };
610
- }
611
- async size() {
612
- if (this.viewportSize)
613
- return this.viewportSize;
614
- const content = await this.getPageContentByCDP();
615
- return content.size;
616
- }
617
- async screenshotBase64() {
618
- await this.hideMousePointer();
619
- const base64 = await this.sendCommandToDebugger("Page.captureScreenshot", {
620
- format: "jpeg",
621
- quality: 90
622
- });
623
- return `data:image/jpeg;base64,${base64.data}`;
624
- }
625
- async url() {
626
- const tabId = await this.getTabIdOrConnectToCurrentTab();
627
- const url = await chrome.tabs.get(tabId).then((tab) => tab.url);
628
- return url || "";
629
- }
630
- async scrollUntilTop(startingPoint) {
631
- if (startingPoint) {
632
- await this.mouse.move(startingPoint.left, startingPoint.top);
633
- }
634
- return this.mouse.wheel(0, -9999999);
635
- }
636
- async scrollUntilBottom(startingPoint) {
637
- if (startingPoint) {
638
- await this.mouse.move(startingPoint.left, startingPoint.top);
639
- }
640
- return this.mouse.wheel(0, 9999999);
641
- }
642
- async scrollUntilLeft(startingPoint) {
643
- if (startingPoint) {
644
- await this.mouse.move(startingPoint.left, startingPoint.top);
645
- }
646
- return this.mouse.wheel(-9999999, 0);
647
- }
648
- async scrollUntilRight(startingPoint) {
649
- if (startingPoint) {
650
- await this.mouse.move(startingPoint.left, startingPoint.top);
651
- }
652
- return this.mouse.wheel(9999999, 0);
653
- }
654
- async scrollUp(distance, startingPoint) {
655
- const { height } = await this.size();
656
- const scrollDistance = distance || height * 0.7;
657
- return this.mouse.wheel(
658
- 0,
659
- -scrollDistance,
660
- startingPoint?.left,
661
- startingPoint?.top
662
- );
663
- }
664
- async scrollDown(distance, startingPoint) {
665
- const { height } = await this.size();
666
- const scrollDistance = distance || height * 0.7;
667
- return this.mouse.wheel(
668
- 0,
669
- scrollDistance,
670
- startingPoint?.left,
671
- startingPoint?.top
672
- );
673
- }
674
- async scrollLeft(distance, startingPoint) {
675
- const { width } = await this.size();
676
- const scrollDistance = distance || width * 0.7;
677
- return this.mouse.wheel(
678
- -scrollDistance,
679
- 0,
680
- startingPoint?.left,
681
- startingPoint?.top
682
- );
683
- }
684
- async scrollRight(distance, startingPoint) {
685
- const { width } = await this.size();
686
- const scrollDistance = distance || width * 0.7;
687
- return this.mouse.wheel(
688
- scrollDistance,
689
- 0,
690
- startingPoint?.left,
691
- startingPoint?.top
692
- );
693
- }
694
- async clearInput(element) {
695
- if (!element) {
696
- console.warn("No element to clear input");
697
- return;
698
- }
699
- await this.mouse.click(element.center[0], element.center[1]);
700
- await this.sendCommandToDebugger("Input.dispatchKeyEvent", {
701
- type: "keyDown",
702
- commands: ["selectAll"]
703
- });
704
- await this.sendCommandToDebugger("Input.dispatchKeyEvent", {
705
- type: "keyUp",
706
- commands: ["selectAll"]
707
- });
708
- await sleep(100);
709
- await this.keyboard.press({
710
- key: "Backspace"
711
- });
712
- }
713
- async destroy() {
714
- this.activeTabId = null;
715
- await this.detachDebugger();
716
- this.destroyed = true;
717
- }
718
- };
719
-
720
- // src/bridge-mode/common.ts
721
- var DefaultBridgeServerPort = 3766;
722
- var DefaultLocalEndpoint = `http://127.0.0.1:${DefaultBridgeServerPort}`;
723
-
724
- // src/bridge-mode/io-client.ts
725
- var import_utils4 = require("misoai-shared/utils");
726
- var import_socket = require("socket.io-client");
727
- var BridgeClient = class {
728
- constructor(endpoint, onBridgeCall, onDisconnect) {
729
- this.endpoint = endpoint;
730
- this.onBridgeCall = onBridgeCall;
731
- this.onDisconnect = onDisconnect;
732
- this.socket = null;
733
- this.serverVersion = null;
734
- }
735
- async connect() {
736
- return new Promise((resolve, reject) => {
737
- this.socket = (0, import_socket.io)(this.endpoint, {
738
- reconnection: false,
739
- query: {
740
- version: "1.0.0"
741
- }
742
- });
743
- const timeout = setTimeout(() => {
744
- try {
745
- this.socket?.offAny();
746
- this.socket?.close();
747
- } catch (e) {
748
- console.warn("got error when offing socket", e);
749
- }
750
- this.socket = null;
751
- reject(new Error("failed to connect to bridge server after timeout"));
752
- }, 1 * 1e3);
753
- this.socket.on("disconnect", (reason) => {
754
- this.socket = null;
755
- this.onDisconnect?.();
756
- });
757
- this.socket.on("connect_error", (e) => {
758
- console.error("bridge-connect-error", e);
759
- reject(new Error(e || "bridge connect error"));
760
- });
761
- this.socket.on(
762
- "bridge-connected" /* Connected */,
763
- (payload) => {
764
- clearTimeout(timeout);
765
- this.serverVersion = payload?.version || "unknown";
766
- resolve(this.socket);
767
- }
768
- );
769
- this.socket.on("bridge-refused" /* Refused */, (e) => {
770
- console.error("bridge-refused", e);
771
- try {
772
- this.socket?.disconnect();
773
- } catch (e2) {
774
- }
775
- reject(new Error(e || "bridge refused"));
776
- });
777
- this.socket.on("bridge-call" /* Call */, (call) => {
778
- const id = call.id;
779
- (0, import_utils4.assert)(typeof id !== "undefined", "call id is required");
780
- (async () => {
781
- let response;
782
- try {
783
- response = await this.onBridgeCall(call.method, call.args);
784
- } catch (e) {
785
- const errorContent = `Error from bridge client when calling, method: ${call.method}, args: ${call.args}, error: ${e?.message || e}
786
- ${e?.stack || ""}`;
787
- console.error(errorContent);
788
- return this.socket?.emit("bridge-call-response" /* CallResponse */, {
789
- id,
790
- error: errorContent
791
- });
792
- }
793
- this.socket?.emit("bridge-call-response" /* CallResponse */, {
794
- id,
795
- response
796
- });
797
- })();
798
- });
799
- });
800
- }
801
- disconnect() {
802
- this.socket?.disconnect();
803
- this.socket = null;
804
- }
805
- };
806
-
807
- // src/bridge-mode/page-browser-side.ts
808
- var ExtensionBridgePageBrowserSide = class extends ChromeExtensionProxyPage {
809
- constructor(onDisconnect = () => {
810
- }, onLogMessage = () => {
811
- }, forceSameTabNavigation = true) {
812
- super(forceSameTabNavigation);
813
- this.onDisconnect = onDisconnect;
814
- this.onLogMessage = onLogMessage;
815
- this.bridgeClient = null;
816
- this.newlyCreatedTabIds = [];
817
- }
818
- async setupBridgeClient() {
819
- this.bridgeClient = new BridgeClient(
820
- `ws://localhost:${DefaultBridgeServerPort}`,
821
- async (method, args) => {
822
- console.log("bridge call from cli side", method, args);
823
- if (method === "connectNewTabWithUrl" /* ConnectNewTabWithUrl */) {
824
- return this.connectNewTabWithUrl.apply(
825
- this,
826
- args
827
- );
828
- }
829
- if (method === "getBrowserTabList" /* GetBrowserTabList */) {
830
- return this.getBrowserTabList.apply(this, args);
831
- }
832
- if (method === "setActiveTabId" /* SetActiveTabId */) {
833
- return this.setActiveTabId.apply(this, args);
834
- }
835
- if (method === "connectCurrentTab" /* ConnectCurrentTab */) {
836
- return this.connectCurrentTab.apply(this, args);
837
- }
838
- if (method === "bridge-update-agent-status" /* UpdateAgentStatus */) {
839
- return this.onLogMessage(args[0], "status");
840
- }
841
- const tabId = await this.getActiveTabId();
842
- if (!tabId || tabId === 0) {
843
- throw new Error("no tab is connected");
844
- }
845
- if (method.startsWith("mouse." /* PREFIX */)) {
846
- const actionName = method.split(".")[1];
847
- if (actionName === "drag") {
848
- return this.mouse[actionName].apply(this.mouse, args);
849
- }
850
- return this.mouse[actionName].apply(this.mouse, args);
851
- }
852
- if (method.startsWith("keyboard." /* PREFIX */)) {
853
- const actionName = method.split(".")[1];
854
- if (actionName === "press") {
855
- return this.keyboard[actionName].apply(this.keyboard, args);
856
- }
857
- return this.keyboard[actionName].apply(this.keyboard, args);
858
- }
859
- try {
860
- const result = await this[method](
861
- ...args
862
- );
863
- return result;
864
- } catch (e) {
865
- const errorMessage = e instanceof Error ? e.message : "Unknown error";
866
- console.error("error calling method", method, args, e);
867
- this.onLogMessage(
868
- `Error calling method: ${method}, ${errorMessage}`,
869
- "log"
870
- );
871
- throw new Error(errorMessage, { cause: e });
872
- }
873
- },
874
- // on disconnect
875
- () => {
876
- return this.destroy();
877
- }
878
- );
879
- await this.bridgeClient.connect();
880
- this.onLogMessage(
881
- `Bridge connected, cli-side version v${this.bridgeClient.serverVersion}, browser-side version v${"1.0.0"}`,
882
- "log"
883
- );
884
- }
885
- async connect() {
886
- return await this.setupBridgeClient();
887
- }
888
- async connectNewTabWithUrl(url, options = {
889
- forceSameTabNavigation: true
890
- }) {
891
- const tab = await chrome.tabs.create({ url });
892
- const tabId = tab.id;
893
- (0, import_utils5.assert)(tabId, "failed to get tabId after creating a new tab");
894
- this.onLogMessage(`Creating new tab: ${url}`, "log");
895
- this.newlyCreatedTabIds.push(tabId);
896
- if (options?.forceSameTabNavigation) {
897
- this.forceSameTabNavigation = true;
898
- }
899
- await this.setActiveTabId(tabId);
900
- }
901
- async connectCurrentTab(options = {
902
- forceSameTabNavigation: true
903
- }) {
904
- const tabs = await chrome.tabs.query({ active: true, currentWindow: true });
905
- const tabId = tabs[0]?.id;
906
- (0, import_utils5.assert)(tabId, "failed to get tabId");
907
- this.onLogMessage(`Connected to current tab: ${tabs[0]?.url}`, "log");
908
- if (options?.forceSameTabNavigation) {
909
- this.forceSameTabNavigation = true;
910
- }
911
- await this.setActiveTabId(tabId);
912
- }
913
- async setDestroyOptions(options) {
914
- this.destroyOptions = options;
915
- }
916
- async destroy() {
917
- if (this.destroyOptions?.closeTab && this.newlyCreatedTabIds.length > 0) {
918
- this.onLogMessage("Closing all newly created tabs by bridge...", "log");
919
- for (const tabId of this.newlyCreatedTabIds) {
920
- await chrome.tabs.remove(tabId);
921
- }
922
- this.newlyCreatedTabIds = [];
923
- }
924
- await super.destroy();
925
- if (this.bridgeClient) {
926
- this.bridgeClient.disconnect();
927
- this.bridgeClient = null;
928
- this.onDisconnect();
929
- }
930
- }
931
- };
932
- // Annotate the CommonJS export names for ESM import in node:
933
- 0 && (module.exports = {
934
- ExtensionBridgePageBrowserSide
935
- });
936
- /**
937
- * @license
938
- * Copyright 2017 Google Inc.
939
- * SPDX-License-Identifier: Apache-2.0
940
- */
941
-
942
- //# sourceMappingURL=bridge-mode-browser.js.map