After several hours of research, I finally found a solution to take a screenshot of the element, even if the origin-clean FLAG checkbox is selected (to prevent XSS), so you can even capture, for example, Google Maps (in my case). I wrote a universal function to get a screenshot. The only thing you need is the html2canvas library ( https://html2canvas.hertzen.com/ ).
Example:
getScreenshotOfElement($("div#toBeCaptured").get(0), 0, 0, 100, 100, function(data) {
Keep in mind that console.log() and alert() will not generate output if the image size is large.
Function:
function getScreenshotOfElement(element, posX, posY, width, height, callback) { html2canvas(element, { onrendered: function (canvas) { var context = canvas.getContext('2d'); var imageData = context.getImageData(posX, posY, width, height).data; var outputCanvas = document.createElement('canvas'); var outputContext = outputCanvas.getContext('2d'); outputCanvas.width = width; outputCanvas.height = height; var idata = outputContext.createImageData(width, height); idata.data.set(imageData); outputContext.putImageData(idata, 0, 0); callback(outputCanvas.toDataURL().replace("data:image/png;base64,", "")); }, width: width, height: height, useCORS: true, taintTest: false, allowTaint: false }); }
source share