Join the Stack Overflow Community
Stack Overflow is a community of 6.3 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I want to be able to change the icon of my extension according to what site I am currently browsing. How can I listen for changes in tab focus?

share|improve this question

I think I've figured this one out. You need two listeners. One to sense when the tab has been changed, one to sense when it's been updated. And then they both can trigger the same function too run. Here's what would be in the background file...

function changeIcon() {
    //query the information on the active tab
    chrome.tabs.query({active: true}, function(tab){
        //pull the url from that information
        var url=tab[0].url;
        //do whatever you need to do with the URL
        //alert(url);
        //change the icon
        chrome.browserAction.setIcon({path: 'pathToIcon'});
    });
}

//listen for new tab to be activated
chrome.tabs.onActivated.addListener(function(activeInfo) {
    changeIcon();
});

//listen for current tab to be changed
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
    changeIcon();
});
share|improve this answer

Simply register for tab update notifications in your background page:

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab)
{
    if (changeInfo.status == "loading")
    {
        var url = tab.url;
        var iconPath = ???
        chrome.pageAction.setIcon({tabId: tabId, path: iconPath});
    }
});

This handler will be called whenever a tab changes location. You don't need to care which tab is currently selected because you will have defined a different icon for each tab. Still, if you want to do it - http://code.google.com/chrome/extensions/tabs.html#event-onSelectionChanged is the way to go.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.