I am trying to implement message passing from background script to content script
manifest.json
"background": {
"scripts": ["background.js"],
"persistent": false
},
"content_scripts" : [
{
"matches" : [ "http://*/*" ],
"js": ["hello.js"],
"css": [ "hello.css" ]
}
background.js
function showpopup()
{
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello"}, function(response) {
console.log(response.farewell);
});
});
}
chrome.alarms.onAlarm.addListener(showpopup);
content script js aka hello.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")
sendResponse({farewell: "goodbye"});
});
I am getting the error Error in event handler for (unknown): TypeError: Cannot read property 'farewell' of undefined
I have gone extensively through StackOverflow and also implemented this solution but to no avail.