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
tabIdparameter for chrome.browserAction.setPopup, but the docs are a bit scant) - Something else?
All help very much appreciated!
Option 1, bind an event listener:
Use
chrome.tabs.onUpdatedto listen for URI changes, followed bychrome.browserAction.setPopupwith a giventabIdto set the popup for the given tab. For example: