0

当然,这个问题太疯狂了,但我找不到任何解决方案。不能使用任何Chrome API来收听活动

详细的我的代码:

manifest.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 
} 

我2天前卡住。对我来说任何援助对我的生活都是巨大的帮助。

对不起,我的英语不好。感谢收看。

回答

2

您不能使用chrome.management.* API(或对于该事件,chrome.runtime.onInstalled事件)from a content script;这是一个没有特权的环境。

但是,内容脚本有一些限制。他们不能:

  • 使用Chrome *的API,以除外:
    • 扩展(使用getURL,inIncognitoContext,lastError,onRequest,sendRequest将)
    • 国际化
    • 运行时(连接,getManifest,使用getURL,ID,的onConnect,的onMessage话,SendMessage)
    • 存储

您需要有一个backgroundevent脚本来为您处理它。如果你需要对它做出反应,或者从内容脚本中发起一些事情,那么带背景的messaging就是答案。