为什么这个chrome.browserAction.setIcon方法不起作用? [英] why is this chrome.browserAction.setIcon method not working?

查看:550
本文介绍了为什么这个chrome.browserAction.setIcon方法不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看文档页,我无法确定在我的代码中出现了什么错误:

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

文件说明:


请注意,'details.imageData = foo'相当于
'details.imageData = {'19':foo}'


所以我非常困惑

解决方案

你的代码基本上是一个很大的语法错误。一个JavaScript对象字面值希望是一个 key:value 对的列表。您不能(也不需要)在部分的任何分配。



仅限语法错误,它会是:

  //仍然错误:
chrome .browserAction.setIcon({
imageData:{
48:Icons / iconfavorite48x.png,
64:Icons / iconfavorite64x.png,
128:Icons / iconfavorite128x.png
}
});

这会失败。 imageData 期望从< canvas> 获得像素数据的二进制blob。如果你想提供路径,你需要使用 path 属性:

  //仍然错误:
chrome.browserAction.setIcon({
path:{
48:Icons / iconfavorite48x.png,
64:图标/iconfavorite64x.png,
128:Icons / iconfavorite128x.png
}
});

请注意,您只能提供预期的大小。如果你包含任何其他的,它会失败。引用文档:


如果适合一个屏幕空间单位的图像像素数量等于缩放比例,则尺寸缩放* 19的图像将会被选中。最初只支持1和2的缩放。


正常大小的图标是19x19像素;在高DPI屏幕上,Chrome可能会显示一个38x38图标。



更新:由于Chrome已在53中切换到Material Design,因此现在分别需要16x16和32x32。您可以提供旧尺码和新尺码,而不会有任何错误。



所以你可以这样做:

p $ p $ //改正
chrome.browserAction.setIcon({
path:{
19:Icons / iconfavorite19x.png,
38:Icons / iconfavorite38x.png
}
});

//同样正确
chrome.browserAction.setIcon({
path:{
19:Icons / iconfavorite19x.png
}
});

//同样正确
chrome.browserAction.setIcon({
path:Icons / iconfavorite19x.png
});

图片不要具有这些尺寸,它们会如有必要缩放;但它确实更好。


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

解决方案

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.

这篇关于为什么这个chrome.browserAction.setIcon方法不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆