I'm working on a chrome extension where I need popup.js to send a message to content.js, and then log the response. I'm running this example exactly as copied from the chrome developer guide on messaging: https://developer.chrome.com/extensions/messaging
popup.js:
function handleClick() {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello"}, function(response) {
console.log(response.farewell);
});
});
}
document.getElementById('bttn').addEventListener('click', handleClick);
content.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"});
});
the message from popup.js is received by content.js because the console logs "from the extension", but response is not received by popup.js because there is no console log for "goodbye".