为什么chrome.devtools.network“未定义”? [英] Why is chrome.devtools.network "undefined"?

查看:1079
本文介绍了为什么chrome.devtools.network“未定义”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个记录所有网络事件的扩展。这是代码:

Manifest.json:

  {
name:我的扩展名,
版本:1.0,
背景:{
scripts:[background.js],
persistent:true
},
devtools_page:devtools.html,
browser_action:{
default_title:Get it,
default_icon:icon.png
},
manifest_version:2
}

background.js:

  chrome.devtools.network.onRequestFinished.addListener(函数(request){}); 

有什么问题?我尝试了很多东西,看起来好像我在devtools.html链接的任何脚本都没有得到。没有日志,没有任何东西。只有background.js正在做某些事情,它似乎不支持chrome.devtools?

解决方案

chrome.devtools.network 仅在devtools页面中可用。从 devtools API的文档(第三列表项目):


chrome.devtools。* API模块仅适用于在开发人员工具窗口中加载的页面。 内容脚本和其他扩展页面没有这些API。因此,API只能在开发人员工具窗口的生命周期中使用。


如果您需要背景页面中的信息,请查看此答案(包括完整代码)以设置沟通渠道: Chrome Devpanel扩展与后台页面通信


I'm trying to create an extension that logs all network events. This is the code:

Manifest.json:

{
  "name": "My extension",
  "version" : "1.0",
  "background": {
    "scripts": ["background.js"],
    "persistent": true
  },
 "devtools_page": "devtools.html",
 "browser_action": {
    "default_title": "Get it",
    "default_icon" : "icon.png"
  },
  "manifest_version": 2
}

background.js:

chrome.devtools.network.onRequestFinished.addListener(function(request) {});

What's the problem? I tried a lot of things, it doesn't seem like any scripts that I link in devtools.html is getting picked up at all. No logs, no nothing. Only the background.js is doing something, and it doesn't seem to support chrome.devtools ?

解决方案

chrome.devtools.network is only available within a devtools page. From the documentation of the devtools API (third list item):

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

If you need the information in the background page, have a look at this answer (full code included) for setting up a communication channel: Chrome Devpanel Extension Communicating with Background Page.

这篇关于为什么chrome.devtools.network“未定义”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆