Hans
Hans

Reputation: 41

How to invoke chrome.devtools.* api calls in regular chrome extension without opening devtools in browser

I'm trying to build a regular chrome browser extension (NOT a devtool extension) that when activated on a particular page obtains a list of all the IPs from which resources are requested. I know how to obtain this list from the HAR log by calling chrome.devtools.network.getHAR() in the devtools_page, and then messaging the HAR log to a background script. However, you can only call chrome.devtools.* from within a devtools page, which exists only for the life span of an open devtools window in the browser. I don't want to have to open up devtools just to invoke this function in devtools.js. Is there a way to make a call to chrome.devtools.* e.g. from background.js without having to open up devtools in the browser?

Here's what's going on right now:

// background.js
chrome.runtime.onConnect.addListener(function(port) {
  console.assert(port.name == "somePort");
  port.onMessage.addListener(function(msg) {
      console.log(msg.content);
  });
});

,

// devtools.js
chrome.devtools.network.getHAR(function(harLog) {
  var port = chrome.extension.connect({name: "somePort"});
  port.postMessage({content: harLog});
});

,

//manifest.json
"devtools_page": "devtools.html",
"background": {
    "scripts": [
        "background.js"
    ],
    "persistent": false
 }

Upvotes: 4

Views: 1359

Answers (1)

Haibara Ai
Haibara Ai

Reputation: 10907

Unfortunately, there is no way to access to chrome.devtools.* api without opening up devtools window.

The chrome.devtools.* API modules are available only to the pages loaded within the DevTools window. Content scripts and other extension pages do not have these APIs. Thus, the APIs are available only through the lifetime of the DevTools window.

Upvotes: 2

Related Questions