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!
If you are sending messages from content script to popup, then you just need to use chrome. runtime. sendMessage instead of tabs. sendMessage .
Communication between extensions and their content scripts works by using message passing. Either side can listen for messages sent from the other end, and respond on the same channel. A message can contain any valid JSON object (null, boolean, number, string, array, or object).
Background Script - Provides persistence and handles background events. Content Script - Scripts that run in isolation in the context of the web page. Injected Script - Scripts that are programmatically injected into the web page.
Content scripts are files that run in the context of web pages. By using the standard Document Object Model (DOM), they are able to read details of the web pages the browser visits, make changes to them, and pass information to their parent extension.
runtime.sendMessage()
(messages to background scripts):The runtime.sendMessage()
(Chrome/Firefox) method is used to send messages to scripts that are running in the background context (background scripts, popup scripts, etc). Even when it is used to send messages from a script that is in the background context (e.g. to communicate between a popup script and a background script), it will be received by other currently listening background context scripts, but not the script that is sending it.
To quote the Google Chrome runtime.sendMessage()
documentation (emphasis mine):
If sending to your extension, the
runtime.onMessage
event will be fired in every frame of your extension (except for the sender's frame)...
tabs.sendMessage()
)If you are wanting to send a message from your background script to your content script you should be using tabs.sendMessage()
(Chrome/Firefox). Alternately, you can use the connect()
methods in runtime
and tabs
, which then provide you with a port (Chrome/Firefox).
References:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With