mirror of
https://github.com/niklasvh/html2canvas.git
synced 2023-08-10 21:13:10 +03:00
Fallback to jsonp proxy if browser doesn't support cors xhr/image
This commit is contained in:
@@ -1,38 +1,18 @@
|
||||
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 self = this;
|
||||
this.promise = new Promise(function(resolve, reject) {
|
||||
self.image.crossOrigin = "Anonymous";
|
||||
self.image.onload = resolve;
|
||||
self.image.onerror = reject;
|
||||
|
||||
window[callbackName] = function(a){
|
||||
if (a.substring(0,6) === "error:"){
|
||||
reject();
|
||||
} else {
|
||||
self.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);
|
||||
})['catch'](function() {
|
||||
var dummy = new DummyImageContainer(src);
|
||||
return dummy.promise.then(function(image) {
|
||||
self.image = image;
|
||||
});
|
||||
new ProxyURL(src, proxy, document).then(function(url) {
|
||||
self.image.src = url;
|
||||
})['catch'](reject);
|
||||
});
|
||||
}
|
||||
|
||||
var proxyImageCount = 0;
|
||||
|
||||
Reference in New Issue
Block a user