Using puppeteer I am trying to read websocket messages and put them into a string to output onto the console. To see an example of some websocket messages you can visit the link provided below within the code and then follow the steps within the parenthesis. (CTRL+SHIFT+I > Network > WS (websocket) > Messages)
var puppeteer = require('puppeteer');
async function run() {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('http://powerline.io');
console.log('navigated successfully');
page.on('response', response => {
const isWSS = ['websocket'].includes(response.request().resourceType())
if (isWSS){
console.log(isWSS);
log(response.url());
response.text().then(log)
}
})
}
run();
Here is an example, how it can be captured:
var puppeteer = require('puppeteer');
async function run() {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('http://powerline.io');
const cdp = await page.target().createCDPSession();
await cdp.send('Network.enable');
await cdp.send('Page.enable');
const printResponse = response => console.log('response: ', response);
cdp.on('Network.webSocketFrameReceived', printResponse); // Fired when WebSocket message is received.
cdp.on('Network.webSocketFrameSent', printResponse); // Fired when WebSocket message is sent.
}
run();
Read more about network events.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With