I\'ve already read the documentation from Google on \'message passing\' a few times and have probably looked at over 10 other questions with the same problem and already tri
Here is the solution, using background script:
{
"manifest_version" : 2,
"name" : "Message Test",
"version" : "1.0",
"background":{
"scripts":["popup.js"]
},
"content_scripts": [
{
"matches" : [""],
"js": ["message-test.js"]
}
]
}
var port = chrome.runtime.connect();
port.onMessage.addListener(function(message, sender, sendResponse) {
if (message.greeting == "Can you hear me?"){
alert("Test");
}
else{
sendResponse({});
}
});
chrome.runtime.onConnect.addListener( function ( port ) {
port.postMessage({
greeting: "Can you hear me?"
});
});
Some explanaition: first we are connecting to our background script from content script and then background script sends message to content script.
I've improved answer according to @Xan remark. But the idea is the same, first of all you should let know your background script of the existence of content script.