admin管理员组文章数量:1388089
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"....: AND
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./KhqxLx5y AND http://pastebin./JaGcp6tj
Share Improve this question edited May 28, 2014 at 3:31 user3334776 asked Apr 16, 2014 at 23:47 user3334776user3334776 1131 silver badge10 bronze badges 3- all_urls is not valid syntax – Zig Mandel Commented Apr 17, 2014 at 1:28
- @ZigMandel It so happens it is. – Xan Commented Apr 17, 2014 at 8:36
- @hucuhy thanks for putting this bounty out on the question. I still haven't received a helpful response since posting this as of today (which is June 3, 2014). Being able to send a message from the background page to a content script after clicking a button from the default popup wouldn't seem too hard for people who know how this stuff works... – user3334776 Commented Jun 3, 2014 at 20:45
3 Answers
Reset to default 3There 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){});
});
Careful reading of this answer may help you. Every point there applies to your problem.
Your problem is in when your main script is executing, and when your content scripts are. You need to ensure there is a content script listening before you send a message to it, in worst case injecting it programmatically.
And to get it working in a popup, follow KAdot's answer.
Here is the solution, using background script:
manifest.json
{
"manifest_version" : 2,
"name" : "Message Test",
"version" : "1.0",
"background":{
"scripts":["popup.js"]
},
"content_scripts": [
{
"matches" : ["<all_urls>"],
"js": ["message-test.js"]
}
]
}
message-test.js
var port = chrome.runtime.connect();
port.onMessage.addListener(function(message, sender, sendResponse) {
if (message.greeting == "Can you hear me?"){
alert("Test");
}
else{
sendResponse({});
}
});
popup.js
chrome.runtime.onConnect.addListener( function ( port ) {
port.postMessage({
greeting: "Can you hear me?"
});
});
Some explanaition: first we are connecting to our background script from content script and then background script sends message to content script.
Update
I've improved answer according to @Xan remark. But the idea is the same, first of all you should let know your background script of the existence of content script.
本文标签: javascriptChrome extension sendMessage doesn39t workStack Overflow
版权声明:本文标题:javascript - Chrome extension: sendMessage doesn't work - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744548443a2612037.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论