Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Google Chrome remote debugging protocol

I need to get the network events from Chrome. I've found this: https://developer.chrome.com/devtools/docs/debugger-protocol https://developer.chrome.com/devtools/docs/protocol/1.1/network#command-enable

It seems that Chrome uses a port to get messages, answer and send events, for remote debugging. It says it uses JSON, so I decided to try it.

So, I wrote some simple java code that opens the port that chrome is listening on (ofcourse i've started it by using google-chrome --remote-debugging-port=9222 on my ubuntu machine). I have a thread that writes to stdout anything coming from this port, and then the code writes this to the outputstream of the socket using this line (a sample method from the protocol):

out.println("{\"id\": 1,\"method\": \"Network.enable\"}");

I would expect some answer (according to the protocol) in the input stream but nothing happens.

Does anyone ever done something like this? I can't find anything on the net.

like image 588
reformy Avatar asked Feb 10 '15 11:02

reformy


People also ask

What is Chrome DevTools protocol?

The Chrome DevTools Protocol provides APIs to instrument, inspect, debug, and profile Chromium-based browsers. The Chrome DevTools Protocol is the foundation for the Microsoft Edge DevTools. Use the Chrome DevTools Protocol for features that aren't implemented in the WebView2 platform.

How do I use remote debugging port?

The Remote Debugger Port on 32-bit Operating Systems You can configure this from either the command line or the remote debugger window. In the remote debugger window, click Tools > Options, and set the TCP/IP port number. On the command line, start the remote debugger with the /port switch: msvsmon /port <port number>.


1 Answers

Finally I've got it. Credit goes to https://www.igvita.com/2012/04/09/driving-google-chrome-via-websocket-api/.

First I send an HTTP request to http://localhost:9222/json. This returns a JSON list of open tabs in Chrome, for each I also get a WebSocket uri (webSocketDebuggerUrl):

[
{
"description": "",
"devtoolsFrontendUrl": "/devtools/devtools.html?ws=localhost:9222/devtools/page/C014A09F-BD0A-40BA-B23C-7B18B84942CD",
"faviconUrl": "http://cdn.sstatic.net/stackoverflow/img/favicon.ico?v=00a326f96f68",
"id": "C014A09F-BD0A-40BA-B23C-7B18B84942CD",
"title": "Using Google Chrome remote debugging protocol - Stack Overflow",
"type": "page",
"url": "https://stackoverflow.com/questions/28430479/using-google-chrome-remote-debugging-protocol",
"webSocketDebuggerUrl": "ws://localhost:9222/devtools/page/C014A09F-BD0A-40BA-B23C-7B18B84942CD"
}
]

Then I can use WebSocket to send messages for debugging a specific tab, using this URI. I also found this for using Jetty implementation of WebSocket: javax.websocket client simple example.

like image 174
reformy Avatar answered Oct 11 '22 22:10

reformy