KingJohnno
KingJohnno

Reputation: 602

Firefox: Returning a value from a content script

When executing a content script, for an active Tab is there a way for that content script to return a value to the popup where the script was executed?

There is a similar question like this for Chromen - but not for FF.

There is a virtual beer for something which helps!

Thanks,

Upvotes: 0

Views: 116

Answers (1)

Kaspar Lee
Kaspar Lee

Reputation: 5596

Look at this page on MDN about Content Scripts

If you are using the High-Level API Tabs (which I think you are), you need to do the following:


Sending Data to Script

To send a message to the content script, underneath the var my_tab = tab.attach({ ... }), add the following:

my_tab.port.emit("my_message", "Message from the add-on")

Then inside the script you can use this to listen for the message:

self.port.on("my_message", function(data) {
    // Do stuff here!
    // data contains the data sent (i.e. "Message from the add-on")
})

Sending Data to index.js from Script

This is the bit you are probably interested in.

To send data from the script, use the following code:

self.port.emit("my_response", "Response from content script")

And to receive it in index.js:

my_tab.port.on("my_response", function(data) {
    // Do stuff here!
    // data contains the data sent (i.e. "Response from content script")
})

Upvotes: 1

Related Questions