Canvas was corrupted by cross-origin data via local chrome: // extension URL

I am working on a google chrome extension and I am trying to upload an image associated with the extension to the canvas.

var canvas = document.createElement('canvas');
canvas.width = 470;
canvas.height = 470;
var context = canvas.getContext('2d');
var image = new Image();
image.addEventListener('load', function(){
     //process
});
image.src = chrome.extension.getURL("asset/gotcha.png");

When I execute the code in the content of the script, I get:

Unable to get image data from canvas because the canvas has been  tainted by 
cross-origin data.

Is there any way to avoid this? I successfully embedded images, audio, video and flash directly into the target sites without any problems. The resource is specified in the web_accessible_resources file in the manifest file.

+4
source share
3 answers

-, .
:

:

  • ( script).
  • dataURL.
  • JS -, dataURL .
  • (dataURL) ( -) .

:

/* In `background.js` */
function injectImg(tabID, remoteCanvasID, imgPath) {
    var canvas = document.createElement("canvas");
    var img = new Image();
    img.addEventListener("load", function() {
        canvas.getContext("2d").drawImage(img, 0, 0);
        var dataURL = canvas.toDataURL();
        var code = [
            "(function() {",
            "    var canvas = document.getElementById(\"" + remoteCanvasID + "\");",
            "    var img = new Image();",
            "    img.addEventListener(\"load\", function() {",
            "        canvas.getContext(\"2d\").drawImage(img, 0, 0);",
            "    });",
            "    img.src = \"" + dataURL + "\";",
            "    ",
            "})();"].join("\n");
        chrome.tabs.executeScript(tabID, { code: code });
    });
    img.src = chrome.extension.getURL(imgPath);
}

chrome.runtime.onMessage.addListener(function(msg, sender)) {
    if (msg.action && (msg.action == "getImg")
            && msg.imgPath && msg.canvasID) {
        injectImg(sender.tab.id, msg.canvasID, msg.imgPath);
    }
});

/* In `content.js` */
chrome.runtime.sendMessage({
    action: "getImg",
    imgPath: "some/image.png",
    canvasID: "someCanvasID"
});

( script ), script. :.

  • script, dataURL .
  • , dataURL ( ).
  • chrome.runtime.getBackgroundPage(), window, dataURL , , dataURL .
+4

ExpertSystem, .

JavaScript , , .

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    if (request.message == "convert_image_url_to_data_url") {
      var canvas = document.createElement("canvas");
      var img = new Image();
      img.addEventListener("load", function() {
        canvas.getContext("2d").drawImage(img, 0, 0);
        sendResponse({data: canvas.toDataURL()}); 
      });
      img.src = request.url;
      return true; // Required for async sendResponse()
    }
  }
)

script:

//@success is the callback
function local_url_to_data_url(url, success) {  
  chrome.runtime.sendMessage(
    {message: "convert_image_url_to_data_url", url: url}, 
    function(response) {success(response.data)}
  );    
}
+5

Try adding your resources to a property web_accessible_resourcesat the top level of the manifest file, for example

    "web_accessible_resources": ["asset/gotcha.png"],

if you haven’t done it yet.

+2
source

All Articles