I'm not sure why, but it seems like chrome.runtime.onMessage
is not loading when my extension loads. I tried to trouble shoot with a basic message:
popup.js
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.contentMessage == "From content script") {
sendResponse({popupMessage:"Message received"});
}
});
content.js
chrome.runtime.sendMessage({contentMessage:"From content script"}, function(response) {
console.log(response);
}
When I run the extension, the error log for content.js shows that it is not receiving a response: "Error in event handler for (unknown): TypeError: Cannot read property 'popupMessage' of undefined.
My popup.js
log shows nothing.
If I open up my popup.js
console and manually paste the code and then try re-sending my message from content.js
(by just attaching it to a button), then everything works.
Can anyone help me figure out why my popup.js
script is not catching the message and returning a response?
Aucun commentaire:
Enregistrer un commentaire