0

I would like to intercept all WebSocket traffic from a Chrome extension. But I don't know if this can even be done.

GirkovArpa
  • 4,427
  • 4
  • 14
  • 43
  • AFAIK the only way is to hook WebSocket.prototype methods in [page context](/a/9517879). – wOxxOm Jun 27 '20 at 05:58
  • That's what I'm doing currently. I'm just hoping there's a nicer way. Hooking the `WebSocket` constructor takes a lot of code to intercept sent and received messages. – GirkovArpa Jun 27 '20 at 06:19
  • Well, the only other method is chrome.webRequest but I don't think you'll be able to read the body/data. Ah, and of course there's always chrome.debugger with full access to [CDP](https://chromedevtools.github.io/devtools-protocol/tot/) but that's kinda nuclear. – wOxxOm Jun 27 '20 at 13:28
  • In Puppeteer it's literally 3 lines: `const cdp = await page.target().createCDPSession(); await cdp.send('Network.enable'); await cdp.send('Page.enable'); cdp.on('Network.webSocketFrameReceived', frame => { // do stuff });` Does this work in extensions too? – GirkovArpa Jun 27 '20 at 14:09
  • See my previous comment, it's what chrome.debugger is for. – wOxxOm Jun 27 '20 at 14:13
  • I'll check it out. – GirkovArpa Jun 27 '20 at 14:30

0 Answers0