Take Screenshot of Browser via JavaScript (or something else)

前端 未结 15 1986
天命终不由人
天命终不由人 2020-12-15 04:43

For support reasons I want to be able for a user to take a screenshot of the current browser window as easy as possible and send it over to the server.

Any (crazy)

15条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-15 05:10

    I understand this post is 5 years old, but for the sake of future visits I'll add my own solution here which I think solves the original post's question without any third-party libraries apart from jQuery.

    pageClone = $('html').clone();
    
    // Make sure that CSS and images load correctly when opening this clone
    pageClone.find('head').append("");
    
    // OPTIONAL: Remove potentially interfering scripts so the page is totally static
    pageClone.find('script').remove();
    
    htmlString = pageClone.html();
    

    You could remove other parts of the DOM you think are unnecessary, such as the support form if it is in a modal window. Or you could choose not to remove scripts if you prefer to maintain some interaction with dynamic controls.

    Send that string to the server, either in a hidden field or by AJAX, and then on the server side just attach the whole lot as an HTML file to the support email.

    The benefits of this are that you'll get not just a screenshot but the entire scrollable page in its current form, plus you can even inspect and debug the DOM.

提交回复
热议问题