Chrome扩展程序:sendMessage不起作用 [英] Chrome extension: sendMessage doesn't work
问题描述
我已经多次阅读了谷歌关于消息传递的文档,并且可能已经查看了10多个同样问题的其他问题,并且已经尝试了大部分解决方案的几个变体以及我有下面...这是黑魔法,对吧?
清单文件:
{
manifest_version:2,
name:Message Test,
version:1.0,
browser_action:{
default_popup:popup.html
},
background:{
scripts:[background.js]
} ,
content_scripts:[
{
matches:[< all_urls>],
js:[message-test。 js]
}
]
}
我知道扩展不假设使用内联JS,但我离开这个,所以原来的问题可以留下来,因为我仍然无法从背景页面发送消息,当我从弹出到后台,我从manifest.json中删除了相应的行
popup.html文件:
< HTML>
< head>
< script>
chrome.tabs.query({active:true,currentWindow:true},function(tabs){
chrome.tabs.sendMessage(tabs [0] .id,{greeting:hello,theMessage :为什么不能这样工作?},function(response){
console.log(response.farewell);
});
});
< / script>
< / head>
< body>
< / body>
< / html>
OR
background.js文件:
chrome.tabs.query({active:true,currentWindow:true},function(tabs){
chrome .tabs.sendMessage(tabs [0] .id,{greeting:hello,theMessage:为什么没有这个工作?),function(response){
console.log(response.farewell );
});
});
message-test.js file:
var Mymessage;
chrome.runtime.onMessage.addListener(function(message,sender,sendResponse){
if(message.greeting ==hello){
Mymessage = message.theMessage;
alert(Mymessage);
}
else {
sendResponse({});
}
});
我得到一个未定义的警报...
我也试图在按下弹出式窗口中的按钮并在指定的URL上有一个窗口后执行此操作,但这是后面的问题。除了在addEventListener(click....:
http://pastebin.com/KhqxLx5y AND
http:// pastebin.com/JaGcp6tj
您的代码有几个问题。
Chrome不允许在扩展程序中使用内嵌脚本,您必须将popup.html划分为脚本+ HTML:
$ b
// popup.html
< html>
< body>
< script type =text / javascriptsrc =popup。 js>< / script>
< / body>
< / html>
// popup.js
chrome.tabs.query {active:true,currentWindow:true},function(tabs){
var tab = tabs [0]; //不要忘记声明tab变量
chrome.tabs.sendMessage(tab。 ID,{
问候ing:你能听到我吗?
},function(response){});
});
I've already read the documentation from Google on 'message passing' a few times and have probably looked at over 10 other questions with the same problem and already tried quiet a few variations of most of their "solutions" and of what I have below... This is black magic, right? Either way, here it goes.
Manifest File:
{
"manifest_version" : 2,
"name" : "Message Test",
"version" : "1.0",
"browser_action": {
"default_popup": "popup.html"
},
"background": {
"scripts": ["background.js"]
},
"content_scripts": [
{
"matches" : ["<all_urls>"],
"js": ["message-test.js"]
}
]
}
I'm aware extensions aren't suppose to use inline JS, but I'm leaving this in so the original question can be left as it was since I still can't get the message to send from the background page, When I switch from the popup to the background, I removed the appropriate lines from the manifest.json
popup.html file:
<html>
<head>
<script>
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello", theMessage: "Why isn\'t this working?"}, function(response) {
console.log(response.farewell);
});
});
</script>
</head>
<body>
</body>
</html>
OR
background.js file:
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello", theMessage: "Why isn\'t this working?"}, function(response) {
console.log(response.farewell);
});
});
message-test.js file:
var Mymessage;
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
if (message.greeting == "hello"){
Mymessage = message.theMessage;
alert(Mymessage);
}
else{
sendResponse({});
}
});
I get an undefined alert...
I'm also trying to execute this after pressing a button from a popup and having a window at a specified url, but that's a later issue. The other files for the button and window can be found here except with the background.js content wrapped in an addEventListener("click"....: http://pastebin.com/KhqxLx5y AND http://pastebin.com/JaGcp6tj
There are several issues in your code.
Chrome doesn't allow inline scripts in extensions. You must divide your popup.html to script + HTML:
// popup.html
<html>
<body>
<script type="text/javascript" src="popup.js"></script>
</body>
</html>
// popup.js
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
var tab = tabs[0]; // do not forget to declare "tab" variable
chrome.tabs.sendMessage(tab.id, {
greeting: "Can you hear me?"
}, function(response){});
});
这篇关于Chrome扩展程序:sendMessage不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!