How can i keep chrome.browserAction.setBadgeText for each tab?

google-chrome-extension, javascript

Solution

Two key points should help you with your troubles.

1) `chrome.browserAction.setBadgeText` has an optional parameter, `tabId`, that binds the value to the tab.

2) You should filter `chrome.tabs.onUpdated` events by `changeInfo`'s fields.

So, change your code to:

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab){
    function(tabId, changeInfo, tab){
        if(!changeInfo.url) return; // URL did not change
        // Might be better to analyze the URL to exclude things like anchor changes

        /* ... */
        chrome.browserAction.setBadgeText({text: newText, tabId: tab.id});
    };
});

This might not catch new tabs' creation; if it doesn't, also listen to `onCreated`

Problem

My extension gets data using tab.url and puts it in chrome.browserAction.setBadgeText. When i open a new tab it resets. How can i update BadgeText only for a new tab? and keep it unchanged for an old one? extension layout: ``` chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab){ function(tabId, changeInfo, tab){ //using tab.url and XMLHttpRequest() i get newText for: chrome.browserAction.setBadgeText({text: newText}); }; }); ```

Original source