Skip to content

Instantly share code, notes, and snippets.

@jasonLaster
Created March 28, 2015 00:08
Show Gist options
  • Save jasonLaster/2bad4dc4262a22b94c44 to your computer and use it in GitHub Desktop.
Save jasonLaster/2bad4dc4262a22b94c44 to your computer and use it in GitHub Desktop.
monitoring the inspector backend class
var dispatch = InspectorBackendClass.Connection.prototype.dispatch;
InspectorBackendClass.Connection.prototype.dispatch = function() {
if (arguments && arguments.length > 0) {
var message = arguments[0];
var object = message ? JSON.parse(message) : {};
console.log.apply(console,['events', object]);
}
dispatch.apply(this, arguments);
}
@paulirish
Copy link

jason... had a request..

I'd love to recommend using this instead of the protocol sniffing technique i doc'd here:
https://developer.chrome.com/devtools/docs/debugger-protocol#sniffing-the protocol

  • I was looking at InspectorBackendClass.Connection and it appears there are some dump and wrap functions that looked to be built for this sort of thing.
  • can you try out logging both directions. Maybe a unicode arrow in front of each log to indication which way it was going?
  • also i think it should strip out things like id and empty message payloads.

@kdzwinel
Copy link

This doesn't seem to be working anymore (at least in canary). Script is trying to JSON.parse an object.

screen shot 2015-12-13 at 22 10 08

@talitore
Copy link

talitore commented Jan 7, 2016

Following the logic in the source of dispatch which this patch calls; I was able to get it working with the following:

var dispatch = InspectorBackendClass.Connection.prototype.dispatch;
InspectorBackendClass.Connection.prototype.dispatch = function() {
    if (arguments && arguments.length > 0) {
        var message = arguments[0];
        var object = ((typeof message === "string") ? JSON.parse(message) : message);
        console.log.apply(console,['events', object]);
    }
    dispatch.apply(this, arguments);
}

I also had success removing object altogether and passing message to dispatch, but wasn't sure of the stability of it.

@paulirish
Copy link

paulirish commented Jan 20, 2016

@jasonLaster et al.. two things:

native dumpInspectorProtocolMessages

Instead of using this logging snippet, you can set InspectorBackendClass.Options.dumpInspectorProtocolMessages = true to log all messages to the console. It will log the JSON.stringify'd messages.

IMO, stringified is a bit messy, so I have a patch to clean it up and match this logging snippet's output: https://codereview.chromium.org/1606303003.

Updated protocol logging snippet:

As mentioned above, the snippet at the top of the gist is broken. I've evolved what @talitore did above and also instrumented the sendMessage method, which is key.

// This will log all protocol traffic to the console

const mainConnection = WebInspector.targetManager.mainTarget()._connection

const _dispatch = mainConnection.__proto__.dispatch;
const _sendMessage = mainConnection.__proto__.sendMessage;


mainConnection.__proto__.dispatch = function(message) {
    console.log('received: ', typeof message === "string" ? JSON.parse(message) : message);
    _dispatch.apply(this, arguments);
}

mainConnection.__proto__.sendMessage = function(message) {
    console.log('sending : ', message);
    _sendMessage.apply(this, arguments);
}

This is worth using until https://codereview.chromium.org/1606303003 has landed.

cheers


updated again in july 2016.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment