niklasvh / html2canvas

Screenshots with JavaScript
https://html2canvas.hertzen.com/
MIT License
30.43k stars 4.79k forks source link

Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported. #1466

Open deanoemcke opened 6 years ago

deanoemcke commented 6 years ago

Bug reports:

I am getting a tainted canvas error, even with the allowTaint flag set to false. Similar to #1409. The url I'm testing is: http://en.miui.com/forum.php I'm not sure how to set up a jsFiddle with this sorry, but I have tested it via a chrome extension using the executeScript functionality.

From extension:

var captureScreenCode = 
  `html2canvas(document.body, {
    allowTaint: false,
    useCORS: false
    }).then(function(canvas) {
    console.log(canvas.toDataURL());
  });`;
chrome.tabs.executeScript(1234, { file: 'html2canvas.js' }, () => {
  chrome.tabs.executeScript(1234, { code: captureScreenCode });
});

Console output of tab:

0ms html2canvas: html2canvas 1.0.0-alpha.10
html2canvas.js:2673 978ms html2canvas: Document cloned, using computed rendering
html2canvas.js:2673 981ms html2canvas: Starting node parsing
html2canvas.js:2673 996ms html2canvas: Added image http://en.miui.com/static/image/miui/base/logo.png
html2canvas.js:2673 1056ms html2canvas: Added image http://en.miui.com/static/image/miui/base/pin_3.gif
html2canvas.js:2673 1063ms html2canvas: Added image http://en.miui.com/static/image/miui/base/rushreply_z.png
html2canvas.js:2673 1066ms html2canvas: Added image http://en.miui.com/static/image/miui/base/hot_3.gif
html2canvas.js:2673 1070ms html2canvas: Added image http://en.miui.com/static/image/miui/base/agree.gif
html2canvas.js:2673 1091ms html2canvas: Added image http://en.miui.com/static/image/miui/base/hot_1.gif
html2canvas.js:2673 1168ms html2canvas: Added image http://en.miui.com/static/image/miui/base/hot_2.gif
html2canvas.js:2673 1290ms html2canvas: Added image http://en.miui.com/static/image/miui/base/pin_2.gif
html2canvas.js:2673 1560ms html2canvas: Added image http://en.miui.com/static/image/miui/base/pin_1.gif
html2canvas.js:2673 1596ms html2canvas: Added image http://en.miui.com/static/image/miui/base/imghack.png
html2canvas.js:2673 1625ms html2canvas: Added image http://en.miui.com/static/image/miui/base/signin_2.png
html2canvas.js:2673 1665ms html2canvas: Added image http://en.miui.com/static/image/miui/base/icon_mi_facebook.jpg
html2canvas.js:2673 1668ms html2canvas: Added image http://en.miui.com/static/image/miui/base/icon_mi_twitter.jpg
html2canvas.js:2673 1676ms html2canvas: Added image http://en.miui.com/static/image/miui/base/icon_mi_gplus.jpg
html2canvas.js:2673 1682ms html2canvas: Added image http://en.miui.com/static/image/miui/base/icon_mi_weibo.jpg
html2canvas.js:2673 1686ms html2canvas: Added image http://en.miui.com/static/image/miui/base/icon_youtube.png
html2canvas.js:2673 1689ms html2canvas: Added image http://en.miui.com/static/image/miui/base/miuiweb.jpg?4
html2canvas.js:2673 1705ms html2canvas: Added image http://en.miui.com/static/image/miui/base/fansStationRetract.png?t=0
html2canvas.js:2673 1711ms html2canvas: Added image http://en.miui.com/static/image/miui/base/fansStationRetract.png??t=0
html2canvas.js:2673 1730ms html2canvas: Added image http://en.miui.com/static/image/miui/base/footer_title.png
html2canvas.js:2673 1778ms html2canvas: Finished parsing node tree
html2canvas.js:2673 1951ms html2canvas: Finished loading 20 images (20) [img, img, img, img, img, img, img, img, img, img, img, img, img, img, img, img, img, img, img, img]
html2canvas.js:2673 1957ms html2canvas: Starting renderer
html2canvas.js:2673 1959ms html2canvas: Canvas renderer initialized (1388x3165 at 0,0) with scale 2
html2canvas.js:2673 2064ms html2canvas: Render completed
VM289:5 Uncaught (in promise) DOMException: Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported.
    at <anonymous>:5:24
    at <anonymous>

Specifications:

Thanks for all your work on this amazing library.

amitguptagwl commented 6 years ago

I'm able to get toDataURL() with html2canvas 0.4.1 but not latest one.

CooLNuanfeng commented 6 years ago

+1

amitguptagwl commented 6 years ago

I've switched to "dom to image" library which solved my issue. Here is the result match-it

jamesmarva commented 6 years ago

hi man, do you solve it ?

amitguptagwl commented 6 years ago

@jamesmarva as you've not addressed particular one in your problem, yes as I said I've switched to other library which solved my problem.

jamesmarva commented 6 years ago

@amitguptagwl thanks, do you know use which library to save website-screenshot to he image?

amitguptagwl commented 6 years ago

@jamesmarva This is what I used to save image of particular div. I hope it should work for full page as well

https://github.com/tsayen/dom-to-image

jamesmarva commented 6 years ago

@amitguptagwl thank u very much.

zhaosaisai commented 5 years ago

Let us hack it

const TempImage = window.Image

const Image = function() {
      const img = new TempImage()
      img.crossOrigin = 'anonymous'
      return img
}

It works

SalahAdDin commented 2 years ago

Let us hack it

const TempImage = window.Image

const Image = function() {
      const img = new TempImage()
      img.crossOrigin = 'anonymous'
      return img
}

It works

crossOrigin is anonymous by default.