Edge: browserAction.setIcon not working when using a default icon with multiple sizes

こ雲淡風輕ζ 提交于 2019-12-24 07:32:34

问题


I can't get browser.browserAction.setIcon to work in Microsoft Edge when manifest.json is specifying a default icon in multiple sizes:

manifest.json

{
  "manifest_version": 2,
  "name": "test",
  "version": "0.0.1",
  "author": "John Doe",
  "background": {
    "scripts": ["background.js"],
    "persistent": false
  },
  "browser_action": {
    "default_icon": {
      "19": "icon.png",
      "38": "icon2.png"
    }
  }
}

background.js

setInterval(function() {
  browser.browserAction.setIcon({
      path: "testimage.png"
  });
}, 2000);

No error logged, the code is executed but the icon doesn't change. The same code works fine in Chrome.

Changing the manifest.json to

"browser_action": {
  "default_icon": "icon.png"
}

Fixes the issue, but what if I need to use multiple default icons?

EDIT:

Unfortunately not even "default_icon": "icon.png" is usable, even though Edge happily loads the extension, when submitting it to the store, the validation fails with

Validation failed: Invalid type: string (expected object) Schema location: /properties/browser_action/allOf/0/properties/default_icon/type Manifest location: /browser_action/default_icon Validation failed for extension manifest: Extension\manifest.json

Which is indeed what MDN says: https://developer.mozilla.org/en-US/Add-ons/WebExtensions/manifest.json/browser_action#Browser_compatibility

'default_icon' must be an object, with explicit sizes.


回答1:


In the end I came to the conclusion that Edge doesn't like string values for the browser action icon path, neither in manifest.json nor in browserAction.setIcon, it fails silently without producing any error.

A way to make it work is to always set explicit sizes in both manifest.json and in every browserAction.setIcon call, even if the different sizes all point to the same image.

manifest.json

{
  "manifest_version": 2,
  "name": "test",
  "version": "0.0.1",
  "author": "John Doe",
  "background": {
    "scripts": ["background.js"],
    "persistent": false
  },
  "browser_action": {
    "default_icon": {
      "19": "icon.png",
      "38": "icon2.png"
    }
  }
}

background.js

browser.browserAction.setIcon({
    path: {
      "19": "testimage.png",
      "38": "testimage.png"
});


来源:https://stackoverflow.com/questions/47271670/edge-browseraction-seticon-not-working-when-using-a-default-icon-with-multiple

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!