How to get current tab id ?

14,538 views
Skip to first unread message

FM

unread,
Nov 11, 2010, 6:58:40 PM11/11/10
to Chromium-extensions
Hi so im adding a button to the current page on, and I would like to
have the click event open a link in the current tab.

so i set the callback function of the button to send a request to my
background.html page. But when it asks for the current tab it returns
undefined. I think this is because the background.html page has no
context of a current tab selected.

I tried in the content script but they are not allowed to make
chrome.tabs* calls.

Any ideas ?

Derek 囧

unread,
Nov 11, 2010, 7:17:48 PM11/11/10
to Chromium-extensions
If you want to get current tab id in Background page, then use:

chrome.tabs.getSelected(null, function(tab{
//DO WHATEVER YOU WANT BETWEEN HERE,
var id=tab.id
alert(tab.id)
//AND HERE.
}))

If you want to get current tab id in Content Script, then you can use
Port to sent message and let background page to send back the number
to content script.

Document: http://goo.gl/co9t

Be sure to add the permission "tabs" first.

-Derek

Arne Roomann-Kurrik

unread,
Nov 11, 2010, 8:13:58 PM11/11/10
to FM, Chromium-extensions
You could do this with message passing, but if all you want to do is change the URL, why not just call window.location = <url> in the button click handler?

~Arne



--
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.


Reply all
Reply to author
Forward
0 new messages