Dexter
Dexter

Reputation: 1178

Selecting Browser Action for Current Tab in Chrome Extension

I'm working on a chrome extension that manipulates certain cookies. Most of the manipulation takes place in the background service, but I need to update the icon and pass data to the browser action for the current tab.

I'm looking for an action similar to the AdBlock extension. AdBlock loads a small number in the bottom right of the icon for the number of ads blocked, so it varies from tab to tab.

When I perform this action from the background service, it seems to change across all browsing tabs. Can someone with experience in extensions point me in the right direction for this one?

Upvotes: 0

Views: 700

Answers (2)

Marc
Marc

Reputation: 3642

This should get you started.

setInterval(function(){//every second
    chrome.tabs.getSelected(null,function(tab) {//on the current tab,
        chrome.browserAction.getBadgeText({tabId:tab.id}, function(badgeText){//get the tab's badge text
            if(badgeText.length<1){
                badgeText="0";//set the text if its empty
            }
            chrome.browserAction.setBadgeText({tabId:tab.id,text:badgeText/1+1+""});//and add one.
        });
    });
},1000);

make sure you don't run this in the console, because chrome will get the developer tool window id, and since no valid tab has that id, it will change every single tab's badgeText.

Upvotes: 1

Daniel Herr
Daniel Herr

Reputation: 20478

You just need to include the tab id when you set it, such as:

chrome.browserAction.setBadgeText({ text: "5", tabId: tab.id })

Upvotes: 1

Related Questions