Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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});
   };
});
like image 868
o0o0o Avatar asked May 17 '14 16:05

o0o0o


People also ask

What is Chrome browseraction?

Description. Use browser actions to put icons in the main Google Chrome toolbar, to the right of the address bar. In addition to its icon, a browser action can have a tooltip, a badge, and a popup.

What is browser action icon?

A browser action is a button that your extension adds to the browser's toolbar. The button has an icon, and may optionally have a popup whose content is specified using HTML, CSS, and JavaScript.


2 Answers

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

like image 188
Xan Avatar answered Sep 22 '22 15:09

Xan


 chrome.browserAction.setBadgeText({text: newText}, tab.id);  //<<this is not working to me

 chrome.browserAction.setBadgeText({text: "Phish", tabId: tab.id}); //<<This is working to me
like image 36
Touhid Avatar answered Sep 25 '22 15:09

Touhid