Skip to content

Commit afc358f

Browse files
committed
fix image loaded through the proxy hanging the preload process
images loaded through the proxy could hang the preload process if they finish loading through the proxy but then fail to decode and thus don't call the onload handler of the image object.
1 parent e925719 commit afc358f

1 file changed

Lines changed: 39 additions & 59 deletions

File tree

src/Preload.js

Lines changed: 39 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -61,37 +61,33 @@ html2canvas.Preload = function(element, opts){
6161
}
6262
}
6363

64-
function proxyGetImage(url, img){
64+
function proxyGetImage(url, img, imageObj){
6565
var callback_name,
6666
scriptUrl = options.proxy,
67-
script,
68-
imgObj = images[url];
67+
script;
6968

7069
link.href = url;
71-
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!
70+
url = link.href; // work around for pages with base href="" set - WARNING: this may change the url
7271

73-
callback_name = 'html2canvas_' + count;
74-
imgObj.callbackname = callback_name;
72+
callback_name = 'html2canvas_' + (count++);
73+
imageObj.callbackname = callback_name;
7574

7675
if (scriptUrl.indexOf("?") > -1) {
7776
scriptUrl += "&";
7877
} else {
7978
scriptUrl += "?";
8079
}
8180
scriptUrl += 'url=' + encodeURIComponent(url) + '&callback=' + callback_name;
82-
81+
script = doc.createElement("script");
82+
8383
window[callback_name] = function(a){
8484
if (a.substring(0,6) === "error:"){
85-
imgObj.succeeded = false;
85+
imageObj.succeeded = false;
8686
images.numLoaded++;
8787
images.numFailed++;
8888
start();
8989
} else {
90-
img.onload = function(){
91-
imgObj.succeeded = true;
92-
images.numLoaded++;
93-
start();
94-
};
90+
setImageLoadHandlers(img, imageObj);
9591
img.src = a;
9692
}
9793
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){
10096
} catch(ex) {}
10197
script.parentNode.removeChild(script);
10298
script = null;
103-
imgObj.callbackname = undefined;
99+
delete imageObj.script;
100+
delete imageObj.callbackname;
104101
};
105102

106-
count += 1;
107-
108-
script = doc.createElement("script");
109-
script.setAttribute("src", scriptUrl);
110103
script.setAttribute("type", "text/javascript");
111-
imgObj.script = script;
104+
script.setAttribute("src", scriptUrl);
105+
imageObj.script = script;
112106
window.document.body.appendChild(script);
113107

114108
/*
@@ -219,54 +213,41 @@ html2canvas.Preload = function(element, opts){
219213
}
220214
}
221215

216+
function setImageLoadHandlers(img, imageObj) {
217+
img.onload = function() {
218+
images.numLoaded++;
219+
imageObj.succeeded = true;
220+
start();
221+
};
222+
img.onerror = function() {
223+
images.numLoaded++;
224+
images.numFailed++;
225+
imageObj.succeeded = false;
226+
start();
227+
};
228+
}
229+
222230
methods = {
223231
loadImage: function( src ) {
224-
var img;
232+
var img, imageObj;
225233
if ( src && images[src] === undefined ) {
234+
img = new Image();
226235
if ( src.match(/data:image\/.*;base64,/i) ) {
227-
228-
//Base64 src
229-
img = new Image();
230236
img.src = src.replace(/url\(['"]{0,}|['"]{0,}\)$/ig, '');
231-
images[src] = {
232-
img: img,
233-
succeeded: true
234-
};
237+
imageObj = images[src] = { img: img };
235238
images.numTotal++;
236-
images.numLoaded++;
237-
start();
238-
239-
}else if ( isSameOrigin( src ) ) {
240-
241-
img = new Image();
242-
images[src] = {
243-
img: img
244-
};
239+
setImageLoadHandlers(img, imageObj);
240+
}
241+
else if ( isSameOrigin( src ) ) {
242+
imageObj = images[src] = { img: img };
245243
images.numTotal++;
246-
247-
img.onload = function() {
248-
images.numLoaded++;
249-
images[src].succeeded = true;
250-
start();
251-
};
252-
253-
img.onerror = function() {
254-
images.numLoaded++;
255-
images.numFailed++;
256-
images[src].succeeded = false;
257-
start();
258-
};
259-
244+
setImageLoadHandlers(img, imageObj);
260245
img.src = src;
261-
262-
}else if ( options.proxy ){
263-
// console.log('b'+src);
264-
img = new Image();
265-
images[src] = {
266-
img: img
267-
};
246+
}
247+
else if ( options.proxy ) {
248+
imageObj = images[src] = { img: img };
268249
images.numTotal++;
269-
proxyGetImage( src, img );
250+
proxyGetImage( src, img, imageObj );
270251
}
271252
}
272253

@@ -326,7 +307,6 @@ html2canvas.Preload = function(element, opts){
326307
if (options.timeout > 0) {
327308
timeoutTimer = window.setTimeout(methods.cleanupDOM, options.timeout);
328309
}
329-
var startTime = (new Date()).getTime();
330310
html2canvas.log('html2canvas: Preload starts: finding background-images');
331311
images.firstRun = true;
332312

0 commit comments

Comments
 (0)