11

How can a greasemonkey script / Chrome's user-script intercept a running websocket communication?

My goal is to have additional statistics for a in-browser game

Vojtech B
  • 2,837
  • 7
  • 31
  • 59
  • Are you also building the in-browser game? If so, I might recommend the pattern established by Redux Devtools, which is to provide a hook in the app that a Chrome plugin can hook into. Code at https://github.com/zalmoxisus/redux-devtools-extension, extension at https://chrome.google.com/webstore/detail/redux-devtools/lmhkpmbekcpmknklioeibfkpmmfibljd – carpeliam Jan 21 '18 at 19:02
  • This comment shows a working tampermonkey script to intercept websocket communication: https://github.com/skepticfx/wshook/issues/5#issuecomment-531067029 – psmith Apr 05 '21 at 08:08

2 Answers2

4

You may use Firefox, then add the Firebug module then add & use websocket-monitor module to monitor websocket.

If you want to hook websocket from javascript you may also use wshook.

A. STEFANI
  • 6,707
  • 1
  • 23
  • 48
1

Credit to: https://github.com/Cryoscalpel

Not sure if this works in all ws games but you can create a proxy websocket message logger.

WebSocket.prototype.send = new Proxy(WebSocket.prototype.send, {
          apply: function(target, scope, args) {
             if(typeof(args[0]) === 'string') {
                let json = JSON.parse(args[0]);
                     console.log(json)
           }
                  let data = target.apply(scope, args);
                      return data;
        }
     })