Here is my solution, I hope it will be helpful:
function setIcon(state, getIconNameCallback) {
// we need to set the icon globally first,
// to avoid blinking to default icon
chrome.windows.getLastFocused(null, function(window) {
chrome.tabs.getSelected(null, function(tab) {
chrome.browserAction.setIcon({
'path': getIconNameCallback(state, tab.url)
});
});
});
// then we must set the icon for each tab,
// without that the extension wont behave
// properly with multiple windows
chrome.tabs.query({}, function(tabs) {
for (var i = 0; i < tabs.length; i++) {
chrome.browserAction.setIcon({
'path': getIconNameCallback(state, tabs[i].url),
'tabId': tabs[i].id
});
}
});
}