Using chrome.browserAction.setPopup per tab

I'm writing a Chrome extension which dynamically changes the content of the popup window based on the current URL.

I'm doing something like this in background.js, which works fine:

if(domains.contains(request.url)){
    chrome.browserAction.setPopup({
        popup: "tracking.html"
    });
}else{
    chrome.browserAction.setPopup({
        popup: "nottracking.html"
    });
}

The problem is that if I switch tab, the content of the popup stays the same between tabs. What's the correct strategy to deal with this?

  • Hook into the tab change event somehow (if such a possibility exists)?
  • Limit the change of popup contents to the current tab? (I did notice that there's an optional tabId parameter for chrome.browserAction.setPopup, but the docs are a bit scant)
  • Something else?

All help very much appreciated!

Answers


Option 1, bind an event listener:

Use chrome.tabs.onUpdated to listen for URI changes, followed by chrome.browserAction.setPopup with a given tabId to set the popup for the given tab. For example:

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
    if (domains.contains(tab.url)) {
        chrome.browserAction.setPopup({
            tabId: tabId,
            popup: 'tracking.html'
        });
    } else {
        chrome.browserAction.setPopup({
            tabId: tabId,
            popup: 'nottracking.html'
        });
    }
});

Need Your Help

How can I get jQuery to call an event handler when an image actually appears on-screen?

javascript jquery html events dom

How can I use jQuery to trigger a callback of a JavaScript function when I scroll down and see a picture? I wish to delay loading certain images until they actually appear on-screen... Ideally, I'd...

Send and Receive a file in socket programming in Linux with C/C++ (GCC/G++)

c++ c linux sockets network-programming

I like to implement a client-server architecture running on Linux using sockets and C/C++ language that is capable of sending and receiving files. Is there any library that makes this task easy? Co...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.