chrome.runtime.sendMessage不按预期工作 [英] chrome.runtime.sendMessage not working as expected
问题描述
我正在编写一个带有内容脚本和后台脚本的Chrome插件,并且我试图让这两者进行通信。
在我的内容脚本中,我是($ greeting,hello},function(response){
console。
chrome.runtime.sendMessage日志(response.farewell);
});
在我的后台脚本中,我正在做
chrome.runtime.onMessage.addListener(
函数(request,sender,sendResponse){
console.log(sender.tab?
从内容脚本:+ sender.tab.url:
from the extension);
if(request.greeting ==hello)
sendResponse({farewell:goodbye });
}
);
我的清单看起来像这样:
<$ p
manifest_version:2,
name:Tesing Phase,
version:1.0,
背景:{
persistent:false,
scripts:[bgscript.js]
},
content_scripts:[{
js:[contentscript.js],
all_frames:true,
run_at:document_start,
matches:[*:// * / * ]
}],
web_accessible_resources:[script.js]
}
运行我的插件时,出现以下错误:
未捕获的TypeError:Object#< ;对象>没有方法'sendMessage'
我试过记录 chrome.runtime
,并且没有方法 sendMessage
。我在Ubuntu上使用Chromium的25.0版本。我也尝试过使用 sendRequest
,但它表示它已折旧并且应该使用 sendMessage
。
任何人都可以指出我在这里错过了什么吗?是否有任何权限需要这个工作?
chrome.runtime.sendMessage
/ onMessage
(以及其他相关事件/方法,如 connect
)在Chrome 26中引入。
如果您想编写与Chrome 20 - 25兼容的扩展程序,请使用 chrome.extension.sendMessage
。
实现最佳兼容性的一种方法是自己定义 chrome.runtime
方法。例如,在代码的其余部分(后台/内容脚本)之前运行以下代码:
$ b
if(!chrome.runtime){
// Chrome 20-21
chrome.runtime = chrome.extension;
} else if(!chrome.runtime.onMessage){
// Chrome 22-25
chrome.runtime.onMessage = chrome.extension.onMessage;
chrome.runtime.sendMessage = chrome.extension.sendMessage;
chrome.runtime.onConnect = chrome.extension.onConnect;
chrome.runtime.connect = chrome.extension.connect;
}
然后您可以使用最新的API格式:
//绑定事件:
chrome.runtime.onMessage.addListener(function(message,sender,sendResponse) {
//做某事
});
//发送消息:
chrome.runtime.sendMessage({greeting:'hello'});
如果您对修改 chrome.runtime
对象,您可以使用下面的方法来代替:
var runtimeOrExtension = chrome.runtime&& chrome.runtime.sendMessage?
'runtime':'extension';
$ b $ //绑定事件:
chrome [runtimeOrExtension] .onMessage.addListener(
函数(message,sender,sendResponse){
//做某事
});
//发送消息:
chrome [runtimeOrExtension] .sendMessage({greeting:'hello'});
I am writing a Chrome plugin with a content script and a background script, and I am trying to make the two communicate.
In my content script, I am doing
chrome.runtime.sendMessage({greeting: "hello"}, function(response) {
console.log(response.farewell);
});
and in my background script, I am doing
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
console.log(sender.tab ?
"from a content script:" + sender.tab.url :
"from the extension");
if (request.greeting == "hello")
sendResponse({farewell: "goodbye"});
}
);
My manifest looks like this:
{
"manifest_version": 2,
"name": "Tesing Phase",
"version": "1.0",
"background": {
"persistent": false,
"scripts": ["bgscript.js"]
},
"content_scripts": [{
"js": ["contentscript.js"],
"all_frames": true,
"run_at" : "document_start",
"matches": ["*://*/*"]
}],
"web_accessible_resources": ["script.js"]
}
When I run my plugin, I get the following error:
Uncaught TypeError: Object #<Object> has no method 'sendMessage'
I tried logging chrome.runtime
, and there was no method sendMessage
. I am using version 25.0 of Chromium on Ubuntu. I tried using sendRequest
as well, but it said it's depreciated and sendMessage
should be used.
Can anyone point me out what I am missing here? Are there any permissions needed for this to work?
chrome.runtime.sendMessage
/ onMessage
(and other related events/methods such as connect
) were introduced in Chrome 26.
If you want to write an extension which is compatible with Chrome 20 - 25, use chrome.extension.sendMessage
.
A way to achieve optimal compatibility is to define the chrome.runtime
methods yourself. For example, run the following code before the rest of your code (background/content script):
if (!chrome.runtime) {
// Chrome 20-21
chrome.runtime = chrome.extension;
} else if(!chrome.runtime.onMessage) {
// Chrome 22-25
chrome.runtime.onMessage = chrome.extension.onMessage;
chrome.runtime.sendMessage = chrome.extension.sendMessage;
chrome.runtime.onConnect = chrome.extension.onConnect;
chrome.runtime.connect = chrome.extension.connect;
}
Then you can just use the latest API format:
// Bind event:
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
// Do something
});
// Send message:
chrome.runtime.sendMessage({greeting: 'hello'});
If you feel uncomfortable with modifying methods on the chrome.runtime
object, you can use the following approach instead:
var runtimeOrExtension = chrome.runtime && chrome.runtime.sendMessage ?
'runtime' : 'extension';
// Bind event:
chrome[runtimeOrExtension].onMessage.addListener(
function(message, sender, sendResponse) {
// Do something
});
// Send message:
chrome[runtimeOrExtension].sendMessage({greeting: 'hello'});
这篇关于chrome.runtime.sendMessage不按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!