chrome.pageAction.show() - getting ID of executing tab from content script

2,262 views
Skip to first unread message

bzlm

unread,
Mar 15, 2010, 1:42:06 PM3/15/10
to Chromium-extensions
According to the documentation, "when you show a page action, you
specify the tab in which the icon should appear". But how do you get
the tab ID of the executing tab? And does it require giving the
extension permissions to tabs?

I have always assumed the signal to show a page action would come from
a content script that has examined the page and found that an action
is suitable (for example, a feed reading extension would show a page
action to subscribe to the feed when it finds a feed on the page). But
I can only think of contrived ways to get the ID of the tab in which
the content script is executing, and which require giving the
extension permissions to tabs, which I don't really want. Maybe I'm
wrong in assuming a content script should trigger the
chrome.pageAction.show()?

Antony Sargent

unread,
Mar 17, 2010, 2:46:04 PM3/17/10
to bzlm, Chromium-extensions
The standard pattern for this is to use one of the two messaging API choices to have your content script tell the background page to turn on the page action. Both the chrome.extension.onRequest and chrome.extension.onConnect events include a MessageSender with the details about the tab including its id.

In your content script:

chrome.extension.sendRequest("show_page_action");


In your background page:

chrome.extension.onRequest.addListener(function (request, sender) {
  if (request == "show_page_action") {
    chrome.pageAction.show(sender.tab.id);
  }
});



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