Question

I have a strange problem with a browser action icon in Chrome. There is a default icon for browser action defined in manifest. The icon is displayed correctly. Then in a background page, under some conditions, I call:

chrome.browserAction.setIcon({path:"green_32.png", tabId:request.tabId});

This icon blinks for a moment, and then changes back to the default icon. The active tab and its id passed to setIcon remain the same during all the process.

Can someone suggest an idea why this can happen?

Was it helpful?

Solution

The reason why the icon was reset to default state every time is because I called setIcon before the tab finishes loading and obtains "complete" state.

I guess there should be some information about this in documentation on tabs or on browser actions, but I didn't find it: the default icon is actually applied - by-design - to a specific page after it finishes loading. I moved the call setIcon into tabs.onUpdated handler, and now custom icon persists.

This contradicts to my former understanding that the browser action icon is set on a per tab basis, regarless to a page loaded into the tab and its state.

@KonradDzwinel kindly provided a simple extension to test the case (look at the comments). I changed its background.js script to demonstrate this behaviour:

chrome.browserAction.onClicked.addListener(function(tab)
{
  chrome.browserAction.setIcon({path: 'gfx/icon2.png', tabId: tab.id});
});

To reproduce this behaviour, on any tab press the browser action icon to get it changed. Then refresh the page. As a result the browser action icon reset back to default.

If this behaviour is explained in some documentation, please, write this in comments, and I'll update the answer. From what I have read so far, I was convinced that default icon is set for new tab at its creation time, and then any changes to it are solely under extension's control.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top