Volder
Volder

Reputation: 992

Chrome extension icon change issue

I'm developing a Chrome extension and have a strange problem with icon change on-the-fly.

In my popup.html I have a button by clicking on which I want to change the icon of the extension in the browser. E.g. from colored one to black and white (when application is inactive).

So the function which is responsible for this:

    function toggleActivated(){
        localStorage.isActive = toBool(localStorage.isActive) ? false : true;
        $('#activate-disactivate span').text(toBool(localStorage.isActive) == false ? 'Включить' : 'Выключить');
        chrome.browserAction.setIcon({path: toBool(localStorage.isActive) ? '48.png' : '48_bw.png'});
        //window.close();
     }

But the problem is that I want to close popup after the icon changed. If I use the window.close() at the end - then the icon is not changed, but if it is commented out - then the icon is changed fine.

Why is there a conflict between chrome.browserAction.setIcon() and window.close()?

Upvotes: 2

Views: 1549

Answers (2)

Luiz Aoqui
Luiz Aoqui

Reputation: 271

I know this is old, but I was having the same problem and abraham was right when he said that chrome.browserAction.setIcon was being called asynchronously. But I don't think that setting a timeout is the best answer.

If you check the setIcon documentation you can see that this method takes a function callback as parameter. That's where you should call window.close(). Like this:

chrome.browserAction.setIcon({ path: icon_path }, function() {
    window.close();
});

UPDATE: As niraj.nijju pointed in the comment below, you can pass a tabId param to the setIcon function to limit the scope of the change.

Upvotes: 2

abraham
abraham

Reputation: 47833

It sounds like chrome.browserAction.setIcon is getting fired asynchronously but the popup is getting closed before it finishes. You could try adding a 500ms setTimeout before closing.

You should also file a bug report at new.crbug.com.

Upvotes: 2

Related Questions