我有一个非常简单的 chrome 扩展程序,我试图将消息从后台脚本传递到内容脚本。但是 chrome.runtime
是未定义的。
这实际上是所有代码,如您所见,几乎没有任何内容。在内容脚本中,运行时未定义。
背景脚本:
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.runtime.sendMessage({action: 'someAction'},
function(response) {
console.log('Response: ', response);
});
});
内容脚本:
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
sendResponse({
user: 'user'
});
});
list .json
{
"manifest_version": 2,
"name": "My Extension",
"version": "1.0",
"description": "Some stupid extension",
"browser_action": {
"default_icon": "icons/MyExtensionIcon.png"
},
"icons": {
"48": "icons/MyExtensionIcon.png"
},
"permissions": [
"tabs",
"storage",
"https://*/",
"http://*/"
],
"content_scripts": [
{
"matches": ["*://*.twitter.com/*", "https://twitter.com/*"],
"js": ["js/content.js"]
}
],
"background": {
"scripts": ["js/background.js"],
"persistent": true
},
"web_accessible_resources": [
"js/*",
"css/*"
]
}
其他信息:
- Chrome 版本 58.0.3029.110(64 位)
- 将我的扩展安装为 具有开发者模式的“解压扩展”
最佳答案
好吧,我明白了。这绝对是愚蠢的,但看起来这只是一个 Heisenbug .通过添加断点或调试器语句,它会导致该值未定义。也许是 Chrome 错误?
我发誓,每天 Chrome 感觉越来越多 like Internet Explorer.
关于javascript - 为什么内容脚本中未定义 chrome.runtime?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44234623/