mystarrocks
mystarrocks

Reputation: 4088

Access contentScript variables in addon context and vice versa?

This one's perhaps a duplicate of "variable not recognized inside contentscript" under the same section. I got part of my query solved there from the answer to that question. Yeah, I understand that

The content script context is entirely disconnected from the addon script context. Content scripts are run in the context of the document, while addon scripts aren't.

But does that mean we can never access a variable in the content script context in the addon script context? If by any means we could access them, please do let me know. My requirement needs objects to be sent as parameters to functions in another script(data/utilities.js) and possibly get the returned object. There was no difficuty in doing the former but am stuck with the latter cos of the aforementioned context problem. I am able to return the value from the content script context but unable to access the same in the addon context. Can anyone please help me out with a little example of this?

PS I could as well discussed it there but I read that I shouldn't as this ain't a discussion forum.

Upvotes: 1

Views: 250

Answers (1)

therealjeffg
therealjeffg

Reputation: 5830

You cannot get direct access to variables in the content script from the addon script context directly. You can pass the variable back to the add-on from the content script using

self.port.emit('send-some-var', some_var)

You would then receive the variable's value in the add-on script by listening for the same event:

worker.port.on('send-some-var', function(data) { console.log(data) })

The main limitation however is that the data being passed through must be JSON-serializable, so you could not have a complex object with methods, etc. Only data.

Upvotes: 1

Related Questions