0

On http://snippet.christopherkade.com I let my user download code snippets, I use html2canvas to render and save the console displayed on the screen. This works just fine on Firefox, but Chrome displays this error:

Not allowed to navigate top frame to data URL

When I do the following:

// Opens a window with the console to be copied or downloaded
html2canvas(document.getElementsByClassName('console'), {      
  onrendered: function(canvas) {
    var img = canvas.toDataURL()
    window.open(img);
  }
});

Note that Chrome displays a blank page with the error in my console.

The only information I have found is here. But it only explains why Google has made this decision and I was not able to see a solution.

Christopher
  • 1,712
  • 2
  • 21
  • 50

1 Answers1

1

You can use a blob: URL instead of a data: URL.

b.onclick=async()=>{
  let blob = await new Promise(resolve=>c.toBlob(resolve));
  let url = URL.createObjectURL(blob);
  // Won't work here, because
  // "the request was made in a sandboxed frame whose 'allow-popups' permission is not set."
  window.open(url);
  let a = document.createElement('a');
  a.href = url;
  a.download = '';
  a.click();
};
<canvas id=c></canvas>
<button id=b>save</button>
Josh Lee
  • 171,072
  • 38
  • 269
  • 275