Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome / Firefox extension - Content script not listening for messages

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!

like image 891
Kaspar Lee Avatar asked Oct 30 '16 11:10

Kaspar Lee


People also ask

How do you send a message to content script in Chrome?

If you are sending messages from content script to popup, then you just need to use chrome. runtime. sendMessage instead of tabs. sendMessage .

How would you communicate between background script and content script?

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).

What is the difference between content script and background script?

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.

What is Content_scripts?

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.


1 Answers

Using 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)...

Sending a message to your content script (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:

  • Message Passing (Chrome)
  • Communicating with background scripts (Firefox)
like image 57
Makyen Avatar answered Sep 30 '22 12:09

Makyen