[reposting from right address this time]
On Mon, Jun 8, 2009 at 2:17 PM, yohann.richard <[email protected]> wrote:
>
> toolstrip snippet :
> var myPorts=[];
> chrome.self.onConnect.addListener(function(port) {
> myPorts.push(port); // I need to add some code here to filter ports
> coming from other tabs
> });
> function sendMsg(msg){
> myPorts.forEach(p){
> p.postMessage(msg);
> }
> }
>
> Is this the proper way to do this ?
That's the way to broadcast to all content scripts, yes. I'm thinking about
adding a way for extensions to open a channel to content scripts that could
make this easier (right now only content scripts can open the channel).
If you want to communicate with only the current tab, there are a few ways
you can do this. Probably the easiest right now is to listen for the
"focus" event in your content script. When the tab containing your script
is focused, it will get this event. That script can then tell your
toolstrip that it is focused and you can keep track of the last focused
tab's port. I believe the "Subscribe in Google Reader" (
http://dev.chromium.org/developers/design-documents/extensions/samples)
extension
contains an example of using the focus event.
Another option: each port object has a tab object identifying the tab that
opened the connection. You can use the tab ID in conjunction with the
tabs.onSelectionChanged event to only send messages to ports for the
selected tab.
If any of my iframes gets replaced by another url, or is reloaded, I
> will get a new port registered, but I will also have an invalid port
> left in my list.
Right. We'll probably add a notification to let you know when ports are
closed, but for now, sending messages through dead ports is just ignored.
> If I may suggest, it would be interesting to get the API to provide us
> the list of active ports opened relative to a specific page.
> A broadcasting mechanism could come handy as well.
This is something we're considering.
> Also, is it a fair assumption that content_script will open only one
> connection per frame ?
No, content scripts are free to open as many ports as they like.
> Or will we get the possibility to provide an ID when creating ports ?
I like this idea. I'll add it to the to-do list.
Hope this helps!
- Matt Perry
--~--~---------~--~----~------------~-------~--~----~
Chromium Discussion mailing list: [email protected]
View archives, change email options, or unsubscribe:
http://groups.google.com/group/chromium-discuss
-~----------~----~----~----~------~----~------~--~---