passing messages between tabs - possible?



Posts: 2

Joined: Thu Aug 11, 2011 7:12 pm

Post Thu Aug 11, 2011 7:14 pm

passing messages between tabs - possible?

I'm trying to figure out a way for my content script, which may be running in multiple tabs, to send messages back and forth...

I have a main.js with my pageMod code that assigns my content script, and it's all working great...

However, the tab object doesn't seem to have any method to dispatch messages to it... so while I can loop through an array of all of my tabs in main.js, I cannot post messages to my content script in those tabs...

Is there any way to achieve this, or is it not available in the SDK as it stands right now? Basically I'm looking for something akin to Chrome's: chrome.tabs.sendRequest(tabid, data) type functionality...

thanks for any assistance


AMO Administrator

Posts: 275

Joined: Thu Jun 23, 2011 4:08 pm

Post Fri Aug 12, 2011 3:15 pm

Re: passing messages between tabs - possible?

I'm not quite clear what your use case is, but a couple of helpful APIs in this area are:

1) - have a look at "Mapping workers to tabs" in the page-mod doc. You use this to fetch the tab a particular worker is loaded into: by keeping a reference to the worker (or even assigning it to the tab) you can send messages to that tab's worker any time you want.

See this example: ... 91/latest/ . Here, each time a page-mod attaches a content script to a tab (that is, when a matching page is loaded) we take a reference to the worker and assign it to that tab.

Then on the widget's click event we send a message to the active tab's worker.

So that's a way to send a message from a tab to a pre-existing content script.

2) tab.attach: this is usually an easier solution. Instead of sending a message to an existing content script, this enables you to execute a new content script on demand inside a tab. See: ... 98/latest/ for the above example rewritten using tab.attach.


Posts: 2

Joined: Thu Aug 11, 2011 7:12 pm

Post Fri Aug 12, 2011 3:29 pm

Re: passing messages between tabs - possible?

I actually got a solution in place just like what you've described with references to workers... my use case is perhaps not the greatest one, but reality being what it is, here's what my problem is:

I'm adapting a massively complex Greasemonkey script to make it into a Jetpack extension, and accessing SimpleStorage is, by nature, asynchronous. Accessing GM_getValue on the other hand was synchronous...

My options were either:

1) Rewrite MASSIVE amounts of code so that any time I was getting stored data, it would work asynchronously

2) Figure out a way to preload the extension's localStorage contents (in this case simple-storage) ahead of time, so that accessing it happened all on the content script side and could thus be synchronous...

So I went with option 2... but then ran into the issue of needing to "tell another tab" that stored content had been updated in the current tab - hence needing to talk across tabs...

This was solved by storing an array of workers, since a new one is generated for each living pageMod... I then cycled through the workers and posted messages to all of them - thus reaching the other tabs...


Posts: 47

Joined: Fri Jul 29, 2011 1:43 pm

Post Sat Aug 13, 2011 4:55 pm

Re: passing messages between tabs - possible?

I'd wondered about the process of porting GM scripts to jetpacks - thanks for the description. When you publish the extension feel free to ping me as I'd be interested in writing up a review of the process and your comments.

Background: I'm the newly hired Rocketeer dedicated to promoting the SDK's use, and one of the things I'll be doing is code reviews of interesting jetpack-based add-ons, particularly ones that have been ported from traditional Xul-based extensions or, in your case, from a Greasemonkey script.

Return to Add-on SDK & Add-on Builder

Who is online

Users browsing this forum: No registered users

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
CA Gen2 style designed by Vjacheslav Trushkin.