Rob
Rob

Reputation: 21

Load popup.html from chrome.pageAction.onClicked.addListener

I have a page action popup that works when loaded from the manifest file. However I want to get the tab information for the tab that was clicked to launch the popup. I can get the tab information from chrome.pageAction.onClicked.addListener but I don't know how to launch popup.html from inside pageAction.onClicked.

Upvotes: 1

Views: 1170

Answers (2)

Rob
Rob

Reputation: 21

I found a workaround for what I wanted to do. On the background page:

chrome.tabs.onActiveChanged.addListener(OnActiveChanged);  
function OnActiveChanged( tabId, selectInfo )  
{  
  chrome.tabs.get( tabId, function( tab ){  
    window.activeTab = tab;  
  } );  
}  

This captures the tab each time the tab changes. Then in my popup's function gets the tab from the background page:

function OnLogin( )  
{
  backgroundWindow = chrome.extension.getBackgroundPage();  
  var activeTab = backgroundWindow.activeTab;  
...  
}

Be careful when you debug the code though. The debugger causes a tab change event which changes the tab away from the tab that launched the popup.

Upvotes: 1

pimvdb
pimvdb

Reputation: 154868

You cannot have both a pageAction.onClicked:

onClicked

This event will not fire if the page action has a popup.

What you can do, though, is fetching the current tab information with the Tabs module when the popup is loaded:

chrome.tabs.getCurrent(function(tab) {
    // tab contains information about the current tab
});

Upvotes: 2

Related Questions