Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cool way to reset the browser action badge?

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,

like image 767
Alejandro Silvestri Avatar asked Aug 30 '13 15:08

Alejandro Silvestri


Video Answer


1 Answers

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.

like image 84
Timothée Boucher Avatar answered Sep 27 '22 21:09

Timothée Boucher