Monday, May 20, 2024
144
rated 0 times [  150] [ 6]  / answers: 1 / hits: 15205  / 9 Years ago, wed, april 29, 2015, 12:00:00

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



chrome.browserAction.setIcon({
details.imageData = {
48: Icons/iconfavorite48x.png,
64: Icons/iconfavorite64x.png,
128: Icons/iconfavorite128x.png
}
});


the documentaion says :




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




so I'm extremely confused


More From » google-chrome

 Answers
36

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:



// Still wrong:
chrome.browserAction.setIcon({
imageData : {
48: Icons/iconfavorite48x.png,
64: Icons/iconfavorite64x.png,
128: Icons/iconfavorite128x.png
}
});


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:



// Still wrong:
chrome.browserAction.setIcon({
path : {
48: Icons/iconfavorite48x.png,
64: Icons/iconfavorite64x.png,
128: Icons/iconfavorite128x.png
}
});


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 19x19 pixels; on high-DPI screens Chrome may show a 38x38 icon.



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



So you can do this:



// Correct
chrome.browserAction.setIcon({
path : {
19: Icons/iconfavorite19x.png,
38: Icons/iconfavorite38x.png
}
});

// Also correct
chrome.browserAction.setIcon({
path : {
19: Icons/iconfavorite19x.png
}
});

// Also correct
chrome.browserAction.setIcon({
path : Icons/iconfavorite19x.png
});


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


[#66825] Tuesday, April 28, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mahogany

Total Points: 645
Total Questions: 107
Total Answers: 98

Location: Israel
Member since Wed, Apr 14, 2021
3 Years ago
;