admin管理员组文章数量:1396092
I'm trying to apply the following flow :
binding for keys in the background.js
, when pressing them :
Sending message from background.js -> contentScript.js
Sending response from contentScript.js -> background.js
Here is the menifit.json
definitions:
"background" : {
"scripts" : ["background.js"],
"persistent" : true
},
"content_scripts": [
{
"matches": ["*://*/*"],
"js": ["contentScript.js"]
}
],
The binding part is working fine.
Here is the code in background.js
:
chrome.runtime.sendMessage({greeting: "hello"}, function(response) {
console.log(response.farewell);
});
});
Here is the code in contentScript.js
:
chrome.runtime.onMessage.addListener(HandleMessage);
function HandleMessage(request, sender, sendResponse)
{
if (request.greeting == "hello")
{
sendResponse({farewell: "goodbye"});
}
};
This is the error that i'm getting:
Error in event handler for (unknown): Cannot read property 'farewell' of undefined
Stack trace: TypeError: Cannot read property 'farewell' of undefined
at chrome-extension://fejkdlpdejnjkmaeadiclinbijnjoeei/background.js:64:26
at disconnectListener (extensions::messaging:338:9)
at Function.target.(anonymous function) (extensions::SafeBuiltins:19:14)
at EventImpl.dispatchToListener (extensions::event_bindings:397:22)
at Function.target.(anonymous function) (extensions::SafeBuiltins:19:14)
at Event.publicClass.(anonymous function) [as dispatchToListener] (extensions::utils:93:26)
at EventImpl.dispatch_ (extensions::event_bindings:379:35)
at EventImpl.dispatch (extensions::event_bindings:403:17)
at Function.target.(anonymous function) (extensions::SafeBuiltins:19:14)
at Event.publicClass.(anonymous function) [as dispatch] (extensions::utils:93:26)
In case of changing
console.log(response.farewell);
to
console.log("OK");
it is working fine , but that way i cant get the values from the contentScript.js
What should i change ?
I'm trying to apply the following flow :
binding for keys in the background.js
, when pressing them :
Sending message from background.js -> contentScript.js
Sending response from contentScript.js -> background.js
Here is the menifit.json
definitions:
"background" : {
"scripts" : ["background.js"],
"persistent" : true
},
"content_scripts": [
{
"matches": ["*://*/*"],
"js": ["contentScript.js"]
}
],
The binding part is working fine.
Here is the code in background.js
:
chrome.runtime.sendMessage({greeting: "hello"}, function(response) {
console.log(response.farewell);
});
});
Here is the code in contentScript.js
:
chrome.runtime.onMessage.addListener(HandleMessage);
function HandleMessage(request, sender, sendResponse)
{
if (request.greeting == "hello")
{
sendResponse({farewell: "goodbye"});
}
};
This is the error that i'm getting:
Error in event handler for (unknown): Cannot read property 'farewell' of undefined
Stack trace: TypeError: Cannot read property 'farewell' of undefined
at chrome-extension://fejkdlpdejnjkmaeadiclinbijnjoeei/background.js:64:26
at disconnectListener (extensions::messaging:338:9)
at Function.target.(anonymous function) (extensions::SafeBuiltins:19:14)
at EventImpl.dispatchToListener (extensions::event_bindings:397:22)
at Function.target.(anonymous function) (extensions::SafeBuiltins:19:14)
at Event.publicClass.(anonymous function) [as dispatchToListener] (extensions::utils:93:26)
at EventImpl.dispatch_ (extensions::event_bindings:379:35)
at EventImpl.dispatch (extensions::event_bindings:403:17)
at Function.target.(anonymous function) (extensions::SafeBuiltins:19:14)
at Event.publicClass.(anonymous function) [as dispatch] (extensions::utils:93:26)
In case of changing
console.log(response.farewell);
to
console.log("OK");
it is working fine , but that way i cant get the values from the contentScript.js
What should i change ?
Share Improve this question edited Oct 10, 2014 at 14:16 Marc Rochkind 3,7403 gold badges33 silver badges41 bronze badges asked Oct 10, 2014 at 9:30 USer22999299USer22999299 5,71410 gold badges51 silver badges84 bronze badges 1- Question should be edited to state that it is about a Chrome Extension, not a Chrome App. I have removed the google-chrome-app tag. – Marc Rochkind Commented Oct 10, 2014 at 13:59
2 Answers
Reset to default 6You're trying to send messages from the background page to the content script. From the official documentation, you're using this code snippet to send messages from the background page:
chrome.runtime.sendMessage({greeting: "hello"}, function(response) {
console.log(response.farewell);
});
However, the docs clearly mention that the code mentioned above is to send a message from a content script. This is the reason why you probably get the error:
Error in event handler for (unknown): Cannot read property 'farewell' of undefined
Stack trace: TypeError: Cannot read property 'farewell' of undefined
What you want to achieve is sending a request from the extension (background page) to a content script which requires you to specify the tab in which the content script is present:
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello"}, function(response) {
console.log(response.farewell);
});
});
I hope this clarifies things and gets you started in the right direction.
There are 3 ways to Send Message in Chrome Extension, you can refer to Message Passing.
Usually use one-time request, for example:
In background.js
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {message: "hello"}, function(response) {
console.log(response);
});
});
In contentscripts.js
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
console.log(request.message);
if (request.message == "hello") {
sendResponse({farewell: "goodbye"});
}
});
本文标签: javascriptSimple message passing between 39backgroundjs39 to 39contentScriptjs39Stack Overflow
版权声明:本文标题:javascript - Simple message passing between 'background.js' to 'contentScript.js' - Stack Overfl 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744112745a2591364.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论