javascript - 无法使用任何Chrome API来监听事件

标签 javascript google-chrome-extension google-chrome-devtools

当然,这个问题太疯狂了,但我找不到任何解决方案。

我的代码详细信息:

ma​​nifest.json

"content_scripts": [{
    "js": [ "ct.js" ],
    "matches": [ "<all_urls>" ]
 }],
"permissions": [ "management","storage","proxy", "*://*/*" ],

ct.js

chrome.management.onInstalled.addListener(function (info) {
    console.info(info);
});

chrome.management.onEnabled.addListener(function (info) {
    console.info(info);
});

我在控制台中得到错误结果

ct.js:58 Uncaught TypeError: Cannot read property 'onInstalled' of undefined

我在控制台中记录 chrome 变量

{
  app: Object
  csi: function()
  loadTimes: function()
  runtime: Object
  webstore: Object
  __proto__: Object
}

我从两天前就被困住了。对我的任何帮助都是对我生活的巨大帮助。

抱歉我的英语不好。感谢您的观看。

最佳答案

您无法使用 chrome.management.* API(或者,chrome.runtime.onInstalled 事件)from a content script ;这是一个特权较低的环境。

However, content scripts have some limitations. They cannot:

  • Use chrome.* APIs, with the exception of:
    • extension (getURL, inIncognitoContext, lastError, onRequest, sendRequest)
    • i18n
    • runtime (connect, getManifest, getURL, id, onConnect, onMessage, sendMessage)
    • storage

您需要有一个 backgroundevent脚本来为您处理它。如果您需要对其使用react,或从内容脚本启动某些内容,messaging有背景就是答案。

关于javascript - 无法使用任何Chrome API来监听事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39139922/

相关文章:

javascript - 在 DOM 中查找元素并替换

javascript - Chrome 扩展 - 如何仅使用名称字段而不是 id 将字段表单聚焦到当前页面

javascript - 无法从 chrome.runtime.sendMessage 访问响应变量。 (关闭?)

javascript - 第二次单击按钮时切换反向动画

javascript - 为使用相同处理程序动态创建的每个组件创建单独的状态

javascript - 获取特定行的属性值?

javascript - Chrome 扩展中的弹出脚本多次发送消息

http - "multipart/form-data"POST 请求是否应该包含一个包含上传图像数据的字符串?

google-chrome - Chrome Web Inspector Web Socket 调试

java - Selenium WebDriver (java) 可以与浏览器的检查工具元素选择器交互吗?