mirror of
https://github.com/niklasvh/html2canvas.git
synced 2023-08-10 21:13:10 +03:00
Fix image loading for cross-origin resources
This commit is contained in:
@ -39,7 +39,7 @@ ImageLoader.prototype.loadImage = function(imageData) {
|
||||
} else if (this.support.cors && !this.options.allowTaint && this.options.useCORS) {
|
||||
return new ImageContainer(src, true);
|
||||
} else if (this.options.proxy) {
|
||||
return new ProxyImageContainer(src);
|
||||
return new ProxyImageContainer(src, this.options.proxy);
|
||||
} else {
|
||||
return new DummyImageContainer(src);
|
||||
}
|
||||
|
33
src/proxyimagecontainer.js
Normal file
33
src/proxyimagecontainer.js
Normal file
@ -0,0 +1,33 @@
|
||||
function ProxyImageContainer(src, proxy) {
|
||||
var callbackName = "html2canvas_" + proxyImageCount++;
|
||||
var script = document.createElement("script");
|
||||
var link = document.createElement("a");
|
||||
link.href = src;
|
||||
src = link.href;
|
||||
var requestUrl = proxy + ((proxy.indexOf("?") > -1) ? "&" : "?" ) + 'url=' + encodeURIComponent(src) + '&callback=' + callbackName;
|
||||
this.src = src;
|
||||
this.image = new Image();
|
||||
var image = this.image;
|
||||
this.promise = new Promise(function(resolve, reject) {
|
||||
image.onload = resolve;
|
||||
image.onerror = reject;
|
||||
|
||||
window[callbackName] = function(a){
|
||||
if (a.substring(0,6) === "error:"){
|
||||
reject();
|
||||
} else {
|
||||
image.src = a;
|
||||
}
|
||||
window[callbackName] = undefined; // to work with IE<9 // NOTE: that the undefined callback property-name still exists on the window object (for IE<9)
|
||||
try {
|
||||
delete window[callbackName]; // for all browser that support this
|
||||
} catch(ex) {}
|
||||
script.parentNode.removeChild(script);
|
||||
};
|
||||
script.setAttribute("type", "text/javascript");
|
||||
script.setAttribute("src", requestUrl);
|
||||
document.body.appendChild(script);
|
||||
});
|
||||
}
|
||||
|
||||
var proxyImageCount = 0;
|
Reference in New Issue
Block a user