Skip to content
Advertisement

why is this chrome.browserAction.setIcon method not working?

I’m looking at the documentation page and I can’t figure out whats wrong in my code:

JavaScript

the documentaion says :

Note that ‘details.imageData = foo’ is equivalent to ‘details.imageData = {’19’: foo}’

so I’m extremely confused

Advertisement

Answer

Your code is basically a big syntax error. A JavaScript object literal expects to be a list of pairs key: value. You can’t (and don’t need) any assignments there in the key part.

So, fixing only the syntax error, it will be:

JavaScript

This will fail. imageData expects binary blobs of pixel data obtained, for example, from <canvas>. If you want to supply paths, you need to use path property:

JavaScript

Note that you can only provide sizes it expects. If you include any other, it will fail. Quoting the docs:

If the number of image pixels that fit into one screen space unit equals scale, then image with size scale * 19 will be selected. Initially only scales 1 and 2 will be supported.

A normal-sized icon is 19×19 pixels; on high-DPI screens Chrome may show a 38×38 icon.

Update: since Chrome has switched to Material Design in 53, this now expects 16×16 and 32×32 respectively. You can supply both old and new sizes without errors.

So you can do this:

JavaScript

The images don’t have to have these dimensions, they will be scaled if necessary; but it’s of course better to be exact.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement