@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,255 @@
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
+ killRunningServer: ()=>killRunningServer,
28
+ BridgeServer: ()=>BridgeServer
29
+ });
30
+ const external_node_http_namespaceObject = require("node:http");
31
+ const utils_namespaceObject = require("@godscene/core/utils");
32
+ const shared_utils_namespaceObject = require("@godscene/shared/utils");
33
+ const external_socket_io_namespaceObject = require("socket.io");
34
+ const external_socket_io_client_namespaceObject = require("socket.io-client");
35
+ const external_common_js_namespaceObject = require("./common.js");
36
+ function _define_property(obj, key, value) {
37
+ if (key in obj) Object.defineProperty(obj, key, {
38
+ value: value,
39
+ enumerable: true,
40
+ configurable: true,
41
+ writable: true
42
+ });
43
+ else obj[key] = value;
44
+ return obj;
45
+ }
46
+ const killRunningServer = async (port, host = 'localhost')=>{
47
+ try {
48
+ const client = (0, external_socket_io_client_namespaceObject.io)(`ws://${host}:${port || external_common_js_namespaceObject.DefaultBridgeServerPort}`, {
49
+ query: {
50
+ [external_common_js_namespaceObject.BridgeSignalKill]: 1
51
+ }
52
+ });
53
+ await (0, utils_namespaceObject.sleep)(300);
54
+ await client.close();
55
+ } catch (e) {}
56
+ };
57
+ class BridgeServer {
58
+ async listen(opts = {}) {
59
+ const { timeout = 30000 } = opts;
60
+ if (this.closeConflictServer) await killRunningServer(this.port, this.host);
61
+ return new Promise((resolve, reject)=>{
62
+ if (this.listeningTimerFlag) return reject(new Error('already listening'));
63
+ this.listeningTimerFlag = true;
64
+ this.listeningTimeoutId = timeout ? setTimeout(()=>{
65
+ reject(new Error(`no extension connected after ${timeout}ms (${external_common_js_namespaceObject.BridgeErrorCodeNoClientConnected})`));
66
+ }, timeout) : null;
67
+ this.connectionTipTimer = !timeout || timeout > 3000 ? setTimeout(()=>{
68
+ (0, shared_utils_namespaceObject.logMsg)('waiting for bridge to connect...');
69
+ }, 2000) : null;
70
+ const httpServer = (0, external_node_http_namespaceObject.createServer)();
71
+ httpServer.once('listening', ()=>{
72
+ resolve();
73
+ });
74
+ httpServer.once('error', (err)=>{
75
+ reject(new Error(`Bridge Listening Error: ${err.message}`));
76
+ });
77
+ if ('127.0.0.1' === this.host) httpServer.listen(this.port);
78
+ else httpServer.listen(this.port, this.host);
79
+ this.io = new external_socket_io_namespaceObject.Server(httpServer, {
80
+ maxHttpBufferSize: 104857600,
81
+ pingTimeout: 60000
82
+ });
83
+ this.io.use((socket, next)=>{
84
+ if (socket.handshake.url.includes(external_common_js_namespaceObject.BridgeSignalKill)) return next();
85
+ if (this.socket?.connected) return next(new Error('server already connected by another client'));
86
+ next();
87
+ });
88
+ this.io.on('connection', (socket)=>{
89
+ const url = socket.handshake.url;
90
+ if (url.includes(external_common_js_namespaceObject.BridgeSignalKill)) {
91
+ console.warn('kill signal received, closing bridge server');
92
+ return this.close();
93
+ }
94
+ this.connectionLost = false;
95
+ this.connectionLostReason = '';
96
+ this.listeningTimeoutId && clearTimeout(this.listeningTimeoutId);
97
+ this.listeningTimeoutId = null;
98
+ this.connectionTipTimer && clearTimeout(this.connectionTipTimer);
99
+ this.connectionTipTimer = null;
100
+ if (this.socket?.connected) {
101
+ socket.emit(external_common_js_namespaceObject.BridgeEvent.Refused);
102
+ socket.disconnect();
103
+ (0, shared_utils_namespaceObject.logMsg)('refused new connection: server already connected by another client');
104
+ return;
105
+ }
106
+ if (this.socket) {
107
+ try {
108
+ this.socket.disconnect();
109
+ } catch (e) {
110
+ (0, shared_utils_namespaceObject.logMsg)(`failed to disconnect stale socket: ${e}`);
111
+ }
112
+ this.socket = null;
113
+ }
114
+ try {
115
+ (0, shared_utils_namespaceObject.logMsg)('one client connected');
116
+ this.socket = socket;
117
+ const clientVersion = socket.handshake.query.version;
118
+ (0, shared_utils_namespaceObject.logMsg)(`Bridge connected, cli-side version v1.7.10, browser-side version v${clientVersion}`);
119
+ socket.on(external_common_js_namespaceObject.BridgeEvent.CallResponse, (params)=>{
120
+ const id = params.id;
121
+ const response = params.response;
122
+ const error = params.error;
123
+ this.triggerCallResponseCallback(id, error, response);
124
+ });
125
+ socket.on('disconnect', (reason)=>{
126
+ this.connectionLost = true;
127
+ this.connectionLostReason = reason;
128
+ this.socket = null;
129
+ for(const id in this.calls){
130
+ const call = this.calls[id];
131
+ if (!call.responseTime) {
132
+ const errorMessage = this.connectionLostErrorMsg();
133
+ this.triggerCallResponseCallback(id, new Error(errorMessage), null);
134
+ }
135
+ }
136
+ for(const id in this.calls)if (this.calls[id].responseTime) delete this.calls[id];
137
+ this.onDisconnect?.(reason);
138
+ });
139
+ setTimeout(()=>{
140
+ this.onConnect?.();
141
+ const payload = {
142
+ version: "1.7.10"
143
+ };
144
+ socket.emit(external_common_js_namespaceObject.BridgeEvent.Connected, payload);
145
+ Promise.resolve().then(()=>{
146
+ for(const id in this.calls)if (0 === this.calls[id].callTime) this.emitCall(id);
147
+ });
148
+ }, 0);
149
+ } catch (e) {
150
+ (0, shared_utils_namespaceObject.logMsg)(`failed to handle connection event: ${e}`);
151
+ }
152
+ });
153
+ this.io.on('close', ()=>{
154
+ this.close();
155
+ });
156
+ });
157
+ }
158
+ async triggerCallResponseCallback(id, error, response) {
159
+ const call = this.calls[id];
160
+ if (!call) throw new Error(`call ${id} not found`);
161
+ if (error) call.error = error instanceof Error ? error : new Error('string' == typeof error ? error : String(error));
162
+ else call.error = void 0;
163
+ call.response = response;
164
+ call.responseTime = Date.now();
165
+ call.callback(call.error, response);
166
+ }
167
+ async emitCall(id) {
168
+ const call = this.calls[id];
169
+ if (!call) throw new Error(`call ${id} not found`);
170
+ if (this.connectionLost) {
171
+ const message = `Connection lost, reason: ${this.connectionLostReason}`;
172
+ call.callback(new Error(message), null);
173
+ return;
174
+ }
175
+ if (this.socket) {
176
+ this.socket.emit(external_common_js_namespaceObject.BridgeEvent.Call, {
177
+ id,
178
+ method: call.method,
179
+ args: call.args
180
+ });
181
+ call.callTime = Date.now();
182
+ }
183
+ }
184
+ async call(method, args, timeout = external_common_js_namespaceObject.BridgeCallTimeout) {
185
+ const id = `${this.callId++}`;
186
+ return new Promise((resolve, reject)=>{
187
+ const timeoutId = setTimeout(()=>{
188
+ (0, shared_utils_namespaceObject.logMsg)(`bridge call timeout, id=${id}, method=${method}, args=`, args);
189
+ this.calls[id].error = new Error(`Bridge call timeout after ${timeout}ms: ${method}`);
190
+ reject(this.calls[id].error);
191
+ }, timeout);
192
+ this.calls[id] = {
193
+ method,
194
+ args,
195
+ response: null,
196
+ callTime: 0,
197
+ responseTime: 0,
198
+ callback: (error, response)=>{
199
+ clearTimeout(timeoutId);
200
+ if (error) reject(error);
201
+ else resolve(response);
202
+ }
203
+ };
204
+ this.emitCall(id);
205
+ });
206
+ }
207
+ async close() {
208
+ this.listeningTimeoutId && clearTimeout(this.listeningTimeoutId);
209
+ this.connectionTipTimer && clearTimeout(this.connectionTipTimer);
210
+ const closeProcess = this.io?.close();
211
+ this.io = null;
212
+ return closeProcess;
213
+ }
214
+ constructor(host, port, onConnect, onDisconnect, closeConflictServer){
215
+ _define_property(this, "host", void 0);
216
+ _define_property(this, "port", void 0);
217
+ _define_property(this, "onConnect", void 0);
218
+ _define_property(this, "onDisconnect", void 0);
219
+ _define_property(this, "closeConflictServer", void 0);
220
+ _define_property(this, "callId", void 0);
221
+ _define_property(this, "io", void 0);
222
+ _define_property(this, "socket", void 0);
223
+ _define_property(this, "listeningTimeoutId", void 0);
224
+ _define_property(this, "listeningTimerFlag", void 0);
225
+ _define_property(this, "connectionTipTimer", void 0);
226
+ _define_property(this, "calls", void 0);
227
+ _define_property(this, "connectionLost", void 0);
228
+ _define_property(this, "connectionLostReason", void 0);
229
+ _define_property(this, "connectionLostErrorMsg", void 0);
230
+ this.host = host;
231
+ this.port = port;
232
+ this.onConnect = onConnect;
233
+ this.onDisconnect = onDisconnect;
234
+ this.closeConflictServer = closeConflictServer;
235
+ this.callId = 0;
236
+ this.io = null;
237
+ this.socket = null;
238
+ this.listeningTimeoutId = null;
239
+ this.listeningTimerFlag = false;
240
+ this.connectionTipTimer = null;
241
+ this.calls = {};
242
+ this.connectionLost = false;
243
+ this.connectionLostReason = '';
244
+ this.connectionLostErrorMsg = ()=>`Connection lost, reason: ${this.connectionLostReason}`;
245
+ }
246
+ }
247
+ exports.BridgeServer = __webpack_exports__.BridgeServer;
248
+ exports.killRunningServer = __webpack_exports__.killRunningServer;
249
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
250
+ "BridgeServer",
251
+ "killRunningServer"
252
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
253
+ Object.defineProperty(exports, '__esModule', {
254
+ value: true
255
+ });
@@ -0,0 +1,163 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.n = (module)=>{
5
+ var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
6
+ __webpack_require__.d(getter, {
7
+ a: getter
8
+ });
9
+ return getter;
10
+ };
11
+ })();
12
+ (()=>{
13
+ __webpack_require__.d = (exports1, definition)=>{
14
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
15
+ enumerable: true,
16
+ get: definition[key]
17
+ });
18
+ };
19
+ })();
20
+ (()=>{
21
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
22
+ })();
23
+ (()=>{
24
+ __webpack_require__.r = (exports1)=>{
25
+ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
26
+ value: 'Module'
27
+ });
28
+ Object.defineProperty(exports1, '__esModule', {
29
+ value: true
30
+ });
31
+ };
32
+ })();
33
+ var __webpack_exports__ = {};
34
+ __webpack_require__.r(__webpack_exports__);
35
+ __webpack_require__.d(__webpack_exports__, {
36
+ ExtensionBridgePageBrowserSide: ()=>ExtensionBridgePageBrowserSide
37
+ });
38
+ const utils_namespaceObject = require("@godscene/shared/utils");
39
+ const page_js_namespaceObject = require("../chrome-extension/page.js");
40
+ var page_js_default = /*#__PURE__*/ __webpack_require__.n(page_js_namespaceObject);
41
+ const external_common_js_namespaceObject = require("./common.js");
42
+ const external_io_client_js_namespaceObject = require("./io-client.js");
43
+ function _define_property(obj, key, value) {
44
+ if (key in obj) Object.defineProperty(obj, key, {
45
+ value: value,
46
+ enumerable: true,
47
+ configurable: true,
48
+ writable: true
49
+ });
50
+ else obj[key] = value;
51
+ return obj;
52
+ }
53
+ class ExtensionBridgePageBrowserSide extends page_js_default() {
54
+ async setupBridgeClient() {
55
+ const endpoint = this.serverEndpoint || `ws://localhost:${external_common_js_namespaceObject.DefaultBridgeServerPort}`;
56
+ let resolveConfirmationGate = ()=>{};
57
+ if (this.onConnectionRequest) this.confirmationPromise = new Promise((resolve)=>{
58
+ resolveConfirmationGate = resolve;
59
+ });
60
+ this.bridgeClient = new external_io_client_js_namespaceObject.BridgeClient(endpoint, async (method, args)=>{
61
+ if (this.confirmationPromise) {
62
+ const allowed = await this.confirmationPromise;
63
+ if (!allowed) throw new Error('Connection denied by user');
64
+ }
65
+ this.onLogMessage(`bridge call from cli side: ${method}`, 'log');
66
+ if (method === external_common_js_namespaceObject.BridgeEvent.ConnectNewTabWithUrl) return this.connectNewTabWithUrl.apply(this, args);
67
+ if (method === external_common_js_namespaceObject.BridgeEvent.GetBrowserTabList) return this.getBrowserTabList.apply(this, args);
68
+ if (method === external_common_js_namespaceObject.BridgeEvent.SetActiveTabId) return this.setActiveTabId.apply(this, args);
69
+ if (method === external_common_js_namespaceObject.BridgeEvent.ConnectCurrentTab) return this.connectCurrentTab.apply(this, args);
70
+ if (method === external_common_js_namespaceObject.BridgeEvent.UpdateAgentStatus) return this.onLogMessage(args[0], 'status');
71
+ const tabId = await this.getActiveTabId();
72
+ if (!tabId || 0 === tabId) throw new Error('no tab is connected');
73
+ if (method.startsWith(external_common_js_namespaceObject.MouseEvent.PREFIX)) {
74
+ const actionName = method.split('.')[1];
75
+ return this.mouse[actionName].apply(this.mouse, args);
76
+ }
77
+ if (method.startsWith(external_common_js_namespaceObject.KeyboardEvent.PREFIX)) {
78
+ const actionName = method.split('.')[1];
79
+ return this.keyboard[actionName].apply(this.keyboard, args);
80
+ }
81
+ if (!this[method]) return void this.onLogMessage(`method not found: ${method}`, 'log');
82
+ try {
83
+ const result = await this[method](...args);
84
+ return result;
85
+ } catch (e) {
86
+ const errorMessage = e instanceof Error ? e.message : 'Unknown error';
87
+ this.onLogMessage(`Error calling method: ${method}, ${errorMessage}`, 'log');
88
+ throw new Error(errorMessage, {
89
+ cause: e
90
+ });
91
+ }
92
+ }, ()=>this.destroy());
93
+ await this.bridgeClient.connect();
94
+ if (this.onConnectionRequest) {
95
+ this.onLogMessage('Waiting for user confirmation...', 'log');
96
+ const allowed = await this.onConnectionRequest();
97
+ resolveConfirmationGate(allowed);
98
+ this.confirmationPromise = null;
99
+ if (!allowed) {
100
+ this.onLogMessage('Connection denied by user', 'log');
101
+ this.bridgeClient.disconnect();
102
+ this.bridgeClient = null;
103
+ throw new Error('Connection denied by user');
104
+ }
105
+ }
106
+ this.onLogMessage(`Bridge connected, cli-side version v${this.bridgeClient.serverVersion}, browser-side version v1.7.10`, 'log');
107
+ }
108
+ async connect() {
109
+ return await this.setupBridgeClient();
110
+ }
111
+ async connectNewTabWithUrl(url, options = {
112
+ forceSameTabNavigation: true
113
+ }) {
114
+ const tab = await chrome.tabs.create({
115
+ url
116
+ });
117
+ const tabId = tab.id;
118
+ (0, utils_namespaceObject.assert)(tabId, 'failed to get tabId after creating a new tab');
119
+ this.onLogMessage(`Creating new tab: ${url}`, 'log');
120
+ this.newlyCreatedTabIds.push(tabId);
121
+ if (options?.forceSameTabNavigation) this.forceSameTabNavigation = true;
122
+ await this.setActiveTabId(tabId);
123
+ }
124
+ async connectCurrentTab(options = {
125
+ forceSameTabNavigation: true
126
+ }) {
127
+ const tabs = await chrome.tabs.query({
128
+ active: true,
129
+ currentWindow: true
130
+ });
131
+ const tabId = tabs[0]?.id;
132
+ (0, utils_namespaceObject.assert)(tabId, 'failed to get tabId');
133
+ this.onLogMessage(`Connected to current tab: ${tabs[0]?.url}`, 'log');
134
+ if (options?.forceSameTabNavigation) this.forceSameTabNavigation = true;
135
+ await this.setActiveTabId(tabId);
136
+ }
137
+ async setDestroyOptions(options) {
138
+ this.destroyOptions = options;
139
+ }
140
+ async destroy() {
141
+ if (this.destroyOptions?.closeTab && this.newlyCreatedTabIds.length > 0) {
142
+ this.onLogMessage('Closing all newly created tabs by bridge...', 'log');
143
+ for (const tabId of this.newlyCreatedTabIds)await chrome.tabs.remove(tabId);
144
+ this.newlyCreatedTabIds = [];
145
+ }
146
+ await super.destroy();
147
+ if (this.bridgeClient) {
148
+ this.bridgeClient.disconnect();
149
+ this.bridgeClient = null;
150
+ this.onDisconnect();
151
+ }
152
+ }
153
+ constructor(serverEndpoint, onDisconnect = ()=>{}, onLogMessage = ()=>{}, forceSameTabNavigation = true, onConnectionRequest){
154
+ super(forceSameTabNavigation), _define_property(this, "serverEndpoint", void 0), _define_property(this, "onDisconnect", void 0), _define_property(this, "onLogMessage", void 0), _define_property(this, "onConnectionRequest", void 0), _define_property(this, "bridgeClient", void 0), _define_property(this, "destroyOptions", void 0), _define_property(this, "newlyCreatedTabIds", void 0), _define_property(this, "confirmationPromise", void 0), this.serverEndpoint = serverEndpoint, this.onDisconnect = onDisconnect, this.onLogMessage = onLogMessage, this.onConnectionRequest = onConnectionRequest, this.bridgeClient = null, this.newlyCreatedTabIds = [], this.confirmationPromise = null;
155
+ }
156
+ }
157
+ exports.ExtensionBridgePageBrowserSide = __webpack_exports__.ExtensionBridgePageBrowserSide;
158
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
159
+ "ExtensionBridgePageBrowserSide"
160
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
161
+ Object.defineProperty(exports, '__esModule', {
162
+ value: true
163
+ });
@@ -0,0 +1,50 @@
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
+ PROXY_ENDPOINT_FILE: ()=>PROXY_ENDPOINT_FILE,
28
+ TARGET_ID_FILE: ()=>TARGET_ID_FILE,
29
+ PROXY_PID_FILE: ()=>PROXY_PID_FILE,
30
+ PROXY_UPSTREAM_FILE: ()=>PROXY_UPSTREAM_FILE
31
+ });
32
+ const external_node_os_namespaceObject = require("node:os");
33
+ const external_node_path_namespaceObject = require("node:path");
34
+ const PROXY_ENDPOINT_FILE = (0, external_node_path_namespaceObject.join)((0, external_node_os_namespaceObject.tmpdir)(), 'midscene-cdp-proxy-endpoint');
35
+ const PROXY_PID_FILE = (0, external_node_path_namespaceObject.join)((0, external_node_os_namespaceObject.tmpdir)(), 'midscene-cdp-proxy-pid');
36
+ const PROXY_UPSTREAM_FILE = (0, external_node_path_namespaceObject.join)((0, external_node_os_namespaceObject.tmpdir)(), 'midscene-cdp-proxy-upstream');
37
+ const TARGET_ID_FILE = (0, external_node_path_namespaceObject.join)((0, external_node_os_namespaceObject.tmpdir)(), 'midscene-cdp-target-id');
38
+ exports.PROXY_ENDPOINT_FILE = __webpack_exports__.PROXY_ENDPOINT_FILE;
39
+ exports.PROXY_PID_FILE = __webpack_exports__.PROXY_PID_FILE;
40
+ exports.PROXY_UPSTREAM_FILE = __webpack_exports__.PROXY_UPSTREAM_FILE;
41
+ exports.TARGET_ID_FILE = __webpack_exports__.TARGET_ID_FILE;
42
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
43
+ "PROXY_ENDPOINT_FILE",
44
+ "PROXY_PID_FILE",
45
+ "PROXY_UPSTREAM_FILE",
46
+ "TARGET_ID_FILE"
47
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
48
+ Object.defineProperty(exports, '__esModule', {
49
+ value: true
50
+ });