Merge pull request #62 from cobexer/for-niklasvh

fix image loaded through the proxy hanging the preload process
This commit is contained in:
Niklas von Hertzen 2012-02-26 12:52:46 -08:00
commit 84a676403f

View File

@ -61,17 +61,16 @@ html2canvas.Preload = function(element, opts){
} }
} }
function proxyGetImage(url, img){ function proxyGetImage(url, img, imageObj){
var callback_name, var callback_name,
scriptUrl = options.proxy, scriptUrl = options.proxy,
script, script;
imgObj = images[url];
link.href = url; link.href = url;
url = link.href; // work around for pages with base href="" set - WARNING: this may change the url -> so access imgObj from images map before changing that url! url = link.href; // work around for pages with base href="" set - WARNING: this may change the url
callback_name = 'html2canvas_' + count; callback_name = 'html2canvas_' + (count++);
imgObj.callbackname = callback_name; imageObj.callbackname = callback_name;
if (scriptUrl.indexOf("?") > -1) { if (scriptUrl.indexOf("?") > -1) {
scriptUrl += "&"; scriptUrl += "&";
@ -79,19 +78,16 @@ html2canvas.Preload = function(element, opts){
scriptUrl += "?"; scriptUrl += "?";
} }
scriptUrl += 'url=' + encodeURIComponent(url) + '&callback=' + callback_name; scriptUrl += 'url=' + encodeURIComponent(url) + '&callback=' + callback_name;
script = doc.createElement("script");
window[callback_name] = function(a){ window[callback_name] = function(a){
if (a.substring(0,6) === "error:"){ if (a.substring(0,6) === "error:"){
imgObj.succeeded = false; imageObj.succeeded = false;
images.numLoaded++; images.numLoaded++;
images.numFailed++; images.numFailed++;
start(); start();
} else { } else {
img.onload = function(){ setImageLoadHandlers(img, imageObj);
imgObj.succeeded = true;
images.numLoaded++;
start();
};
img.src = a; img.src = a;
} }
window[callback_name] = undefined; // to work with IE<9 // NOTE: that the undefined callback property-name still exists on the window object (for IE<9) window[callback_name] = undefined; // to work with IE<9 // NOTE: that the undefined callback property-name still exists on the window object (for IE<9)
@ -100,15 +96,13 @@ html2canvas.Preload = function(element, opts){
} catch(ex) {} } catch(ex) {}
script.parentNode.removeChild(script); script.parentNode.removeChild(script);
script = null; script = null;
imgObj.callbackname = undefined; delete imageObj.script;
delete imageObj.callbackname;
}; };
count += 1;
script = doc.createElement("script");
script.setAttribute("src", scriptUrl);
script.setAttribute("type", "text/javascript"); script.setAttribute("type", "text/javascript");
imgObj.script = script; script.setAttribute("src", scriptUrl);
imageObj.script = script;
window.document.body.appendChild(script); window.document.body.appendChild(script);
/* /*
@ -219,54 +213,41 @@ html2canvas.Preload = function(element, opts){
} }
} }
methods = { function setImageLoadHandlers(img, imageObj) {
loadImage: function( src ) {
var img;
if ( src && images[src] === undefined ) {
if ( src.match(/data:image\/.*;base64,/i) ) {
//Base64 src
img = new Image();
img.src = src.replace(/url\(['"]{0,}|['"]{0,}\)$/ig, '');
images[src] = {
img: img,
succeeded: true
};
images.numTotal++;
images.numLoaded++;
start();
}else if ( isSameOrigin( src ) ) {
img = new Image();
images[src] = {
img: img
};
images.numTotal++;
img.onload = function() { img.onload = function() {
images.numLoaded++; images.numLoaded++;
images[src].succeeded = true; imageObj.succeeded = true;
start(); start();
}; };
img.onerror = function() { img.onerror = function() {
images.numLoaded++; images.numLoaded++;
images.numFailed++; images.numFailed++;
images[src].succeeded = false; imageObj.succeeded = false;
start(); start();
}; };
}
img.src = src; methods = {
loadImage: function( src ) {
}else if ( options.proxy ){ var img, imageObj;
// console.log('b'+src); if ( src && images[src] === undefined ) {
img = new Image(); img = new Image();
images[src] = { if ( src.match(/data:image\/.*;base64,/i) ) {
img: img img.src = src.replace(/url\(['"]{0,}|['"]{0,}\)$/ig, '');
}; imageObj = images[src] = { img: img };
images.numTotal++; images.numTotal++;
proxyGetImage( src, img ); setImageLoadHandlers(img, imageObj);
}
else if ( isSameOrigin( src ) ) {
imageObj = images[src] = { img: img };
images.numTotal++;
setImageLoadHandlers(img, imageObj);
img.src = src;
}
else if ( options.proxy ) {
imageObj = images[src] = { img: img };
images.numTotal++;
proxyGetImage( src, img, imageObj );
} }
} }
@ -326,7 +307,6 @@ html2canvas.Preload = function(element, opts){
if (options.timeout > 0) { if (options.timeout > 0) {
timeoutTimer = window.setTimeout(methods.cleanupDOM, options.timeout); timeoutTimer = window.setTimeout(methods.cleanupDOM, options.timeout);
} }
var startTime = (new Date()).getTime();
html2canvas.log('html2canvas: Preload starts: finding background-images'); html2canvas.log('html2canvas: Preload starts: finding background-images');
images.firstRun = true; images.firstRun = true;