I have an extension that implements a browser action. Of course, the browser action is allways visible, but it has a special significance in certain urls. So, I use filtered events to listen to those urls and set the proper badge
chrome.webNavigation.onDOMContentLoaded.addListener(
function(tab){
chrome.browserAction.setBadgeText({
text:'bdge',
tabId: tab
});
},
{'url':[{hostSuffix: 'somedomain.com', pathPrefix: 'somePath/'}]}
);
Is there some "elegant" way to reset the badge when the user navigates out from that page, without listening every single tab navigation? Should I execute a content script to hang on some exiting event and send a message?
Thank you very much,
It seems to me that a good solution would be to use chrome.tabs.onUpdated
.
In your background page, you would have something like that:
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
// using a regex or however else you want to test the URL
if (/somedomain\.com\/somePath\//.test(changeInfo.url)) {
chrome.browserAction.setBadgeText({
text: 'bdge',
tabId: tabId
});
} else {
chrome.browserAction.setBadgeText({
text: '',
tabId: tabId
});
}
});
I know you wrote "without listening every single tab navigation" but I'm not sure why you want to avoid this.
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