I am trying to get all WebSockets from the current page and save them to a variable, so I can just do socket.send to send messages through them
I’ve tried to do this:
const prototype = await page.evaluateHandle("WebSocket.prototype");
var socketInstances = await page.queryObjects(prototype);
await page.evaluate((instances:Array<WebSocket>) => {
instances[0].send("message");
}, socketInstances);
const cdp = await page.createCDPSession();
await cdp.send('Network.enable');
await cdp.send('Page.enable');
cdp.on("Network.webSocketFrameReceived", response => console.log(response));
cdp.on('Network.webSocketFrameSent', response => console.log(response));
by doing this, i can listen to all messages received by the WebSockets, but i haven’t figured out how to send messages through the WebSockets without calling page.evaluate every time. I’ve tried getting a reference to the WebSockets like this:
await cdp.send('Runtime.enable');
const { exceptionDetails, result: remoteObject } = await cdp.send('Runtime.evaluate', {
expression: 'WebSocket'
});
await cdp.send("Runtime.queryObjects", {prototypeObjectId: remoteObject.objectId});
but I can’t get this to work. Any ideas on how to do this? Maybe in an even easier way?
Sushiaaa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.