Fix image loading for cross-origin resources

This commit is contained in:
Niklas von Hertzen 2014-02-17 00:04:09 +02:00
parent 9af96d3812
commit 82e5a8a7c0
5 changed files with 73 additions and 6 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -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);
}

View 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;

View File

@ -11,7 +11,7 @@ var h2cSelector, h2cOptions;
document.write(srcStart + '/tests/assets/jquery-1.6.2.js' + scrEnd);
document.write(srcStart + '/tests/assets/jquery.plugin.html2canvas.js' + scrEnd);
var html2canvas = ['log', 'nodecontainer', 'stackingcontext', 'textcontainer', 'support', 'imagecontainer', 'gradientcontainer', 'lineargradientcontainer', 'webkitgradientcontainer',
var html2canvas = ['log', 'nodecontainer', 'stackingcontext', 'textcontainer', 'support', 'imagecontainer', 'proxyimagecontainer', 'gradientcontainer', 'lineargradientcontainer', 'webkitgradientcontainer',
'imageloader', 'nodeparser', 'font', 'fontmetrics', 'core', 'renderer', 'promise', 'renderers/canvas'], i;
for (i = 0; i < html2canvas.length; ++i) {
document.write(srcStart + '/src/' + html2canvas[i] + '.js?' + Math.random() + scrEnd);
@ -30,7 +30,7 @@ var h2cSelector, h2cOptions;
logging: true,
profile: true,
proxy: "http://html2canvas.appspot.com/query",
useCORS: true
useCORS: false
}, h2cOptions));
}, 100);
};