4

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?

Stan
  • 8,683
  • 9
  • 58
  • 102
  • 2
    I made a small extension to test that. It changes the browser action icon periodically and everything seems to work just fine: https://dl.dropbox.com/u/8989748/catExtension.zip . So your problem is not in the `chrome.browserAction.setIcon`, you need to give some more details. Try to reproduce your bug on my minimal example. – Konrad Dzwinel Oct 03 '12 at 16:14
  • @KonradDzwinel, your extension works, thanks. Mine works as well - in regard to setting icon, but then it strangely changes back - that was the question, and you are right that there was something more important, which I couldn't figure out until last minute ;-). Just now I got it and going to post an answer. – Stan Oct 03 '12 at 16:57
  • I'm glad you solved it. Thanks for posting such a exhaustive answer, this information may come in handy one day. I agree that this should be documented. Consider reporting this on http://crbug.com. – Konrad Dzwinel Oct 03 '12 at 18:29

1 Answers1

7

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.

Stan
  • 8,683
  • 9
  • 58
  • 102
  • thank you. same problem. was. documetation says: `Automatically resets when the tab is closed.`. :( – befzz Jun 16 '15 at 10:56