Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can't directly pass an image from your extension to a web-page's canvas without making it tainted.<br> This is a work-around:</p> <p><strong>Description:</strong></p> <ol> <li>You access the image from your background page (or content script).</li> <li>You put it in a canvas and convert it to a dataURL.</li> <li>You inject some JS code into the web-page, passing the dataURL as a string.</li> <li>The injected code uses the string (dataURL) to create an image (in the context of the web-page) and draw it onto a canvas.</li> </ol> <p><strong>Sample code:</strong></p> <pre><code>/* 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 &amp;&amp; (msg.action == "getImg") &amp;&amp; msg.imgPath &amp;&amp; msg.canvasID) { injectImg(sender.tab.id, msg.canvasID, msg.imgPath); } }); /* In `content.js` */ chrome.runtime.sendMessage({ action: "getImg", imgPath: "some/image.png", canvasID: "someCanvasID" }); </code></pre> <p>This is a more generic approach (that can be used by any content script with minimum configuration), but it might be simpler to move part of the logic to the content script. E.g.:</p> <ul> <li>Define a function within the content script, that when called with a dataURL creates and draws an image onto a canvas.</li> <li>Define a function in the background page, that takes an image-path and returns a dataURL (as seen above).</li> <li>Use <strong>chrome.runtime.getBackgroundPage()</strong> to get a reference to the background page's <code>window</code> object, call the function to convert an image-path to a dataURL and finally use that dataURL to create an image and draw it onto a canvas.</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload