Current tab id

2,211 views
Skip to first unread message

Jason Barnabe (np)

unread,
Feb 15, 2010, 11:52:25 PM2/15/10
to Chromium-extensions
Is there a way to get the id of the current tab a script is running
in? chrome.tabs.getSelected returns the selected tab, but this could
be different because the script could be running on a tab in the
background.

Matt Perry

unread,
Feb 16, 2010, 2:11:01 PM2/16/10
to Jason Barnabe (np), Chromium-extensions
If you send a message from the script, you will have access to a 'sender' object. From there, the tab ID is accessible via 'sender.tab.id'. See http://code.google.com/chrome/extensions/messaging.html for more info.


--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.
For more options, visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/?hl=en.


Jason Barnabe

unread,
Feb 16, 2010, 2:22:31 PM2/16/10
to Matt Perry, Chromium-extensions
Let me explain a bit more what I'm trying to do:

I have multiple extension pages that the user can open. They allow the
user to edit a record and save. When the user saves, I send out a
notification so that the other pages can update themselves with the
new data, if necessary. So each page can send notifications, and they
also listen for the same kind of notification.

What I'm trying to avoid is having the page that sends the
notification receive its own notification and update itself. In my
listener, I want to look at sender.tab.id to see which tab sent the
notification, and if it's the same as the current tab, it should do
nothing.

I could always set some other variable or property in each tab to
uniquely identify them, but it seems that being able to access a tab's
ID would've been something that's possible...

Matt Perry

unread,
Feb 16, 2010, 2:41:01 PM2/16/10
to Jason Barnabe, Chromium-extensions
Does the sending page ever need to be a content script? Are the receiving pages ever a content script?

If you want to send to both content scripts and other extension pages, you will need to use both APIs. (Recall I said before that sending to a content script uses a different API than to an extension page.) To broadcast to all content scripts, you'll need to manually iterate through all windows and tabs and send a message to each. Something like:
  chrome.windows.getAll({populate: true}, function(windows) {
    for (var i in windows.tabs) {
      chrome.tabs.sendRequest(windows.tabs[i].id, message, responseHandler);
    }
  });

Broadcasting to all extension pages is simpler:
   chrome.extension.sendRequest(message, responseHandler);
You don't have to handle the case of a page getting its own message for this case. Those are already filtered out.

Michael

unread,
Feb 16, 2010, 4:07:20 PM2/16/10
to Chromium-extensions
I don't actually have "tabs" permission in my extension (lack of
foresight), so what I do is make a call to the background page.
onRequest accepts a function with three arguments, the second of which
is 'sender'. So the background page's handler can just call
sendResponse(sender.tab.id). The content script's call thus looks
something like

chrome.extensions.sendRequest('tabid-request', function(tabid) { /* do
something with the tab id */ });

and the background page has something like

chrome.extensions.onRequest.addListener(function(request, sender,
sendResponse) {
if (request == 'tabid-request')
sendResponse(sender.tab.id);
});

It's asynchronous, but it's better than nothing.

Hope this helps,
Michael

On Feb 15, 11:52 pm, "Jason Barnabe (np)" <jason.barn...@gmail.com>
wrote:

Reply all
Reply to author
Forward
0 new messages