I am developing an extension for both Chrome and Firefox, and I have come across a problem.
Basically, I am trying to get a content script to listen to messages using chrome.runtime.onMessage.addListener(...)
, however it doesn't seem to work.
I tested it by sending a message from the content script. The background script (ml.js
) had a listener that worked fine, but the lsitener in the content script just didn't get the message.
You can view the code in this Gist (or below).
manifest.json
:
{
"manifest_version": 2,
"name": "Messaging Extension",
"version": "1.0.0",
"background": {
"scripts": ["ml.js"]
},
"content_scripts": [
{
"matches": ["*://*.google.co.uk/*"],
"js": ["cs.js"],
"run_at": "document_end"
}
]
}
ml.js
:
// When receive message...
chrome.runtime.onMessage.addListener(function(message) {
if (message.key) {
console.log('ML: First message received')
// Send another message
chrome.runtime.sendMessage({
'foo': 'bar'
})
}
})
cs.js
:
// Send message to ml.js
chrome.runtime.sendMessage({
'key': 'value'
})
chrome.runtime.onMessage.addListener(function(message) {
console.log('CS: Second message received')
})
When tested in Firefox (by loading the add-on at about:debugging and then visiting Google), cs.js
sent the message, and ml.js
logged the message to the console, however cs.js
didn't log the message.
I'll appreciate some help, thanks!