Why, after changing a custom icon, does the browser icon activate by default?

I have a strange problem with the browser action icon in Chrome. The default icon for browser action is defined in the manifest. The icon is displayed correctly. Then on the original page, under certain conditions, I call:

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

This icon blinks momentarily and then returns to the default icon. The active tab and its identifier passed to setIcon remain unchanged during the whole process.

Can anyone suggest an idea why this could happen?

+4
source share
1 answer

The reason the icon was reset to default, because I called setIcon before , the tab finishes loading and gets a “full” state.

I suppose that in the documentation on tabs or on the actions of the browser , but I did not find it: the default icon is actually applied in design - to a specific page after the download is complete. I moved the setIcon call to the setIcon handler and now the custom icon is saved.

This contradicts my previous understanding that the browser action icon is set on the basis of each tab, without changes on the page loaded in the tab and its state.

@KonradDzwinel has kindly provided a simple extension to verify the case (look at the comments). I modified its background.js script to demonstrate this behavior:

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

To reproduce this behavior, on any tab, click the browser action icon to change it. Then refresh the page. As a result, the browser action icon reset returns to default.

If this is explained in some documents, write it in the comments and I will update the answer. From what I have read so far, I was convinced that the default icon is set for the new tab at the time of its creation, and then any changes in it are exclusively controlled by the extension.

+6
source

Source: https://habr.com/ru/post/1437595/


All Articles