Fix iframe proxy

This commit is contained in:
Niklas von Hertzen
2015-01-18 21:38:05 +02:00
parent 37b39ec716
commit 77c73c478f
8 changed files with 153 additions and 25 deletions

26
dist/html2canvas.js vendored
View File

@ -587,8 +587,10 @@ window.html2canvas = function(nodeList, options) {
if (typeof(options.proxy) !== "string") {
return Promise.reject("Proxy must be used when rendering url");
}
return loadUrlDocument(absoluteUrl(nodeList), options.proxy, document, window.innerWidth, window.innerHeight, options).then(function(container) {
return renderWindow(container.contentWindow.document.documentElement, container, options, window.innerWidth, window.innerHeight);
var width = options.width != null ? options.width : window.innerWidth;
var height = options.height != null ? options.height : window.innerHeight;
return loadUrlDocument(absoluteUrl(nodeList), options.proxy, document, width, height, options).then(function(container) {
return renderWindow(container.contentWindow.document.documentElement, container, options, width, height);
});
}
@ -607,7 +609,7 @@ window.html2canvas.punycode = this.punycode;
window.html2canvas.proxy = {};
function renderDocument(document, options, windowWidth, windowHeight, html2canvasIndex) {
return createWindowClone(document, document, windowWidth, windowHeight, options).then(function(container) {
return createWindowClone(document, document, windowWidth, windowHeight, options, document.defaultView.pageXOffset, document.defaultView.pageYOffset).then(function(container) {
log("Document cloned");
var attributeName = html2canvasNodeAttribute + html2canvasIndex;
var selector = "[" + attributeName + "='" + html2canvasIndex + "']";
@ -688,7 +690,7 @@ function smallImage() {
return "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7";
}
function createWindowClone(ownerDocument, containerDocument, width, height, options) {
function createWindowClone(ownerDocument, containerDocument, width, height, options, x ,y) {
labelCanvasElements(ownerDocument);
var documentElement = ownerDocument.documentElement.cloneNode(true),
container = containerDocument.createElement("iframe");
@ -726,9 +728,6 @@ function createWindowClone(ownerDocument, containerDocument, width, height, opti
}, 50);
};
var x = ownerDocument.defaultView.pageXOffset;
var y = ownerDocument.defaultView.pageYOffset;
documentClone.open();
documentClone.write("<!DOCTYPE html><html></html>");
// Chrome scrolls the parent document for some reason after the write to the cloned window???
@ -748,14 +747,14 @@ function cloneNodeValues(document, clone, nodeName) {
}
function restoreOwnerScroll(ownerDocument, x, y) {
if (x !== ownerDocument.defaultView.pageXOffset || y !== ownerDocument.defaultView.pageYOffset) {
if (ownerDocument.defaultView && (x !== ownerDocument.defaultView.pageXOffset || y !== ownerDocument.defaultView.pageYOffset)) {
ownerDocument.defaultView.scrollTo(x, y);
}
}
function loadUrlDocument(src, proxy, document, width, height, options) {
return new Proxy(src, proxy, window.document).then(documentFromHTML(src)).then(function(doc) {
return createWindowClone(doc, document, width, height, options);
return createWindowClone(doc, document, width, height, options, 0, 0);
});
}
@ -1371,7 +1370,7 @@ ImageLoader.prototype.fetch = function(nodes) {
ImageLoader.prototype.timeout = function(container, timeout) {
var timer;
return Promise.race([container.promise, new Promise(function(res, reject) {
var promise = Promise.race([container.promise, new Promise(function(res, reject) {
timer = setTimeout(function() {
log("Timed out loading image", container);
reject(container);
@ -1380,6 +1379,10 @@ ImageLoader.prototype.timeout = function(container, timeout) {
clearTimeout(timer);
return container;
});
promise['catch'](function() {
clearTimeout(timer);
});
return promise;
};
function LinearGradientContainer(imageData) {
@ -2732,6 +2735,9 @@ function hasUnicode(string) {
}
function Proxy(src, proxyUrl, document) {
if (!proxyUrl) {
return Promise.reject("No proxy configured");
}
var callback = createCallback(supportsCORS);
var url = createProxyUrl(proxyUrl, src, callback);

File diff suppressed because one or more lines are too long