问题描述:

Trying to make a Chrome extension, and for some reasons beyond me, I have to load images from a url hosted elsewhere...

{

"name": "Name",

"version": "1.0",

"manifest_version": 1,

"description": "A something something something",

"browser_action": {

"default_icon": "favicon.ico",

"default_popup": "popup.html"

},

"icons": {

"128": "logo_white.gif"

}

}

How can I load favicon.ico and logo_white.gif from http://www.example.com/favicon.ico and http://www.example.com/logo_white.gif instead? I'm baffled since I'm reasonably new to chrome extensions and their use of JSON.

Thanks

网友答案:

You can load the data at runtime as an image tag, then stick it in a canvas object, extract an ImageData and pass that to the setIcon API.

Something like the following in a background.js would do the trick:

var canvas = document.createElement("canvas");
var img = new Image();
img.onload = function() {
  document.body.appendChild(canvas);
  var context = canvas.getContext("2d");
  context.drawImage(this, 0, 0);
  var imageData = context.getImageData(0, 0, img.width, img.height);
  chrome.browserAction.setIcon({imageData: imageData});
}
img.src = "http://www.example.com/logo_white.gif";
网友答案:

You can't use icons located outside of your extension. Your extension will be installed on a local computer and Google Chrome should be able to get your extension's icon when there's no internet connection available.

相关阅读:
Top