我正在尝试构建一个常规的 Chrome 浏览器扩展(不是开发工具扩展),当在特定页面上激活时,它会获取请求资源的所有 IP 的列表。我知道如何通过在 devtools_page 中调用 chrome.devtools.network.getHAR() 从 HAR 日志获取此列表,然后将 HAR 日志消息发送到后台脚本。但是,您只能从 devtools 页面中调用 chrome.devtools.*,该页面仅在浏览器中打开的 devtools 窗口的生命周期内存在。我不想仅仅为了调用 devtools.js 中的这个函数而打开 devtools。有没有办法调用 chrome.devtools.* 例如来自background.js,而无需在浏览器中打开devtools?
这是现在发生的事情:
// 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
}
最佳答案
不幸的是,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.
关于javascript - 如何在常规 Chrome 扩展中调用 chrome.devtools.* api 调用,而无需在浏览器中打开 devtools,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38733845/