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?
tabId
parameter for chrome.browserAction.setPopup, but the docs are a bit scant)All help very much appreciated!
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'
});
}
});
Simply pass the TabId to the setPopup call. http://code.google.com/chrome/extensions/browserAction.html
I do this in my background page all the time:
chrome.browserAction.setPopup({"tabId":data.tabId,"popup":url});
Works like a charm.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With