I'm trying to message passing in a chrome extension. I follow this example (see here)- :
content_script:
chrome.runtime.sendMessage({greeting: "hello"}, function(response) {
console.log(response.farewell);
});
background:
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello"}, function(response) {
console.log(response.farewell);
});
});
popup.js
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")
console.log("message recive")
sendResponse({farewell: "goodbye"});
});
Although I did copy-paste - the message is not sent. Error pops up:
Error in event handler for (unknown): TypeError: Cannot read property 'farewell' of undefined
Where is the mistake?