I'm writing a chrome extension (content script) that gets some photo url from an api and shows it in some place on the current page.
The problem is that sometimes the photo url is http (to a photo hosted on a website, not from my server), but the current page is https, which leads to mixed-content errors.
So, I've tried using a background.js script, sending it a message from my content-script to download the image.
I've tried two methods to download and store the image in background.js -
function getImageDataURL(url, callback) {
var data, canvas, ctx;
var img = new Image();
img.setAttribute('crossOrigin', 'anonymous');
img.onload = function () {
canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
callback(canvas.toDataURL());
};
img.src = url;
}
which fails due to:
Image from origin 'http://www.not-my-server.com' has been blocked from loading by Cross-Origin Resource Sharing policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'chrome-extension://...' is therefore not allowed access.
and also:
jQuery.ajax({
url: url,
//data: myData,
type: 'GET',
crossDomain: true,
dataType: 'jsonp',
success: function() { callback(false) },
error: function() { callback(true) }
});
Which failed due to:
Refused to load the script 'http://www.not-my-server.com/...' because it violates the following Content Security Policy directive: "script-src 'self' https://*.my-server.com https://localhost:4443/".
Should I change my chrome-extension's CSP to include all images somehow? Is there some other way to do it? All I want is to simply temporarily download a jpg in the background, and use it in the content-script html.
Thanks.