rohithpr
rohithpr

Reputation: 6330

How to access the DevTools API without actually opening the DevTools window?

The documentation says "An instance of the extension's DevTools page is created each time a DevTools window opens. The DevTools page exists for the lifetime of the DevTools window."

However, the Facebook Pixel Helper manages to to get network information without the devtools window being open.

So, I was wondering if there was some other API or a workaround to access the content from the DevTools window (specifically the networks tab, which Facebook seems to be able to do).

Upvotes: 4

Views: 8458

Answers (1)

Rob W
Rob W

Reputation: 349012

The chrome.devtools API cannot be used without opening the devtools window. The Facebook Pixel Helper uses the chrome.webRequest API to get information about network requests. This API does not offer any access to the response body, so that's not particularly helpful.

There are some more (powerful) methods, which are not used by the FB Pixel Helper extension:

  • The chrome.debugger API allows you to do almost anything that the devtools is capable of, including querying the response body, as long as the devtools window is not open. In the following explanation, I assume that you're already attached to the debugger. If you don't know how to do that, see this answer for an example.

    1. Add a listener to chrome.debugger.onEvent
    2. Send the Network.enable command to start listening to events.
    3. When a request completes, the onEvent event will be triggered, with the method name set to Network.responseReceived. The event parameters include things like the request/response headers, status line, etc. If you want to get the response body as well, invoke the Network.getResponseBody command to get the response body.

    For other network events/commands, see the debugging protocol specification for network.

  • Content scripts can use the performance.timing API to query load times of the document in a (top-level/sub) frame.

Upvotes: 8

Related Questions