Using chrome.browserAction.setPopup per tab
google-chrome, google-chrome-extension
Solution
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'
});
}
});
Problem
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!