Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
 }
like image 586
Hans Avatar asked Oct 16 '25 05:10

Hans


1 Answers

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.

like image 81
Haibara Ai Avatar answered Oct 17 '25 19:10

Haibara Ai



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!