@midscene/web 1.8.1-beta-20260513084557.0 → 1.8.1

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.
@@ -23,7 +23,7 @@ class BridgeClient {
23
23
  ]
24
24
  } : {},
25
25
  query: {
26
- version: "1.8.1-beta-20260513084557.0"
26
+ version: "1.8.1"
27
27
  }
28
28
  });
29
29
  const timeout = setTimeout(()=>{
@@ -86,7 +86,7 @@ class BridgeServer {
86
86
  logMsg('one client connected');
87
87
  this.socket = socket;
88
88
  const clientVersion = socket.handshake.query.version;
89
- logMsg(`Bridge connected, cli-side version v1.8.1-beta-20260513084557.0, browser-side version v${clientVersion}`);
89
+ logMsg(`Bridge connected, cli-side version v1.8.1, browser-side version v${clientVersion}`);
90
90
  socket.on(BridgeEvent.CallResponse, (params)=>{
91
91
  const id = params.id;
92
92
  const response = params.response;
@@ -110,7 +110,7 @@ class BridgeServer {
110
110
  setTimeout(()=>{
111
111
  this.onConnect?.();
112
112
  const payload = {
113
- version: "1.8.1-beta-20260513084557.0"
113
+ version: "1.8.1"
114
114
  };
115
115
  socket.emit(BridgeEvent.Connected, payload);
116
116
  Promise.resolve().then(()=>{
@@ -1 +1 @@
1
- {"version":3,"file":"bridge-mode/io-server.mjs","sources":["../../../src/bridge-mode/io-server.ts"],"sourcesContent":["import { createServer } from 'node:http';\nimport { sleep } from '@midscene/core/utils';\nimport { logMsg } from '@midscene/shared/utils';\nimport { Server, type Socket as ServerSocket } from 'socket.io';\nimport { io as ClientIO } from 'socket.io-client';\n\nimport {\n type BridgeCall,\n type BridgeCallResponse,\n BridgeCallTimeout,\n type BridgeConnectedEventPayload,\n BridgeErrorCodeNoClientConnected,\n BridgeEvent,\n BridgeSignalKill,\n DefaultBridgeServerPort,\n} from './common';\n\ndeclare const __VERSION__: string;\n\nexport const killRunningServer = async (port?: number, host = 'localhost') => {\n try {\n const client = ClientIO(`ws://${host}:${port || DefaultBridgeServerPort}`, {\n query: {\n [BridgeSignalKill]: 1,\n },\n });\n await sleep(300);\n await client.close();\n } catch (e) {\n // console.error('failed to kill port', e);\n }\n};\n\n// ws server, this is where the request is sent\nexport class BridgeServer {\n private callId = 0;\n private io: Server | null = null;\n private socket: ServerSocket | null = null;\n private listeningTimeoutId: NodeJS.Timeout | null = null;\n private listeningTimerFlag = false;\n private connectionTipTimer: NodeJS.Timeout | null = null;\n public calls: Record<string, BridgeCall> = {};\n\n private connectionLost = false;\n private connectionLostReason = '';\n\n constructor(\n public host: string,\n public port: number,\n public onConnect?: () => void,\n public onDisconnect?: (reason: string) => void,\n public closeConflictServer?: boolean,\n ) {}\n\n async listen(\n opts: {\n timeout?: number | false;\n } = {},\n ): Promise<void> {\n const { timeout = 30000 } = opts;\n\n if (this.closeConflictServer) {\n await killRunningServer(this.port, this.host);\n }\n\n return new Promise((resolve, reject) => {\n if (this.listeningTimerFlag) {\n return reject(new Error('already listening'));\n }\n this.listeningTimerFlag = true;\n\n this.listeningTimeoutId = timeout\n ? setTimeout(() => {\n reject(\n new Error(\n `no extension connected after ${timeout}ms (${BridgeErrorCodeNoClientConnected})`,\n ),\n );\n }, timeout)\n : null;\n\n this.connectionTipTimer =\n !timeout || timeout > 3000\n ? setTimeout(() => {\n logMsg('waiting for bridge to connect...');\n }, 2000)\n : null;\n\n // Create HTTP server and start listening on the specified host and port\n const httpServer = createServer();\n\n // Set up HTTP server event listeners FIRST\n httpServer.once('listening', () => {\n resolve();\n });\n\n httpServer.once('error', (err: Error) => {\n reject(new Error(`Bridge Listening Error: ${err.message}`));\n });\n\n // Start listening BEFORE creating Socket.IO Server\n // When host is 127.0.0.1 (default), don't specify host to listen on all local interfaces (IPv4 + IPv6)\n // This ensures localhost resolves correctly in both IPv4 and IPv6 environments\n if (this.host === '127.0.0.1') {\n httpServer.listen(this.port);\n } else {\n httpServer.listen(this.port, this.host);\n }\n\n // Now create Socket.IO Server attached to the already-listening HTTP server\n this.io = new Server(httpServer, {\n maxHttpBufferSize: 100 * 1024 * 1024, // 100MB\n // Increase pingTimeout to tolerate Chrome MV3 Service Worker suspension.\n // The SW keepalive alarm fires every ~24s; default pingTimeout (20s) may\n // be too short if the SW is suspended between alarm pings.\n pingTimeout: 60000,\n });\n\n this.io.use((socket, next) => {\n // Always allow kill signal connections through\n if (socket.handshake.url.includes(BridgeSignalKill)) {\n return next();\n }\n // Allow new connections to replace old ones (reconnection after\n // extension Stop→Start). If the old socket is already disconnected\n // or unresponsive, accept the new connection immediately.\n if (this.socket?.connected) {\n return next(new Error('server already connected by another client'));\n }\n next();\n });\n\n this.io.on('connection', (socket) => {\n // check the connection url\n const url = socket.handshake.url;\n if (url.includes(BridgeSignalKill)) {\n console.warn('kill signal received, closing bridge server');\n return this.close();\n }\n\n this.connectionLost = false;\n this.connectionLostReason = '';\n this.listeningTimeoutId && clearTimeout(this.listeningTimeoutId);\n this.listeningTimeoutId = null;\n this.connectionTipTimer && clearTimeout(this.connectionTipTimer);\n this.connectionTipTimer = null;\n if (this.socket?.connected) {\n socket.emit(BridgeEvent.Refused);\n socket.disconnect();\n logMsg(\n 'refused new connection: server already connected by another client',\n );\n return;\n }\n\n // Clean up stale old socket if it exists but is no longer connected\n if (this.socket) {\n try {\n this.socket.disconnect();\n } catch (e) {\n logMsg(`failed to disconnect stale socket: ${e}`);\n }\n this.socket = null;\n }\n\n try {\n logMsg('one client connected');\n this.socket = socket;\n\n const clientVersion = socket.handshake.query.version;\n logMsg(\n `Bridge connected, cli-side version v${__VERSION__}, browser-side version v${clientVersion}`,\n );\n\n socket.on(BridgeEvent.CallResponse, (params: BridgeCallResponse) => {\n const id = params.id;\n const response = params.response;\n const error = params.error;\n\n this.triggerCallResponseCallback(id, error, response);\n });\n\n socket.on('disconnect', (reason: string) => {\n this.connectionLost = true;\n this.connectionLostReason = reason;\n this.socket = null;\n\n // flush all pending calls as error and clean up completed calls\n for (const id in this.calls) {\n const call = this.calls[id];\n\n if (!call.responseTime) {\n const errorMessage = this.connectionLostErrorMsg();\n this.triggerCallResponseCallback(\n id,\n new Error(errorMessage),\n null,\n );\n }\n }\n\n // Clean up completed calls to prevent memory leaks in long-running sessions\n for (const id in this.calls) {\n if (this.calls[id].responseTime) {\n delete this.calls[id];\n }\n }\n\n this.onDisconnect?.(reason);\n });\n\n setTimeout(() => {\n this.onConnect?.();\n\n const payload = {\n version: __VERSION__,\n } as BridgeConnectedEventPayload;\n socket.emit(BridgeEvent.Connected, payload);\n Promise.resolve().then(() => {\n for (const id in this.calls) {\n if (this.calls[id].callTime === 0) {\n this.emitCall(id);\n }\n }\n });\n }, 0);\n } catch (e) {\n logMsg(`failed to handle connection event: ${e}`);\n }\n });\n\n this.io.on('close', () => {\n this.close();\n });\n });\n }\n\n private connectionLostErrorMsg = () => {\n return `Connection lost, reason: ${this.connectionLostReason}`;\n };\n\n private async triggerCallResponseCallback(\n id: string | number,\n error: Error | string | null,\n response: any,\n ) {\n const call = this.calls[id];\n if (!call) {\n throw new Error(`call ${id} not found`);\n }\n // Ensure error is always an Error object (bridge client may send strings)\n if (error) {\n call.error =\n error instanceof Error\n ? error\n : new Error(typeof error === 'string' ? error : String(error));\n } else {\n call.error = undefined;\n }\n call.response = response;\n call.responseTime = Date.now();\n\n call.callback(call.error, response);\n }\n\n private async emitCall(id: string) {\n const call = this.calls[id];\n if (!call) {\n throw new Error(`call ${id} not found`);\n }\n\n if (this.connectionLost) {\n const message = `Connection lost, reason: ${this.connectionLostReason}`;\n call.callback(new Error(message), null);\n return;\n }\n\n if (this.socket) {\n this.socket.emit(BridgeEvent.Call, {\n id,\n method: call.method,\n args: call.args,\n });\n call.callTime = Date.now();\n }\n }\n\n async call<T = any>(\n method: string,\n args: any[],\n timeout = BridgeCallTimeout,\n ): Promise<T> {\n const id = `${this.callId++}`;\n\n return new Promise((resolve, reject) => {\n const timeoutId = setTimeout(() => {\n logMsg(`bridge call timeout, id=${id}, method=${method}, args=`, args);\n this.calls[id].error = new Error(\n `Bridge call timeout after ${timeout}ms: ${method}`,\n );\n reject(this.calls[id].error);\n }, timeout);\n\n this.calls[id] = {\n method,\n args,\n response: null,\n callTime: 0,\n responseTime: 0,\n callback: (error: Error | undefined, response: any) => {\n clearTimeout(timeoutId);\n if (error) {\n reject(error);\n } else {\n resolve(response);\n }\n },\n };\n\n this.emitCall(id);\n });\n }\n\n // do NOT restart after close\n async close() {\n this.listeningTimeoutId && clearTimeout(this.listeningTimeoutId);\n this.connectionTipTimer && clearTimeout(this.connectionTipTimer);\n const closeProcess = this.io?.close();\n this.io = null;\n\n return closeProcess;\n }\n}\n"],"names":["killRunningServer","port","host","client","ClientIO","DefaultBridgeServerPort","BridgeSignalKill","sleep","e","BridgeServer","opts","timeout","Promise","resolve","reject","Error","setTimeout","BridgeErrorCodeNoClientConnected","logMsg","httpServer","createServer","err","Server","socket","next","url","console","clearTimeout","BridgeEvent","clientVersion","params","id","response","error","reason","call","errorMessage","payload","__VERSION__","String","undefined","Date","message","method","args","BridgeCallTimeout","timeoutId","closeProcess","onConnect","onDisconnect","closeConflictServer"],"mappings":";;;;;;;;;;;;;;;;AAmBO,MAAMA,oBAAoB,OAAOC,MAAeC,OAAO,WAAW;IACvE,IAAI;QACF,MAAMC,SAASC,GAAS,CAAC,KAAK,EAAEF,KAAK,CAAC,EAAED,QAAQI,yBAAyB,EAAE;YACzE,OAAO;gBACL,CAACC,iBAAiB,EAAE;YACtB;QACF;QACA,MAAMC,MAAM;QACZ,MAAMJ,OAAO,KAAK;IACpB,EAAE,OAAOK,GAAG,CAEZ;AACF;AAGO,MAAMC;IAoBX,MAAM,OACJC,OAEI,CAAC,CAAC,EACS;QACf,MAAM,EAAEC,UAAU,KAAK,EAAE,GAAGD;QAE5B,IAAI,IAAI,CAAC,mBAAmB,EAC1B,MAAMV,kBAAkB,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,IAAI;QAG9C,OAAO,IAAIY,QAAQ,CAACC,SAASC;YAC3B,IAAI,IAAI,CAAC,kBAAkB,EACzB,OAAOA,OAAO,IAAIC,MA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,kBAAkB;QAC/D,IAAI,CAAC,kBAAkB,IAAIA,aAAa,IAAI,CAAC,kBAAkB;QAC/D,MAAMoB,eAAe,IAAI,CAAC,EAAE,EAAE;QAC9B,IAAI,CAAC,EAAE,GAAG;QAEV,OAAOA;IACT;IA7RA,YACS7C,IAAY,EACZD,IAAY,EACZ+C,SAAsB,EACtBC,YAAuC,EACvCC,mBAA6B,CACpC;;;;;;QAjBF,uBAAQ,UAAR;QACA,uBAAQ,MAAR;QACA,uBAAQ,UAAR;QACA,uBAAQ,sBAAR;QACA,uBAAQ,sBAAR;QACA,uBAAQ,sBAAR;QACA,uBAAO,SAAP;QAEA,uBAAQ,kBAAR;QACA,uBAAQ,wBAAR;QAiMA,uBAAQ,0BAAR;aA9LShD,IAAI,GAAJA;aACAD,IAAI,GAAJA;aACA+C,SAAS,GAATA;aACAC,YAAY,GAAZA;aACAC,mBAAmB,GAAnBA;aAhBD,MAAM,GAAG;aACT,EAAE,GAAkB;aACpB,MAAM,GAAwB;aAC9B,kBAAkB,GAA0B;aAC5C,kBAAkB,GAAG;aACrB,kBAAkB,GAA0B;aAC7C,KAAK,GAA+B,CAAC;aAEpC,cAAc,GAAG;aACjB,oBAAoB,GAAG;aAiMvB,sBAAsB,GAAG,IACxB,CAAC,yBAAyB,EAAE,IAAI,CAAC,oBAAoB,EAAE;IA1L7D;AAwRL"}
1
+ {"version":3,"file":"bridge-mode/io-server.mjs","sources":["../../../src/bridge-mode/io-server.ts"],"sourcesContent":["import { createServer } from 'node:http';\nimport { sleep } from '@midscene/core/utils';\nimport { logMsg } from '@midscene/shared/utils';\nimport { Server, type Socket as ServerSocket } from 'socket.io';\nimport { io as ClientIO } from 'socket.io-client';\n\nimport {\n type BridgeCall,\n type BridgeCallResponse,\n BridgeCallTimeout,\n type BridgeConnectedEventPayload,\n BridgeErrorCodeNoClientConnected,\n BridgeEvent,\n BridgeSignalKill,\n DefaultBridgeServerPort,\n} from './common';\n\ndeclare const __VERSION__: string;\n\nexport const killRunningServer = async (port?: number, host = 'localhost') => {\n try {\n const client = ClientIO(`ws://${host}:${port || DefaultBridgeServerPort}`, {\n query: {\n [BridgeSignalKill]: 1,\n },\n });\n await sleep(300);\n await client.close();\n } catch (e) {\n // console.error('failed to kill port', e);\n }\n};\n\n// ws server, this is where the request is sent\nexport class BridgeServer {\n private callId = 0;\n private io: Server | null = null;\n private socket: ServerSocket | null = null;\n private listeningTimeoutId: NodeJS.Timeout | null = null;\n private listeningTimerFlag = false;\n private connectionTipTimer: NodeJS.Timeout | null = null;\n public calls: Record<string, BridgeCall> = {};\n\n private connectionLost = false;\n private connectionLostReason = '';\n\n constructor(\n public host: string,\n public port: number,\n public onConnect?: () => void,\n public onDisconnect?: (reason: string) => void,\n public closeConflictServer?: boolean,\n ) {}\n\n async listen(\n opts: {\n timeout?: number | false;\n } = {},\n ): Promise<void> {\n const { timeout = 30000 } = opts;\n\n if (this.closeConflictServer) {\n await killRunningServer(this.port, this.host);\n }\n\n return new Promise((resolve, reject) => {\n if (this.listeningTimerFlag) {\n return reject(new Error('already listening'));\n }\n this.listeningTimerFlag = true;\n\n this.listeningTimeoutId = timeout\n ? setTimeout(() => {\n reject(\n new Error(\n `no extension connected after ${timeout}ms (${BridgeErrorCodeNoClientConnected})`,\n ),\n );\n }, timeout)\n : null;\n\n this.connectionTipTimer =\n !timeout || timeout > 3000\n ? setTimeout(() => {\n logMsg('waiting for bridge to connect...');\n }, 2000)\n : null;\n\n // Create HTTP server and start listening on the specified host and port\n const httpServer = createServer();\n\n // Set up HTTP server event listeners FIRST\n httpServer.once('listening', () => {\n resolve();\n });\n\n httpServer.once('error', (err: Error) => {\n reject(new Error(`Bridge Listening Error: ${err.message}`));\n });\n\n // Start listening BEFORE creating Socket.IO Server\n // When host is 127.0.0.1 (default), don't specify host to listen on all local interfaces (IPv4 + IPv6)\n // This ensures localhost resolves correctly in both IPv4 and IPv6 environments\n if (this.host === '127.0.0.1') {\n httpServer.listen(this.port);\n } else {\n httpServer.listen(this.port, this.host);\n }\n\n // Now create Socket.IO Server attached to the already-listening HTTP server\n this.io = new Server(httpServer, {\n maxHttpBufferSize: 100 * 1024 * 1024, // 100MB\n // Increase pingTimeout to tolerate Chrome MV3 Service Worker suspension.\n // The SW keepalive alarm fires every ~24s; default pingTimeout (20s) may\n // be too short if the SW is suspended between alarm pings.\n pingTimeout: 60000,\n });\n\n this.io.use((socket, next) => {\n // Always allow kill signal connections through\n if (socket.handshake.url.includes(BridgeSignalKill)) {\n return next();\n }\n // Allow new connections to replace old ones (reconnection after\n // extension Stop→Start). If the old socket is already disconnected\n // or unresponsive, accept the new connection immediately.\n if (this.socket?.connected) {\n return next(new Error('server already connected by another client'));\n }\n next();\n });\n\n this.io.on('connection', (socket) => {\n // check the connection url\n const url = socket.handshake.url;\n if (url.includes(BridgeSignalKill)) {\n console.warn('kill signal received, closing bridge server');\n return this.close();\n }\n\n this.connectionLost = false;\n this.connectionLostReason = '';\n this.listeningTimeoutId && clearTimeout(this.listeningTimeoutId);\n this.listeningTimeoutId = null;\n this.connectionTipTimer && clearTimeout(this.connectionTipTimer);\n this.connectionTipTimer = null;\n if (this.socket?.connected) {\n socket.emit(BridgeEvent.Refused);\n socket.disconnect();\n logMsg(\n 'refused new connection: server already connected by another client',\n );\n return;\n }\n\n // Clean up stale old socket if it exists but is no longer connected\n if (this.socket) {\n try {\n this.socket.disconnect();\n } catch (e) {\n logMsg(`failed to disconnect stale socket: ${e}`);\n }\n this.socket = null;\n }\n\n try {\n logMsg('one client connected');\n this.socket = socket;\n\n const clientVersion = socket.handshake.query.version;\n logMsg(\n `Bridge connected, cli-side version v${__VERSION__}, browser-side version v${clientVersion}`,\n );\n\n socket.on(BridgeEvent.CallResponse, (params: BridgeCallResponse) => {\n const id = params.id;\n const response = params.response;\n const error = params.error;\n\n this.triggerCallResponseCallback(id, error, response);\n });\n\n socket.on('disconnect', (reason: string) => {\n this.connectionLost = true;\n this.connectionLostReason = reason;\n this.socket = null;\n\n // flush all pending calls as error and clean up completed calls\n for (const id in this.calls) {\n const call = this.calls[id];\n\n if (!call.responseTime) {\n const errorMessage = this.connectionLostErrorMsg();\n this.triggerCallResponseCallback(\n id,\n new Error(errorMessage),\n null,\n );\n }\n }\n\n // Clean up completed calls to prevent memory leaks in long-running sessions\n for (const id in this.calls) {\n if (this.calls[id].responseTime) {\n delete this.calls[id];\n }\n }\n\n this.onDisconnect?.(reason);\n });\n\n setTimeout(() => {\n this.onConnect?.();\n\n const payload = {\n version: __VERSION__,\n } as BridgeConnectedEventPayload;\n socket.emit(BridgeEvent.Connected, payload);\n Promise.resolve().then(() => {\n for (const id in this.calls) {\n if (this.calls[id].callTime === 0) {\n this.emitCall(id);\n }\n }\n });\n }, 0);\n } catch (e) {\n logMsg(`failed to handle connection event: ${e}`);\n }\n });\n\n this.io.on('close', () => {\n this.close();\n });\n });\n }\n\n private connectionLostErrorMsg = () => {\n return `Connection lost, reason: ${this.connectionLostReason}`;\n };\n\n private async triggerCallResponseCallback(\n id: string | number,\n error: Error | string | null,\n response: any,\n ) {\n const call = this.calls[id];\n if (!call) {\n throw new Error(`call ${id} not found`);\n }\n // Ensure error is always an Error object (bridge client may send strings)\n if (error) {\n call.error =\n error instanceof Error\n ? error\n : new Error(typeof error === 'string' ? error : String(error));\n } else {\n call.error = undefined;\n }\n call.response = response;\n call.responseTime = Date.now();\n\n call.callback(call.error, response);\n }\n\n private async emitCall(id: string) {\n const call = this.calls[id];\n if (!call) {\n throw new Error(`call ${id} not found`);\n }\n\n if (this.connectionLost) {\n const message = `Connection lost, reason: ${this.connectionLostReason}`;\n call.callback(new Error(message), null);\n return;\n }\n\n if (this.socket) {\n this.socket.emit(BridgeEvent.Call, {\n id,\n method: call.method,\n args: call.args,\n });\n call.callTime = Date.now();\n }\n }\n\n async call<T = any>(\n method: string,\n args: any[],\n timeout = BridgeCallTimeout,\n ): Promise<T> {\n const id = `${this.callId++}`;\n\n return new Promise((resolve, reject) => {\n const timeoutId = setTimeout(() => {\n logMsg(`bridge call timeout, id=${id}, method=${method}, args=`, args);\n this.calls[id].error = new Error(\n `Bridge call timeout after ${timeout}ms: ${method}`,\n );\n reject(this.calls[id].error);\n }, timeout);\n\n this.calls[id] = {\n method,\n args,\n response: null,\n callTime: 0,\n responseTime: 0,\n callback: (error: Error | undefined, response: any) => {\n clearTimeout(timeoutId);\n if (error) {\n reject(error);\n } else {\n resolve(response);\n }\n },\n };\n\n this.emitCall(id);\n });\n }\n\n // do NOT restart after close\n async close() {\n this.listeningTimeoutId && clearTimeout(this.listeningTimeoutId);\n this.connectionTipTimer && clearTimeout(this.connectionTipTimer);\n const closeProcess = this.io?.close();\n this.io = null;\n\n return closeProcess;\n }\n}\n"],"names":["killRunningServer","port","host","client","ClientIO","DefaultBridgeServerPort","BridgeSignalKill","sleep","e","BridgeServer","opts","timeout","Promise","resolve","reject","Error","setTimeout","BridgeErrorCodeNoClientConnected","logMsg","httpServer","createServer","err","Server","socket","next","url","console","clearTimeout","BridgeEvent","clientVersion","params","id","response","error","reason","call","errorMessage","payload","__VERSION__","String","undefined","Date","message","method","args","BridgeCallTimeout","timeoutId","closeProcess","onConnect","onDisconnect","closeConflictServer"],"mappings":";;;;;;;;;;;;;;;;AAmBO,MAAMA,oBAAoB,OAAOC,MAAeC,OAAO,WAAW;IACvE,IAAI;QACF,MAAMC,SAASC,GAAS,CAAC,KAAK,EAAEF,KAAK,CAAC,EAAED,QAAQI,yBAAyB,EAAE;YACzE,OAAO;gBACL,CAACC,iBAAiB,EAAE;YACtB;QACF;QACA,MAAMC,MAAM;QACZ,MAAMJ,OAAO,KAAK;IACpB,EAAE,OAAOK,GAAG,CAEZ;AACF;AAGO,MAAMC;IAoBX,MAAM,OACJC,OAEI,CAAC,CAAC,EACS;QACf,MAAM,EAAEC,UAAU,KAAK,EAAE,GAAGD;QAE5B,IAAI,IAAI,CAAC,mBAAmB,EAC1B,MAAMV,kBAAkB,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,IAAI;QAG9C,OAAO,IAA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ACD;QAChB;IACF;IAGA,MAAM,QAAQ;QACZ,IAAI,CAAC,kBAAkB,IAAIJ,aAAa,IAAI,CAAC,kBAAkB;QAC/D,IAAI,CAAC,kBAAkB,IAAIA,aAAa,IAAI,CAAC,kBAAkB;QAC/D,MAAMoB,eAAe,IAAI,CAAC,EAAE,EAAE;QAC9B,IAAI,CAAC,EAAE,GAAG;QAEV,OAAOA;IACT;IA7RA,YACS7C,IAAY,EACZD,IAAY,EACZ+C,SAAsB,EACtBC,YAAuC,EACvCC,mBAA6B,CACpC;;;;;;QAjBF,uBAAQ,UAAR;QACA,uBAAQ,MAAR;QACA,uBAAQ,UAAR;QACA,uBAAQ,sBAAR;QACA,uBAAQ,sBAAR;QACA,uBAAQ,sBAAR;QACA,uBAAO,SAAP;QAEA,uBAAQ,kBAAR;QACA,uBAAQ,wBAAR;QAiMA,uBAAQ,0BAAR;aA9LShD,IAAI,GAAJA;aACAD,IAAI,GAAJA;aACA+C,SAAS,GAATA;aACAC,YAAY,GAAZA;aACAC,mBAAmB,GAAnBA;aAhBD,MAAM,GAAG;aACT,EAAE,GAAkB;aACpB,MAAM,GAAwB;aAC9B,kBAAkB,GAA0B;aAC5C,kBAAkB,GAAG;aACrB,kBAAkB,GAA0B;aAC7C,KAAK,GAA+B,CAAC;aAEpC,cAAc,GAAG;aACjB,oBAAoB,GAAG;aAiMvB,sBAAsB,GAAG,IACxB,CAAC,yBAAyB,EAAE,IAAI,CAAC,oBAAoB,EAAE;IA1L7D;AAwRL"}
@@ -65,7 +65,7 @@ class ExtensionBridgePageBrowserSide extends page {
65
65
  throw new Error('Connection denied by user');
66
66
  }
67
67
  }
68
- this.onLogMessage(`Bridge connected, cli-side version v${this.bridgeClient.serverVersion}, browser-side version v1.8.1-beta-20260513084557.0`, 'log');
68
+ this.onLogMessage(`Bridge connected, cli-side version v${this.bridgeClient.serverVersion}, browser-side version v1.8.1`, 'log');
69
69
  }
70
70
  async connect() {
71
71
  return await this.setupBridgeClient();
@@ -1 +1 @@
1
- {"version":3,"file":"bridge-mode/page-browser-side.mjs","sources":["../../../src/bridge-mode/page-browser-side.ts"],"sourcesContent":["import { assert } from '@midscene/shared/utils';\nimport ChromeExtensionProxyPage from '../chrome-extension/page';\nimport type {\n ChromePageDestroyOptions,\n KeyboardAction,\n MouseAction,\n} from '../web-page';\nimport {\n type BridgeConnectTabOptions,\n BridgeEvent,\n DefaultBridgeServerPort,\n KeyboardEvent,\n MouseEvent,\n} from './common';\nimport { BridgeClient } from './io-client';\n\ndeclare const __VERSION__: string;\n\nexport class ExtensionBridgePageBrowserSide extends ChromeExtensionProxyPage {\n public bridgeClient: BridgeClient | null = null;\n\n private destroyOptions?: ChromePageDestroyOptions;\n\n private newlyCreatedTabIds: number[] = [];\n\n // Connection confirmation state\n private confirmationPromise: Promise<boolean> | null = null;\n\n constructor(\n public serverEndpoint?: string,\n public onDisconnect: () => void = () => {},\n public onLogMessage: (\n message: string,\n type: 'log' | 'status',\n ) => void = () => {},\n forceSameTabNavigation = true,\n public onConnectionRequest?: () => Promise<boolean>,\n ) {\n super(forceSameTabNavigation);\n }\n\n private async setupBridgeClient() {\n const endpoint =\n this.serverEndpoint || `ws://localhost:${DefaultBridgeServerPort}`;\n\n // Create confirmation gate BEFORE establishing connection,\n // so that any calls received immediately after connection are blocked\n // until user confirms. This prevents a race condition where server-side\n // queued calls bypass the confirmation dialog.\n let resolveConfirmationGate: (allowed: boolean) => void = () => {};\n if (this.onConnectionRequest) {\n this.confirmationPromise = new Promise<boolean>((resolve) => {\n resolveConfirmationGate = resolve;\n });\n }\n\n this.bridgeClient = new BridgeClient(\n endpoint,\n async (method, args: any[]) => {\n // Wait for user confirmation before processing any commands\n if (this.confirmationPromise) {\n const allowed = await this.confirmationPromise;\n if (!allowed) {\n throw new Error('Connection denied by user');\n }\n }\n\n this.onLogMessage(`bridge call from cli side: ${method}`, 'log');\n if (method === BridgeEvent.ConnectNewTabWithUrl) {\n return this.connectNewTabWithUrl.apply(\n this,\n args as unknown as [string],\n );\n }\n\n if (method === BridgeEvent.GetBrowserTabList) {\n return this.getBrowserTabList.apply(this, args as any);\n }\n\n if (method === BridgeEvent.SetActiveTabId) {\n return this.setActiveTabId.apply(this, args as any);\n }\n\n if (method === BridgeEvent.ConnectCurrentTab) {\n return this.connectCurrentTab.apply(this, args as any);\n }\n\n if (method === BridgeEvent.UpdateAgentStatus) {\n return this.onLogMessage(args[0] as string, 'status');\n }\n\n const tabId = await this.getActiveTabId();\n if (!tabId || tabId === 0) {\n throw new Error('no tab is connected');\n }\n\n // this.onLogMessage(`calling method: ${method}`);\n\n if (method.startsWith(MouseEvent.PREFIX)) {\n const actionName = method.split('.')[1] as keyof MouseAction;\n if (actionName === 'drag') {\n return this.mouse[actionName].apply(this.mouse, args as any);\n }\n return this.mouse[actionName].apply(this.mouse, args as any);\n }\n\n if (method.startsWith(KeyboardEvent.PREFIX)) {\n const actionName = method.split('.')[1] as keyof KeyboardAction;\n if (actionName === 'press') {\n return this.keyboard[actionName].apply(this.keyboard, args as any);\n }\n return this.keyboard[actionName].apply(this.keyboard, args as any);\n }\n\n if (!this[method as keyof ChromeExtensionProxyPage]) {\n this.onLogMessage(`method not found: ${method}`, 'log');\n return undefined;\n }\n\n try {\n // @ts-expect-error\n const result = await this[method as keyof ChromeExtensionProxyPage](\n ...args,\n );\n return result;\n } catch (e) {\n const errorMessage = e instanceof Error ? e.message : 'Unknown error';\n this.onLogMessage(\n `Error calling method: ${method}, ${errorMessage}`,\n 'log',\n );\n throw new Error(errorMessage, { cause: e });\n }\n },\n // on disconnect\n () => {\n return this.destroy();\n },\n );\n await this.bridgeClient.connect();\n\n // Show confirmation dialog after connection is established\n if (this.onConnectionRequest) {\n this.onLogMessage('Waiting for user confirmation...', 'log');\n const allowed = await this.onConnectionRequest();\n resolveConfirmationGate(allowed);\n this.confirmationPromise = null;\n\n if (!allowed) {\n this.onLogMessage('Connection denied by user', 'log');\n this.bridgeClient.disconnect();\n this.bridgeClient = null;\n throw new Error('Connection denied by user');\n }\n }\n\n this.onLogMessage(\n `Bridge connected, cli-side version v${this.bridgeClient.serverVersion}, browser-side version v${__VERSION__}`,\n 'log',\n );\n }\n\n public async connect() {\n return await this.setupBridgeClient();\n }\n\n public async connectNewTabWithUrl(\n url: string,\n options: BridgeConnectTabOptions = {\n forceSameTabNavigation: true,\n },\n ) {\n const tab = await chrome.tabs.create({ url });\n const tabId = tab.id;\n assert(tabId, 'failed to get tabId after creating a new tab');\n\n // new tab\n this.onLogMessage(`Creating new tab: ${url}`, 'log');\n this.newlyCreatedTabIds.push(tabId);\n\n if (options?.forceSameTabNavigation) {\n this.forceSameTabNavigation = true;\n }\n\n await this.setActiveTabId(tabId);\n }\n\n public async connectCurrentTab(\n options: BridgeConnectTabOptions = {\n forceSameTabNavigation: true,\n },\n ) {\n const tabs = await chrome.tabs.query({ active: true, currentWindow: true });\n const tabId = tabs[0]?.id;\n assert(tabId, 'failed to get tabId');\n\n this.onLogMessage(`Connected to current tab: ${tabs[0]?.url}`, 'log');\n\n if (options?.forceSameTabNavigation) {\n this.forceSameTabNavigation = true;\n }\n\n await this.setActiveTabId(tabId);\n }\n\n public async setDestroyOptions(options: ChromePageDestroyOptions) {\n this.destroyOptions = options;\n }\n\n async destroy() {\n if (this.destroyOptions?.closeTab && this.newlyCreatedTabIds.length > 0) {\n this.onLogMessage('Closing all newly created tabs by bridge...', 'log');\n for (const tabId of this.newlyCreatedTabIds) {\n await chrome.tabs.remove(tabId);\n }\n this.newlyCreatedTabIds = [];\n }\n\n await super.destroy();\n\n if (this.bridgeClient) {\n this.bridgeClient.disconnect();\n this.bridgeClient = null;\n this.onDisconnect();\n }\n }\n}\n"],"names":["ExtensionBridgePageBrowserSide","ChromeExtensionProxyPage","endpoint","DefaultBridgeServerPort","resolveConfirmationGate","Promise","resolve","BridgeClient","method","args","allowed","Error","BridgeEvent","tabId","MouseEvent","actionName","KeyboardEvent","result","e","errorMessage","url","options","tab","chrome","assert","tabs","serverEndpoint","onDisconnect","onL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AAC,kBAAkB,GAAG,EAAE;QAC9B;QAEA,MAAM,KAAK,CAAC;QAEZ,IAAI,IAAI,CAAC,YAAY,EAAE;YACrB,IAAI,CAAC,YAAY,CAAC,UAAU;YAC5B,IAAI,CAAC,YAAY,GAAG;YACpB,IAAI,CAAC,YAAY;QACnB;IACF;IArMA,YACSa,cAAuB,EACvBC,eAA2B,KAAO,CAAC,EACnCC,eAGK,KAAO,CAAC,EACpBC,yBAAyB,IAAI,EACtBC,mBAA4C,CACnD;QACA,KAAK,CAACD,yBAAAA,iBAAAA,IAAAA,EAAAA,kBAAAA,KAAAA,IAAAA,iBAAAA,IAAAA,EAAAA,gBAAAA,KAAAA,IAAAA,iBAAAA,IAAAA,EAAAA,gBAAAA,KAAAA,IAAAA,iBAAAA,IAAAA,EAAAA,uBAAAA,KAAAA,IAnBR,uBAAO,gBAAP,SAEA,uBAAQ,kBAAR,SAEA,uBAAQ,sBAAR,SAGA,uBAAQ,uBAAR,cAGSH,cAAc,GAAdA,gBAAAA,IAAAA,CACAC,YAAY,GAAZA,cAAAA,IAAAA,CACAC,YAAY,GAAZA,cAAAA,IAAAA,CAKAE,mBAAmB,GAAnBA,qBAAAA,IAAAA,CAjBF,YAAY,GAAwB,WAInC,kBAAkB,GAAa,EAAE,OAGjC,mBAAmB,GAA4B;IAavD;AA2LF"}
1
+ {"version":3,"file":"bridge-mode/page-browser-side.mjs","sources":["../../../src/bridge-mode/page-browser-side.ts"],"sourcesContent":["import { assert } from '@midscene/shared/utils';\nimport ChromeExtensionProxyPage from '../chrome-extension/page';\nimport type {\n ChromePageDestroyOptions,\n KeyboardAction,\n MouseAction,\n} from '../web-page';\nimport {\n type BridgeConnectTabOptions,\n BridgeEvent,\n DefaultBridgeServerPort,\n KeyboardEvent,\n MouseEvent,\n} from './common';\nimport { BridgeClient } from './io-client';\n\ndeclare const __VERSION__: string;\n\nexport class ExtensionBridgePageBrowserSide extends ChromeExtensionProxyPage {\n public bridgeClient: BridgeClient | null = null;\n\n private destroyOptions?: ChromePageDestroyOptions;\n\n private newlyCreatedTabIds: number[] = [];\n\n // Connection confirmation state\n private confirmationPromise: Promise<boolean> | null = null;\n\n constructor(\n public serverEndpoint?: string,\n public onDisconnect: () => void = () => {},\n public onLogMessage: (\n message: string,\n type: 'log' | 'status',\n ) => void = () => {},\n forceSameTabNavigation = true,\n public onConnectionRequest?: () => Promise<boolean>,\n ) {\n super(forceSameTabNavigation);\n }\n\n private async setupBridgeClient() {\n const endpoint =\n this.serverEndpoint || `ws://localhost:${DefaultBridgeServerPort}`;\n\n // Create confirmation gate BEFORE establishing connection,\n // so that any calls received immediately after connection are blocked\n // until user confirms. This prevents a race condition where server-side\n // queued calls bypass the confirmation dialog.\n let resolveConfirmationGate: (allowed: boolean) => void = () => {};\n if (this.onConnectionRequest) {\n this.confirmationPromise = new Promise<boolean>((resolve) => {\n resolveConfirmationGate = resolve;\n });\n }\n\n this.bridgeClient = new BridgeClient(\n endpoint,\n async (method, args: any[]) => {\n // Wait for user confirmation before processing any commands\n if (this.confirmationPromise) {\n const allowed = await this.confirmationPromise;\n if (!allowed) {\n throw new Error('Connection denied by user');\n }\n }\n\n this.onLogMessage(`bridge call from cli side: ${method}`, 'log');\n if (method === BridgeEvent.ConnectNewTabWithUrl) {\n return this.connectNewTabWithUrl.apply(\n this,\n args as unknown as [string],\n );\n }\n\n if (method === BridgeEvent.GetBrowserTabList) {\n return this.getBrowserTabList.apply(this, args as any);\n }\n\n if (method === BridgeEvent.SetActiveTabId) {\n return this.setActiveTabId.apply(this, args as any);\n }\n\n if (method === BridgeEvent.ConnectCurrentTab) {\n return this.connectCurrentTab.apply(this, args as any);\n }\n\n if (method === BridgeEvent.UpdateAgentStatus) {\n return this.onLogMessage(args[0] as string, 'status');\n }\n\n const tabId = await this.getActiveTabId();\n if (!tabId || tabId === 0) {\n throw new Error('no tab is connected');\n }\n\n // this.onLogMessage(`calling method: ${method}`);\n\n if (method.startsWith(MouseEvent.PREFIX)) {\n const actionName = method.split('.')[1] as keyof MouseAction;\n if (actionName === 'drag') {\n return this.mouse[actionName].apply(this.mouse, args as any);\n }\n return this.mouse[actionName].apply(this.mouse, args as any);\n }\n\n if (method.startsWith(KeyboardEvent.PREFIX)) {\n const actionName = method.split('.')[1] as keyof KeyboardAction;\n if (actionName === 'press') {\n return this.keyboard[actionName].apply(this.keyboard, args as any);\n }\n return this.keyboard[actionName].apply(this.keyboard, args as any);\n }\n\n if (!this[method as keyof ChromeExtensionProxyPage]) {\n this.onLogMessage(`method not found: ${method}`, 'log');\n return undefined;\n }\n\n try {\n // @ts-expect-error\n const result = await this[method as keyof ChromeExtensionProxyPage](\n ...args,\n );\n return result;\n } catch (e) {\n const errorMessage = e instanceof Error ? e.message : 'Unknown error';\n this.onLogMessage(\n `Error calling method: ${method}, ${errorMessage}`,\n 'log',\n );\n throw new Error(errorMessage, { cause: e });\n }\n },\n // on disconnect\n () => {\n return this.destroy();\n },\n );\n await this.bridgeClient.connect();\n\n // Show confirmation dialog after connection is established\n if (this.onConnectionRequest) {\n this.onLogMessage('Waiting for user confirmation...', 'log');\n const allowed = await this.onConnectionRequest();\n resolveConfirmationGate(allowed);\n this.confirmationPromise = null;\n\n if (!allowed) {\n this.onLogMessage('Connection denied by user', 'log');\n this.bridgeClient.disconnect();\n this.bridgeClient = null;\n throw new Error('Connection denied by user');\n }\n }\n\n this.onLogMessage(\n `Bridge connected, cli-side version v${this.bridgeClient.serverVersion}, browser-side version v${__VERSION__}`,\n 'log',\n );\n }\n\n public async connect() {\n return await this.setupBridgeClient();\n }\n\n public async connectNewTabWithUrl(\n url: string,\n options: BridgeConnectTabOptions = {\n forceSameTabNavigation: true,\n },\n ) {\n const tab = await chrome.tabs.create({ url });\n const tabId = tab.id;\n assert(tabId, 'failed to get tabId after creating a new tab');\n\n // new tab\n this.onLogMessage(`Creating new tab: ${url}`, 'log');\n this.newlyCreatedTabIds.push(tabId);\n\n if (options?.forceSameTabNavigation) {\n this.forceSameTabNavigation = true;\n }\n\n await this.setActiveTabId(tabId);\n }\n\n public async connectCurrentTab(\n options: BridgeConnectTabOptions = {\n forceSameTabNavigation: true,\n },\n ) {\n const tabs = await chrome.tabs.query({ active: true, currentWindow: true });\n const tabId = tabs[0]?.id;\n assert(tabId, 'failed to get tabId');\n\n this.onLogMessage(`Connected to current tab: ${tabs[0]?.url}`, 'log');\n\n if (options?.forceSameTabNavigation) {\n this.forceSameTabNavigation = true;\n }\n\n await this.setActiveTabId(tabId);\n }\n\n public async setDestroyOptions(options: ChromePageDestroyOptions) {\n this.destroyOptions = options;\n }\n\n async destroy() {\n if (this.destroyOptions?.closeTab && this.newlyCreatedTabIds.length > 0) {\n this.onLogMessage('Closing all newly created tabs by bridge...', 'log');\n for (const tabId of this.newlyCreatedTabIds) {\n await chrome.tabs.remove(tabId);\n }\n this.newlyCreatedTabIds = [];\n }\n\n await super.destroy();\n\n if (this.bridgeClient) {\n this.bridgeClient.disconnect();\n this.bridgeClient = null;\n this.onDisconnect();\n }\n }\n}\n"],"names":["ExtensionBridgePageBrowserSide","ChromeExtensionProxyPage","endpoint","DefaultBridgeServerPort","resolveConfirmationGate","Promise","resolve","BridgeClient","method","args","allowed","Error","BridgeEvent","tabId","MouseEvent","actionName","KeyboardEvent","result","e","errorMessage","url","options","tab","chrome","assert","tabs","serverEndpoint","onDisconnect","onLogMessage","forceSameTabNavigation","onConnectionRequest"],"mappings":";;;;;;;;;;;;;;AAkBO,MAAMA,uCAAuCC;IAuBlD,MAAc,oBAAoB;QAChC,MAAMC,WACJ,IAAI,CAAC,cAAc,IAAI,CAAC,eAAe,EAAEC,yBAAyB;QAMpE,IAAIC,0BAAsD,KAAO;QACjE,IAAI,IAAI,CAAC,mBAAmB,EAC1B,IAAI,CAAC,mBAAmB,GAAG,IAAIC,QAAiB,CAACC;YAC/CF,0BAA0BE;QAC5B;QAGF,IAAI,CAAC,YAAY,GAAG,IAAIC,aACtBL,UACA,OAAOM,QAAQC;YAEb,IAAI,IAAI,CAAC,mBAAmB,E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A,KAAAA,IAAAA,iBAAAA,IAAAA,EAAAA,gBAAAA,KAAAA,IAAAA,iBAAAA,IAAAA,EAAAA,uBAAAA,KAAAA,IAnBR,uBAAO,gBAAP,SAEA,uBAAQ,kBAAR,SAEA,uBAAQ,sBAAR,SAGA,uBAAQ,uBAAR,cAGSH,cAAc,GAAdA,gBAAAA,IAAAA,CACAC,YAAY,GAAZA,cAAAA,IAAAA,CACAC,YAAY,GAAZA,cAAAA,IAAAA,CAKAE,mBAAmB,GAAnBA,qBAAAA,IAAAA,CAjBF,YAAY,GAAwB,WAInC,kBAAkB,GAAa,EAAE,OAGjC,mBAAmB,GAA4B;IAavD;AA2LF"}
package/dist/es/cli.mjs CHANGED
@@ -18,7 +18,7 @@ Promise.resolve().then(()=>{
18
18
  return runToolsCLI(tools, 'midscene-web', {
19
19
  stripPrefix: 'web_',
20
20
  argv: parsedOptions.argv,
21
- version: "1.8.1-beta-20260513084557.0",
21
+ version: "1.8.1",
22
22
  extraCommands: createReportCliCommands()
23
23
  });
24
24
  }).catch((e)=>{
@@ -7,7 +7,7 @@ class WebMCPServer extends BaseMCPServer {
7
7
  constructor(toolsManager){
8
8
  super({
9
9
  name: '@midscene/web-bridge-mcp',
10
- version: "1.8.1-beta-20260513084557.0",
10
+ version: "1.8.1",
11
11
  description: 'Control the browser using natural language commands'
12
12
  }, toolsManager);
13
13
  }
@@ -1,4 +1,4 @@
1
- import { defineActionDragAndDrop, defineActionHover, defineActionInput, defineActionKeyboardPress, defineActionRightClick, defineActionScroll, defineActionSwipe, defineActionTap } from "@midscene/core/device";
1
+ import { defineActionsFromInputPrimitives } from "@midscene/core/device";
2
2
  import { ERROR_CODE_NOT_IMPLEMENTED_AS_DESIGNED } from "@midscene/shared/common";
3
3
  function _define_property(obj, key, value) {
4
4
  if (key in obj) Object.defineProperty(obj, key, {
@@ -15,32 +15,9 @@ const ThrowNotImplemented = (methodName)=>{
15
15
  };
16
16
  class StaticPage {
17
17
  actionSpace() {
18
- return [
19
- defineActionTap(async (param)=>{
20
- ThrowNotImplemented('Tap');
21
- }),
22
- defineActionRightClick(async (param)=>{
23
- ThrowNotImplemented('RightClick');
24
- }),
25
- defineActionHover(async (param)=>{
26
- ThrowNotImplemented('Hover');
27
- }),
28
- defineActionInput(async (param)=>{
29
- ThrowNotImplemented('Input');
30
- }),
31
- defineActionKeyboardPress(async (param)=>{
32
- ThrowNotImplemented('KeyboardPress');
33
- }),
34
- defineActionScroll(async (param)=>{
35
- ThrowNotImplemented('Scroll');
36
- }),
37
- defineActionDragAndDrop(async (param)=>{
38
- ThrowNotImplemented('DragAndDrop');
39
- }),
40
- defineActionSwipe(async (param)=>{
41
- ThrowNotImplemented('Swipe');
42
- })
43
- ];
18
+ return defineActionsFromInputPrimitives(this.inputPrimitives, {
19
+ size: ()=>this.size()
20
+ });
44
21
  }
45
22
  async evaluateJavaScript(script) {
46
23
  return ThrowNotImplemented('evaluateJavaScript');
@@ -104,6 +81,25 @@ class StaticPage {
104
81
  constructor(uiContext){
105
82
  _define_property(this, "interfaceType", 'static');
106
83
  _define_property(this, "uiContext", void 0);
84
+ _define_property(this, "inputPrimitives", {
85
+ pointer: {
86
+ tap: async ()=>ThrowNotImplemented('Tap'),
87
+ rightClick: async ()=>ThrowNotImplemented('RightClick'),
88
+ hover: async ()=>ThrowNotImplemented('Hover'),
89
+ dragAndDrop: async ()=>ThrowNotImplemented('DragAndDrop')
90
+ },
91
+ keyboard: {
92
+ typeText: async ()=>ThrowNotImplemented('Input'),
93
+ keyboardPress: async ()=>ThrowNotImplemented('KeyboardPress'),
94
+ clearInput: async ()=>ThrowNotImplemented('ClearInput')
95
+ },
96
+ touch: {
97
+ swipe: async ()=>ThrowNotImplemented('Swipe')
98
+ },
99
+ scroll: {
100
+ scroll: async ()=>ThrowNotImplemented('Scroll')
101
+ }
102
+ });
107
103
  _define_property(this, "mouse", {
108
104
  click: ThrowNotImplemented.bind(null, 'mouse.click'),
109
105
  wheel: ThrowNotImplemented.bind(null, 'mouse.wheel'),
@@ -1 +1 @@
1
- {"version":3,"file":"static/static-page.mjs","sources":["../../../src/static/static-page.ts"],"sourcesContent":["import type { DeviceAction, Point, UIContext } from '@midscene/core';\nimport type { AbstractInterface } from '@midscene/core/device';\nimport {\n defineActionDragAndDrop,\n defineActionHover,\n defineActionInput,\n defineActionKeyboardPress,\n defineActionRightClick,\n defineActionScroll,\n defineActionSwipe,\n defineActionTap,\n} from '@midscene/core/device';\nimport { ERROR_CODE_NOT_IMPLEMENTED_AS_DESIGNED } from '@midscene/shared/common';\n\n\nconst ThrowNotImplemented = (methodName: string) => {\n throw new Error(\n `The method \"${methodName}\" is not implemented as designed since this is a static UI context. (${ERROR_CODE_NOT_IMPLEMENTED_AS_DESIGNED})`,\n );\n};\n\ntype StaticPageUIContext = Omit<UIContext, 'deprecatedDpr'>;\n\nexport default class StaticPage implements AbstractInterface {\n interfaceType = 'static';\n\n private uiContext: StaticPageUIContext;\n\n constructor(uiContext: StaticPageUIContext) {\n this.uiContext = uiContext;\n }\n\n actionSpace(): DeviceAction[] {\n // Return available actions for static page - they will throw \"not implemented\" errors when executed\n // but need to be available for planning phase\n return [\n defineActionTap(async (param) => {\n ThrowNotImplemented('Tap');\n }),\n defineActionRightClick(async (param) => {\n ThrowNotImplemented('RightClick');\n }),\n defineActionHover(async (param) => {\n ThrowNotImplemented('Hover');\n }),\n defineActionInput(async (param) => {\n ThrowNotImplemented('Input');\n }),\n defineActionKeyboardPress(async (param) => {\n ThrowNotImplemented('KeyboardPress');\n }),\n defineActionScroll(async (param) => {\n ThrowNotImplemented('Scroll');\n }),\n defineActionDragAndDrop(async (param) => {\n ThrowNotImplemented('DragAndDrop');\n }),\n defineActionSwipe(async (param) => {\n ThrowNotImplemented('Swipe');\n }),\n ];\n }\n\n async evaluateJavaScript<T = unknown>(script: string): Promise<T> {\n return ThrowNotImplemented('evaluateJavaScript');\n }\n\n // @deprecated\n async getElementsInfo() {\n return ThrowNotImplemented('getElementsInfo');\n }\n\n async getElementsNodeTree() {\n return ThrowNotImplemented('getElementsNodeTree');\n }\n\n async getXpathsByPoint(point: Point) {\n return ThrowNotImplemented('getXpathsByPoint');\n }\n\n async getElementInfoByXpath(xpath: string) {\n return ThrowNotImplemented('getElementInfoByXpath');\n }\n\n async size() {\n return {\n ...this.uiContext.shotSize\n };\n }\n\n async screenshotBase64() {\n const screenshot = this.uiContext.screenshot;\n if (typeof screenshot === 'object' && 'base64' in screenshot) {\n return (screenshot as { base64: string }).base64;\n }\n return screenshot as unknown as string;\n }\n\n async url() {\n return Promise.resolve('https://static_page_without_url');\n }\n\n async scrollUntilTop(startingPoint?: Point) {\n return ThrowNotImplemented('scrollUntilTop');\n }\n\n async scrollUntilBottom(startingPoint?: Point) {\n return ThrowNotImplemented('scrollUntilBottom');\n }\n\n async scrollUntilLeft(startingPoint?: Point) {\n return ThrowNotImplemented('scrollUntilLeft');\n }\n\n async scrollUntilRight(startingPoint?: Point) {\n return ThrowNotImplemented('scrollUntilRight');\n }\n\n async scrollUp(distance?: number, startingPoint?: Point) {\n return ThrowNotImplemented('scrollUp');\n }\n\n async scrollDown(distance?: number, startingPoint?: Point) {\n return ThrowNotImplemented('scrollDown');\n }\n\n async scrollLeft(distance?: number, startingPoint?: Point) {\n return ThrowNotImplemented('scrollLeft');\n }\n\n async scrollRight(distance?: number, startingPoint?: Point) {\n return ThrowNotImplemented('scrollRight');\n }\n\n async clearInput() {\n return ThrowNotImplemented('clearInput');\n }\n\n mouse = {\n click: ThrowNotImplemented.bind(null, 'mouse.click'),\n wheel: ThrowNotImplemented.bind(null, 'mouse.wheel'),\n move: ThrowNotImplemented.bind(null, 'mouse.move'),\n drag: ThrowNotImplemented.bind(null, 'mouse.drag'),\n };\n\n keyboard = {\n type: ThrowNotImplemented.bind(null, 'keyboard.type'),\n press: ThrowNotImplemented.bind(null, 'keyboard.press'),\n };\n\n async destroy(): Promise<void> {\n //\n }\n\n updateContext(newContext: StaticPageUIContext): void {\n this.uiContext = newContext;\n }\n}\n"],"names":["ThrowNotImplemented","methodName","Error","ERROR_CODE_NOT_IMPLEMENTED_AS_DESIGNED","StaticPage","defineActionTap","param","defineActionRightClick","defineActionHover","defineActionInput","defineActionKeyboardPress","defineActionScroll","defineActionDragAndDrop","defineActionSwipe","script","point","xpath","screenshot","Promise","startingPoint","distance","newContext","uiContext"],"mappings":";;;;;;;;;;;;AAeA,MAAMA,sBAAsB,CAACC;IAC3B,MAAM,IAAIC,MACR,CAAC,YAAY,EAAED,WAAW,qEAAqE,EAAEE,uCAAuC,CAAC,CAAC;AAE9I;AAIe,MAAMC;IASnB,cAA8B;QAG5B,OAAO;YACLC,gBAAgB,OAAOC;gBACrBN,oBAAoB;YACtB;YACAO,uBAAuB,OAAOD;gBAC5BN,oBAAoB;YACtB;YACAQ,kBAAkB,OAAOF;gBACvBN,oBAAoB;YACtB;YACAS,kBAAkB,OAAOH;gBACvBN,oBAAoB;YACtB;YACAU,0BAA0B,OAAOJ;gBAC/BN,oBAAoB;YACtB;YACAW,mBAAmB,OAAOL;gBACxBN,oBAAoB;YACtB;YACAY,wBAAwB,OAAON;gBAC7BN,oBAAoB;YACtB;YACAa,kBAAkB,OAAOP;gBACvBN,oBAAoB;YACtB;SACD;IACH;IAEA,MAAM,mBAAgCc,MAAc,EAAc;QAChE,OAAOd,oBAAoB;IAC7B;IAGA,MAAM,kBAAkB;QACtB,OAAOA,oBAAoB;IAC7B;IAEA,MAAM,sBAAsB;QAC1B,OAAOA,oBAAoB;IAC7B;IAEA,MAAM,iBAAiBe,KAAY,EAAE;QACnC,OAAOf,oBAAoB;IAC7B;IAEA,MAAM,sBAAsBgB,KAAa,EAAE;QACzC,OAAOhB,oBAAoB;IAC7B;IAEA,MAAM,OAAO;QACX,OAAO;YACL,GAAG,IAAI,CAAC,SAAS,CAAC,QAAQ;QAC5B;IACF;IAEA,MAAM,mBAAmB;QACvB,MAAMiB,aAAa,IAAI,CAAC,SAAS,CAAC,UAAU;QAC5C,IAAI,AAAsB,YAAtB,OAA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version":3,"file":"static/static-page.mjs","sources":["../../../src/static/static-page.ts"],"sourcesContent":["import type { DeviceAction, Point, UIContext } from '@midscene/core';\nimport type { AbstractInterface } from '@midscene/core/device';\nimport {\n type InputPrimitives,\n defineActionsFromInputPrimitives,\n} from '@midscene/core/device';\nimport { ERROR_CODE_NOT_IMPLEMENTED_AS_DESIGNED } from '@midscene/shared/common';\n\nconst ThrowNotImplemented = (methodName: string) => {\n throw new Error(\n `The method \"${methodName}\" is not implemented as designed since this is a static UI context. (${ERROR_CODE_NOT_IMPLEMENTED_AS_DESIGNED})`,\n );\n};\n\ntype StaticPageUIContext = Omit<UIContext, 'deprecatedDpr'>;\n\nexport default class StaticPage implements AbstractInterface {\n interfaceType = 'static';\n\n private uiContext: StaticPageUIContext;\n readonly inputPrimitives: InputPrimitives = {\n pointer: {\n tap: async () => ThrowNotImplemented('Tap'),\n rightClick: async () => ThrowNotImplemented('RightClick'),\n hover: async () => ThrowNotImplemented('Hover'),\n dragAndDrop: async () => ThrowNotImplemented('DragAndDrop'),\n },\n keyboard: {\n typeText: async () => ThrowNotImplemented('Input'),\n keyboardPress: async () => ThrowNotImplemented('KeyboardPress'),\n clearInput: async () => ThrowNotImplemented('ClearInput'),\n },\n touch: {\n swipe: async () => ThrowNotImplemented('Swipe'),\n },\n scroll: {\n scroll: async () => ThrowNotImplemented('Scroll'),\n },\n };\n\n constructor(uiContext: StaticPageUIContext) {\n this.uiContext = uiContext;\n }\n\n actionSpace(): DeviceAction[] {\n // Return available actions for static page - they will throw \"not implemented\" errors when executed\n // but need to be available for planning phase\n return defineActionsFromInputPrimitives(this.inputPrimitives, {\n size: () => this.size(),\n });\n }\n\n async evaluateJavaScript<T = unknown>(script: string): Promise<T> {\n return ThrowNotImplemented('evaluateJavaScript');\n }\n\n // @deprecated\n async getElementsInfo() {\n return ThrowNotImplemented('getElementsInfo');\n }\n\n async getElementsNodeTree() {\n return ThrowNotImplemented('getElementsNodeTree');\n }\n\n async getXpathsByPoint(point: Point) {\n return ThrowNotImplemented('getXpathsByPoint');\n }\n\n async getElementInfoByXpath(xpath: string) {\n return ThrowNotImplemented('getElementInfoByXpath');\n }\n\n async size() {\n return {\n ...this.uiContext.shotSize,\n };\n }\n\n async screenshotBase64() {\n const screenshot = this.uiContext.screenshot;\n if (typeof screenshot === 'object' && 'base64' in screenshot) {\n return (screenshot as { base64: string }).base64;\n }\n return screenshot as unknown as string;\n }\n\n async url() {\n return Promise.resolve('https://static_page_without_url');\n }\n\n async scrollUntilTop(startingPoint?: Point) {\n return ThrowNotImplemented('scrollUntilTop');\n }\n\n async scrollUntilBottom(startingPoint?: Point) {\n return ThrowNotImplemented('scrollUntilBottom');\n }\n\n async scrollUntilLeft(startingPoint?: Point) {\n return ThrowNotImplemented('scrollUntilLeft');\n }\n\n async scrollUntilRight(startingPoint?: Point) {\n return ThrowNotImplemented('scrollUntilRight');\n }\n\n async scrollUp(distance?: number, startingPoint?: Point) {\n return ThrowNotImplemented('scrollUp');\n }\n\n async scrollDown(distance?: number, startingPoint?: Point) {\n return ThrowNotImplemented('scrollDown');\n }\n\n async scrollLeft(distance?: number, startingPoint?: Point) {\n return ThrowNotImplemented('scrollLeft');\n }\n\n async scrollRight(distance?: number, startingPoint?: Point) {\n return ThrowNotImplemented('scrollRight');\n }\n\n async clearInput() {\n return ThrowNotImplemented('clearInput');\n }\n\n mouse = {\n click: ThrowNotImplemented.bind(null, 'mouse.click'),\n wheel: ThrowNotImplemented.bind(null, 'mouse.wheel'),\n move: ThrowNotImplemented.bind(null, 'mouse.move'),\n drag: ThrowNotImplemented.bind(null, 'mouse.drag'),\n };\n\n keyboard = {\n type: ThrowNotImplemented.bind(null, 'keyboard.type'),\n press: ThrowNotImplemented.bind(null, 'keyboard.press'),\n };\n\n async destroy(): Promise<void> {\n //\n }\n\n updateContext(newContext: StaticPageUIContext): void {\n this.uiContext = newContext;\n }\n}\n"],"names":["ThrowNotImplemented","methodName","Error","ERROR_CODE_NOT_IMPLEMENTED_AS_DESIGNED","StaticPage","defineActionsFromInputPrimitives","script","point","xpath","screenshot","Promise","startingPoint","distance","newContext","uiContext"],"mappings":";;;;;;;;;;;;AAQA,MAAMA,sBAAsB,CAACC;IAC3B,MAAM,IAAIC,MACR,CAAC,YAAY,EAAED,WAAW,qEAAqE,EAAEE,uCAAuC,CAAC,CAAC;AAE9I;AAIe,MAAMC;IA4BnB,cAA8B;QAG5B,OAAOC,iCAAiC,IAAI,CAAC,eAAe,EAAE;YAC5D,MAAM,IAAM,IAAI,CAAC,IAAI;QACvB;IACF;IAEA,MAAM,mBAAgCC,MAAc,EAAc;QAChE,OAAON,oBAAoB;IAC7B;IAGA,MAAM,kBAAkB;QACtB,OAAOA,oBAAoB;IAC7B;IAEA,MAAM,sBAAsB;QAC1B,OAAOA,oBAAoB;IAC7B;IAEA,MAAM,iBAAiBO,KAAY,EAAE;QACnC,OAAOP,oBAAoB;IAC7B;IAEA,MAAM,sBAAsBQ,KAAa,EAAE;QACzC,OAAOR,oBAAoB;IAC7B;IAEA,MAAM,OAAO;QACX,OAAO;YACL,GAAG,IAAI,CAAC,SAAS,CAAC,QAAQ;QAC5B;IACF;IAEA,MAAM,mBAAmB;QACvB,MAAMS,aAAa,IAAI,CAAC,SAAS,CAAC,UAAU;QAC5C,IAAI,AAAsB,YAAtB,OAAOA,cAA2B,YAAYA,YAChD,OAAQA,WAAkC,MAAM;QAElD,OAAOA;IACT;IAEA,MAAM,MAAM;QACV,OAAOC,QAAQ,OAAO,CAAC;IACzB;IAEA,MAAM,eAAeC,aAAqB,EAAE;QAC1C,OAAOX,oBAAoB;IAC7B;IAEA,MAAM,kBAAkBW,aAAqB,EAAE;QAC7C,OAAOX,oBAAoB;IAC7B;IAEA,MAAM,gBAAgBW,aAAqB,EAAE;QAC3C,OAAOX,oBAAoB;IAC7B;IAEA,MAAM,iBAAiBW,aAAqB,EAAE;QAC5C,OAAOX,oBAAoB;IAC7B;IAEA,MAAM,SAASY,QAAiB,EAAED,aAAqB,EAAE;QACvD,OAAOX,oBAAoB;IAC7B;IAEA,MAAM,WAAWY,QAAiB,EAAED,aAAqB,EAAE;QACzD,OAAOX,oBAAoB;IAC7B;IAEA,MAAM,WAAWY,QAAiB,EAAED,aAAqB,EAAE;QACzD,OAAOX,oBAAoB;IAC7B;IAEA,MAAM,YAAYY,QAAiB,EAAED,aAAqB,EAAE;QAC1D,OAAOX,oBAAoB;IAC7B;IAEA,MAAM,aAAa;QACjB,OAAOA,oBAAoB;IAC7B;IAcA,MAAM,UAAyB,CAE/B;IAEA,cAAca,UAA+B,EAAQ;QACnD,IAAI,CAAC,SAAS,GAAGA;IACnB;IAzGA,YAAYC,SAA8B,CAAE;QAvB5C,wCAAgB;QAEhB,uBAAQ,aAAR;QACA,uBAAS,mBAAmC;YAC1C,SAAS;gBACP,KAAK,UAAYd,oBAAoB;gBACrC,YAAY,UAAYA,oBAAoB;gBAC5C,OAAO,UAAYA,oBAAoB;gBACvC,aAAa,UAAYA,oBAAoB;YAC/C;YACA,UAAU;gBACR,UAAU,UAAYA,oBAAoB;gBAC1C,eAAe,UAAYA,oBAAoB;gBAC/C,YAAY,UAAYA,oBAAoB;YAC9C;YACA,OAAO;gBACL,OAAO,UAAYA,oBAAoB;YACzC;YACA,QAAQ;gBACN,QAAQ,UAAYA,oBAAoB;YAC1C;QACF;QAyFA,gCAAQ;YACN,OAAOA,oBAAoB,IAAI,CAAC,MAAM;YACtC,OAAOA,oBAAoB,IAAI,CAAC,MAAM;YACtC,MAAMA,oBAAoB,IAAI,CAAC,MAAM;YACrC,MAAMA,oBAAoB,IAAI,CAAC,MAAM;QACvC;QAEA,mCAAW;YACT,MAAMA,oBAAoB,IAAI,CAAC,MAAM;YACrC,OAAOA,oBAAoB,IAAI,CAAC,MAAM;QACxC;QAhGE,IAAI,CAAC,SAAS,GAAGc;IACnB;AAwGF"}
@@ -1,10 +1,7 @@
1
- import node_assert from "node:assert";
2
1
  import { z } from "@midscene/core";
3
- import { AbstractInterface, defineAction, defineActionClearInput, defineActionCursorMove, defineActionDoubleClick, defineActionDragAndDrop, defineActionHover, defineActionInput, defineActionKeyboardPress, defineActionLongPress, defineActionPinch, defineActionRightClick, defineActionScroll, defineActionSwipe, defineActionTap, normalizePinchParam } from "@midscene/core/device";
2
+ import { AbstractInterface, defineAction, defineActionsFromInputPrimitives } from "@midscene/core/device";
4
3
  import { sleep } from "@midscene/core/utils";
5
- import { getDebug } from "@midscene/shared/logger";
6
4
  import { transformHotkeyInput } from "@midscene/shared/us-keyboard-layout";
7
- const debug = getDebug('web:page');
8
5
  const navigateParamSchema = z.object({
9
6
  url: z.string().describe('The URL to navigate to. Must start with https://, file://, or a similar protocol.')
10
7
  });
@@ -85,152 +82,117 @@ class AbstractWebPage extends AbstractInterface {
85
82
  }
86
83
  async clearInput(element) {}
87
84
  }
88
- const commonWebActionsForWebPage = (page, includeTouchEvents = false)=>[
89
- defineActionTap(async (param)=>{
90
- const element = param.locate;
91
- node_assert(element, 'Element not found, cannot tap');
92
- await page.mouse.click(element.center[0], element.center[1], {
93
- button: 'left'
94
- });
95
- }),
96
- defineActionRightClick(async (param)=>{
97
- const element = param.locate;
98
- node_assert(element, 'Element not found, cannot right click');
99
- await page.mouse.click(element.center[0], element.center[1], {
100
- button: 'right'
101
- });
102
- }),
103
- defineActionDoubleClick(async (param)=>{
104
- const element = param.locate;
105
- node_assert(element, 'Element not found, cannot double click');
106
- await page.mouse.click(element.center[0], element.center[1], {
107
- button: 'left',
108
- count: 2
109
- });
110
- }),
111
- defineActionHover(async (param)=>{
112
- const element = param.locate;
113
- node_assert(element, 'Element not found, cannot hover');
114
- await page.mouse.move(element.center[0], element.center[1]);
115
- }),
116
- defineActionInput(async (param)=>{
117
- const element = param.locate;
118
- if (element && 'typeOnly' !== param.mode) await page.clearInput(element);
119
- else if (element && 'typeOnly' === param.mode) {
120
- await page.mouse.click(element.center[0], element.center[1], {
85
+ function createWebInputPrimitives(page) {
86
+ return {
87
+ pointer: {
88
+ tap: async ({ x, y })=>{
89
+ await page.mouse.click(x, y, {
121
90
  button: 'left'
122
91
  });
123
- await page.keyboard.press([
124
- {
125
- key: 'End'
126
- }
127
- ]);
92
+ },
93
+ rightClick: async ({ x, y })=>{
94
+ await page.mouse.click(x, y, {
95
+ button: 'right'
96
+ });
97
+ },
98
+ doubleClick: async ({ x, y })=>{
99
+ await page.mouse.click(x, y, {
100
+ button: 'left',
101
+ count: 2
102
+ });
103
+ },
104
+ hover: async ({ x, y })=>{
105
+ await page.mouse.move(x, y);
106
+ },
107
+ dragAndDrop: async (from, to)=>{
108
+ await page.mouse.drag(from, to);
109
+ },
110
+ longPress: async ({ x, y }, opts)=>{
111
+ await page.longPress(x, y, opts?.duration);
128
112
  }
129
- if ('clear' === param.mode) return;
130
- if (!param || !param.value) return;
131
- await page.keyboard.type(param.value);
132
- await page.flushPendingVisualUpdate?.();
133
- }),
134
- defineActionKeyboardPress(async (param)=>{
135
- const element = param.locate;
136
- if (element) await page.mouse.click(element.center[0], element.center[1], {
137
- button: 'left'
138
- });
139
- const keys = getKeyCommands(param.keyName);
140
- await page.keyboard.press(keys);
141
- await page.flushPendingVisualUpdate?.();
142
- }),
143
- defineActionCursorMove(async (param)=>{
144
- const arrowKey = 'left' === param.direction ? 'ArrowLeft' : 'ArrowRight';
145
- const times = param.times ?? 1;
146
- for(let i = 0; i < times; i++){
147
- await page.keyboard.press([
148
- {
149
- key: arrowKey
150
- }
151
- ]);
152
- await sleep(100);
113
+ },
114
+ keyboard: {
115
+ typeText: async (value, opts)=>{
116
+ const element = opts?.target;
117
+ if (element && opts?.replace !== false) await page.clearInput(element);
118
+ else if (element) {
119
+ const target = element;
120
+ await page.mouse.click(target.center[0], target.center[1], {
121
+ button: 'left'
122
+ });
123
+ await page.keyboard.press([
124
+ {
125
+ key: 'End'
126
+ }
127
+ ]);
128
+ }
129
+ if (opts?.focusOnly) return;
130
+ await page.keyboard.type(value);
131
+ await page.flushPendingVisualUpdate?.();
132
+ },
133
+ keyboardPress: async (keyName, opts)=>{
134
+ const element = opts?.target;
135
+ if (element) await page.mouse.click(element.center[0], element.center[1], {
136
+ button: 'left'
137
+ });
138
+ const keys = getKeyCommands(keyName);
139
+ await page.keyboard.press(keys);
140
+ await page.flushPendingVisualUpdate?.();
141
+ },
142
+ cursorMove: async (direction, times = 1)=>{
143
+ const arrowKey = 'left' === direction ? 'ArrowLeft' : 'ArrowRight';
144
+ for(let i = 0; i < times; i++){
145
+ await page.keyboard.press([
146
+ {
147
+ key: arrowKey
148
+ }
149
+ ]);
150
+ await sleep(100);
151
+ }
152
+ },
153
+ clearInput: async (target)=>{
154
+ await page.clearInput(target);
153
155
  }
154
- }),
155
- defineActionScroll(async (param)=>{
156
- const element = param.locate;
157
- const startingPoint = element ? {
158
- left: element.center[0],
159
- top: element.center[1]
160
- } : void 0;
161
- const scrollToEventName = param?.scrollType;
162
- if ('scrollToTop' === scrollToEventName) await page.scrollUntilTop(startingPoint);
163
- else if ('scrollToBottom' === scrollToEventName) await page.scrollUntilBottom(startingPoint);
164
- else if ('scrollToRight' === scrollToEventName) await page.scrollUntilRight(startingPoint);
165
- else if ('scrollToLeft' === scrollToEventName) await page.scrollUntilLeft(startingPoint);
166
- else if ('singleAction' !== scrollToEventName && scrollToEventName) throw new Error(`Unknown scroll event type: ${scrollToEventName}, param: ${JSON.stringify(param)}`);
167
- else {
168
- if (param?.direction !== 'down' && param && param.direction) if ('up' === param.direction) await page.scrollUp(param.distance || void 0, startingPoint);
169
- else if ('left' === param.direction) await page.scrollLeft(param.distance || void 0, startingPoint);
170
- else if ('right' === param.direction) await page.scrollRight(param.distance || void 0, startingPoint);
171
- else throw new Error(`Unknown scroll direction: ${param.direction}`);
172
- else await page.scrollDown(param?.distance || void 0, startingPoint);
173
- await sleep(500);
156
+ },
157
+ touch: {
158
+ pinch: async ({ x, y }, opts)=>{
159
+ await page.pinch(x, y, opts.startDistance, opts.endDistance, opts.duration);
160
+ },
161
+ swipe: async (from, to, opts)=>{
162
+ await page.swipe(from, to, opts?.duration);
174
163
  }
175
- }),
176
- defineActionDragAndDrop(async (param)=>{
177
- const from = param.from;
178
- const to = param.to;
179
- node_assert(from, 'missing "from" param for drag and drop');
180
- node_assert(to, 'missing "to" param for drag and drop');
181
- await page.mouse.drag({
182
- x: from.center[0],
183
- y: from.center[1]
184
- }, {
185
- x: to.center[0],
186
- y: to.center[1]
187
- });
188
- }),
189
- defineActionLongPress(async (param)=>{
190
- const element = param.locate;
191
- node_assert(element, 'Element not found, cannot long press');
192
- const duration = param?.duration;
193
- await page.longPress(element.center[0], element.center[1], duration);
194
- }),
195
- defineActionPinch(async (param)=>{
196
- const { centerX, centerY, startDistance, endDistance, duration } = normalizePinchParam(param, await page.size());
197
- await page.pinch(centerX, centerY, startDistance, endDistance, duration);
198
- }),
199
- ...includeTouchEvents ? [
200
- defineActionSwipe(async (param)=>{
201
- const { width, height } = await page.size();
202
- const { start, end } = param;
203
- const startPoint = start ? {
204
- x: start.center[0],
205
- y: start.center[1]
206
- } : {
207
- x: width / 2,
208
- y: height / 2
209
- };
210
- let endPoint;
211
- if (end) endPoint = {
212
- x: end.center[0],
213
- y: end.center[1]
214
- };
215
- else if (param.distance) {
216
- const direction = param.direction;
217
- if (!direction) throw new Error('direction is required for swipe gesture');
218
- endPoint = {
219
- x: startPoint.x + ('right' === direction ? param.distance : 'left' === direction ? -param.distance : 0),
220
- y: startPoint.y + ('down' === direction ? param.distance : 'up' === direction ? -param.distance : 0)
221
- };
222
- } else throw new Error('Either end or distance must be specified for swipe gesture');
223
- endPoint.x = Math.max(0, Math.min(endPoint.x, width));
224
- endPoint.y = Math.max(0, Math.min(endPoint.y, height));
225
- const duration = param.duration;
226
- debug(`swipe from ${startPoint.x}, ${startPoint.y} to ${endPoint.x}, ${endPoint.y} with duration ${duration}ms, repeat is set to ${param.repeat}`);
227
- let repeat = 'number' == typeof param.repeat ? param.repeat : 1;
228
- if (0 === repeat) repeat = 10;
229
- for(let i = 0; i < repeat; i++)await page.swipe(startPoint, endPoint, duration);
230
- })
231
- ] : [],
232
- defineActionClearInput(async (param)=>{
233
- await page.clearInput(param.locate);
164
+ },
165
+ scroll: {
166
+ scroll: async (param)=>{
167
+ const element = param.locate;
168
+ const startingPoint = element ? {
169
+ left: element.center[0],
170
+ top: element.center[1]
171
+ } : void 0;
172
+ const scrollToEventName = param?.scrollType;
173
+ if ('scrollToTop' === scrollToEventName) await page.scrollUntilTop(startingPoint);
174
+ else if ('scrollToBottom' === scrollToEventName) await page.scrollUntilBottom(startingPoint);
175
+ else if ('scrollToRight' === scrollToEventName) await page.scrollUntilRight(startingPoint);
176
+ else if ('scrollToLeft' === scrollToEventName) await page.scrollUntilLeft(startingPoint);
177
+ else if ('singleAction' !== scrollToEventName && scrollToEventName) throw new Error(`Unknown scroll event type: ${scrollToEventName}, param: ${JSON.stringify(param)}`);
178
+ else {
179
+ if (param?.direction !== 'down' && param && param.direction) if ('up' === param.direction) await page.scrollUp(param.distance || void 0, startingPoint);
180
+ else if ('left' === param.direction) await page.scrollLeft(param.distance || void 0, startingPoint);
181
+ else if ('right' === param.direction) await page.scrollRight(param.distance || void 0, startingPoint);
182
+ else throw new Error(`Unknown scroll direction: ${param.direction}`);
183
+ else await page.scrollDown(param?.distance || void 0, startingPoint);
184
+ await sleep(500);
185
+ }
186
+ }
187
+ }
188
+ };
189
+ }
190
+ const commonWebActionsForWebPage = (page, includeTouchEvents = false)=>{
191
+ const input = createWebInputPrimitives(page);
192
+ return [
193
+ ...defineActionsFromInputPrimitives(input, {
194
+ size: ()=>page.size(),
195
+ includeSwipe: includeTouchEvents
234
196
  }),
235
197
  defineAction({
236
198
  name: 'Navigate',
@@ -269,6 +231,7 @@ const commonWebActionsForWebPage = (page, includeTouchEvents = false)=>[
269
231
  }
270
232
  })
271
233
  ];
272
- export { AbstractWebPage, commonWebActionsForWebPage, getKeyCommands };
234
+ };
235
+ export { AbstractWebPage, commonWebActionsForWebPage, createWebInputPrimitives, getKeyCommands };
273
236
 
274
237
  //# sourceMappingURL=web-page.mjs.map