当然,这个问题太疯狂了,但我找不到任何解决方案。
我的代码详细信息:
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
}
我从两天前就被困住了。对我的任何帮助都是对我生活的巨大帮助。
抱歉我的英语不好。感谢您的观看。
最佳答案
您无法使用 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
您需要有一个 background或event脚本来为您处理它。如果您需要对其使用react,或从内容脚本启动某些内容,messaging有背景就是答案。
关于javascript - 无法使用任何Chrome API来监听事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39139922/