niklasvh / html2canvas

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

Does not update element data #3166

Open nodvick opened 7 months ago

nodvick commented 7 months ago

Please follow the general troubleshooting steps first:

Bug reports:

If you change an element's value in the desired section to screenshot, then press a button to call the html2canvas function, the screenshot will be the old value.

neither

var ae = document.activeElement; ae.setAttribute("value", ae.value + ae.getAttribute("data-text"));

or

document.activeElement.blur();

before calling so that the focus is changed in an attempt to sync the value attribute to the value property does not prevent this issue.

Specifications:

nodvick commented 7 months ago

More details and fix:

I was using the canvas.todataurl string as data to submit, then processing/converting/saving without ever actually putting the canvas on the page anywhere. appendchild(canvas) screenshotdata = canvas.todaturl(), then removechild(canvas) fixes it, with in my edge case because there were so many many elements its cloning, it is also a timing issue, async then await the function to generate screenshot, await the canvas creation, then submit the data was also required

(

async function getSS() {

            if(o('TEST')!=null) document.body.removeChild(o('TEST'));

            await html2canvas(document.body).then(
            function (c) {
                c.id="TEST";
                document.body.appendChild(c);

            });

            o('screenshotData').value = o('TEST').toDataURL();

            document.body.removeChild(o('TEST'));

            return true;
        }
////---later
        async function SubmitNotes(e) {
            e.preventDefault();
            await getSS();

EDIT: o() is sort of my fingerprint, you see it in almost every single javascript thing I do, sorry, for reference: function o(obj) { return document.getElementById(obj); }