I am developing a Chrome extension. I want to pass a message from the contentScript.js to my popup.js file. This should happen when the contentScript.js loads. From what I have read, I use " chrome.runtime.sendMessage" to do this. I am not sure how to actually receive it in the popup.js file. I have read that you need to use "chrome.runtime.onMessage.addListener", however it does not work.
contentScript.js
window.onload = function() {
var markup = document.documentElement.innerHTML;
alert(markup);
chrome.runtime.sendMessage({a:"b"});
//This alert fires.
alert("Content sent");
};
popup.js
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
//This alert never fires.
alert("Message Received!");
}
);
There are no errors received in the console, nor the extensions page. The message is either never sent, or is sent on not received. What am I missing?