chrome.runtime.onMessage 在后台脚本中未定义(chrome 扩展) [英] chrome.runtime.onMessage undefined in background script (chrome extension)
问题描述
我正在尝试创建一个 chrome 扩展,但由于某些原因,有时 chrome.runtime 对象似乎不完整,并且缺少很多方法(包括我想要的 onMessage).
I am trying to create a chrome extension, but for some reasons, sometimes, the chrome.runtime object seems incomplete, and a lot of methods are missing (including onMessage, which is the one I want).
似乎有时有效,有时无效.我认为这可能是与时间相关的问题,但我不明白为什么我不能简单地在后台创建消息监听器?
It seems sometimes it works, sometimes not. I assumed it may be a time related issue, but I don't understand why I can't simply create a message listener on background?
我的后台脚本:
setTimeout(function () {
console.log("gogo!");
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
console.log(sender.tab ?
"from a content script:" + sender.tab.url :
"from the extension");
if (request.type == "tab") {
console.log("tab!");
sendResponse({status: "ok"});
}
}),
2
});
其中chrome.runtime.onMessage"未定义.
Where "chrome.runtime.onMessage" is undefined.
谢谢!
Edit2:我构建了一个简单得多的原型,但它又失败了.现在我真的很困惑.这是我所拥有的:
I have built a much simpler prototype, and it's failing again. Now I am really confused. Here is what I have:
$tree
.
├── manifest.json
├── src
│ ├── background.html
│ ├── background.js
│ └── test.js
└── vendor
└── jquery.js
2 directories, 5 files
manifest.json 文件:
manifest.json file:
{
"manifest_version": 2,
"name": "test",
"description": "test",
"version": "1.0",
"author": "test",
"homepage_url": "http://www.test.com",
"content_scripts": [
{
"run_at" : "document_idle",
"matches": ["https://www.google*"],
"js": ["vendor/jquery.js", "src/test.js"]
}
],
"background": {
"page": "src/background.html",
"persistent": false
},
"permissions": [
"tabs",
"https://www.google*"
]
}
background.html 文件:
background.html file:
<script src="../vendor/jquery.js"></script>
<script src="background.js"></script>
background.js 文件:
background.js file:
function run () {
console.log("gogo!");
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
console.log(sender.tab ?
"from a content script:" + sender.tab.url :
"from the extension");
if (request.type == "tab") {
console.log("tab!");
sendResponse({status: "ok"});
}
});
}
run();
test.js 文件:
test.js file:
'use strict';
run();
function run() {
var url = window.location.href;
// Error if no URI
if (!url) {
return 1;
}
var uriRe = /https://www.google.*/;
var reParse = uriRe.exec(url);
if (!reParse) {
return 2;
}
chrome.runtime.sendMessage({type: "tab"}, function(response) {
console.log(response);
});
}
我在 OSX 上使用 Chrome 49.0.2623.112(64 位).
I am using Chrome 49.0.2623.112 (64-bit) on OSX.
这是失败时发生的屏幕截图:
Here is a screenshot of what happens the times it fails:
我想再次准确地说它不会一直失败(可能有 50% 的时间?),这让它变得更加奇怪,让我相信在我不存在的地方存在某种竞争"情况知道.
I want to precise again that it doesn't fail all the time (maybe 50% of the time?), which makes it even more weird and makes me believe there is a kind of "race" condition somewhere I am not aware of.
推荐答案
问题已解决:我必须禁用,然后重新启用所有其他扩展.似乎是 Chrome 的一个错误.
Problem solved: I had to disable, then re-enable all my other extensions. Seems like a bug from Chrome.
这篇关于chrome.runtime.onMessage 在后台脚本中未定义(chrome 扩展)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!