I am working on a project that requires certain images that a user selects to be processed such that their image data is encoded in base64. Ideally I would like to do this from within a content script but can't because of CORS * so I have no other option but to delegate this to the background page. Now, since send.message only accepts JSON serializable data, I am unable to send a direct reference to an HTMLImageElement instance to the background page and have to pass its src attribute instead. In the background page, I then instantiate an Image, set its src element and carry out the required processing once the onload event fires.
My question is this: what will the browser do when the Image instance above is created and its src set? Will it simply retrieve the image data from its cache store, since it's just been loaded in a tab, or will the browser attempt to re-download the image? The latter isn't desirable since there is a chance that the image won't be available due to hot linking countermeasures by some servers or some other techniques.
What real options have I got to ensure that the same image that was loaded in the tab is made available to the background page?
* When attempting to retrieve image data in base64 encoding via canvas trickery in the content script, I run into the exception "Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported." Hence why I'm delegating to the background page.
Here is some code to help with some context.
In the content script:
// Can't process image in content script due to CORS which leaves the
// background page as the only real option.
//
// Can't send reference to image element either because it needs to be
// serializable.
chrome.runtime.sendMessage( { operation: 'get-image-base64', src: img.src },
In the background page, portion of function responsible for processing the get-image-base64message:
var img;
ent = ent.trim();
img = new window.Image();
img.src = /^\/\//.test(ent) ? "http:" + ent : ent;
/* Set up events. */
img.onload = function () { callback(getImageData_(img)); };
img.onerror = function () {
console.error("Failed to load image: %s", img.src);
};
getImageData_ above simply uses some canvas trickery to retrieve the image data in base64 encoding.
--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To post to this group, send email to chromium-...@chromium.org.
Visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/d568bd7b-c4ed-4b35-9a6b-a0aa73848649%40chromium.org.
For more options, visit https://groups.google.com/a/chromium.org/d/optout.