Take a screenshot of a webpage with JavaScript?

JavascriptHtaWebpage Screenshot

Javascript Problem Overview


Is it possible to to take a screenshot of a webpage with JavaScript and then submit that back to the server?

I'm not so concerned with browser security issues. etc. as the implementation would be for HTA. But is it possible?

Javascript Solutions


Solution 1 - Javascript

Google is doing this in Google+ and a talented developer reverse engineered it and produced http://html2canvas.hertzen.com/ . To work in IE you'll need a canvas support library such as http://excanvas.sourceforge.net/

Solution 2 - Javascript

I have done this for an HTA by using an ActiveX control. It was pretty easy to build the control in VB6 to take the screenshot. I had to use the keybd_event API call because SendKeys can't do PrintScreen. Here's the code for that:

Declare Sub keybd_event Lib "user32" _
(ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Public Const CaptWindow = 2

Public Sub ScreenGrab()
   keybd_event &H12, 0, 0, 0
   keybd_event &H2C, CaptWindow, 0, 0
   keybd_event &H2C, CaptWindow, &H2, 0
   keybd_event &H12, 0, &H2, 0
End Sub

That only gets you as far as getting the window to the clipboard.

Another option, if the window you want a screenshot of is an HTA would be to just use an XMLHTTPRequest to send the DOM nodes to the server, then create the screenshots server-side.

Solution 3 - Javascript

Another possible solution that I've discovered is http://www.phantomjs.org/ which allows one to very easily take screenshots of pages and a whole lot more. Whilst my original requirements for this question aren't valid any more (different job), I will likely integrate PhantomJS into future projects.

Solution 4 - Javascript

Pounder's if this is possible to do by setting the whole body elements into a canvase then using canvas2image ?

http://www.nihilogic.dk/labs/canvas2image/

Solution 5 - Javascript

A possible way to do this, if running on windows and have .NET installed you can do:

public Bitmap GenerateScreenshot(string url)
{
    // This method gets a screenshot of the webpage
    // rendered at its full size (height and width)
    return GenerateScreenshot(url, -1, -1);
}

public Bitmap GenerateScreenshot(string url, int width, int height)
{
    // Load the webpage into a WebBrowser control
    WebBrowser wb = new WebBrowser();
    wb.ScrollBarsEnabled = false;
    wb.ScriptErrorsSuppressed = true;
    wb.Navigate(url);
    while (wb.ReadyState != WebBrowserReadyState.Complete) { Application.DoEvents(); }


    // Set the size of the WebBrowser control
    wb.Width = width;
    wb.Height = height;

    if (width == -1)
    {
        // Take Screenshot of the web pages full width
        wb.Width = wb.Document.Body.ScrollRectangle.Width;
    }

    if (height == -1)
    {
        // Take Screenshot of the web pages full height
        wb.Height = wb.Document.Body.ScrollRectangle.Height;
    }

    // Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control
    Bitmap bitmap = new Bitmap(wb.Width, wb.Height);
    wb.DrawToBitmap(bitmap, new Rectangle(0, 0, wb.Width, wb.Height));
    wb.Dispose();

    return bitmap;
}

And then via PHP you can do:

exec("CreateScreenShot.exe -url http://.... -save C:/shots domain_page.png");

Then you have the screenshot in the server side.

Solution 6 - Javascript

This might not be the ideal solution for you, but it might still be worth mentioning.

Snapsie is an open source, ActiveX object that enables Internet Explorer screenshots to be captured and saved. Once the DLL file is registered on the client, you should be able to capture the screenshot and upload the file to the server withing JavaScript. Drawbacks: it needs to register the DLL file at the client and works only with Internet Explorer.

Solution 7 - Javascript

We had a similar requirement for reporting bugs. Since it was for an intranet scenario, we were able to use browser addons (like Fireshot for Firefox and IE Screenshot for Internet Explorer).

Solution 8 - Javascript

The SnapEngage uses a Java applet (1.5+) to make a browser screenshot. AFAIK, java.awt.Robot should do the job - the user has just to permit the applet to do it (once).

And I have just found a post about it:

Solution 9 - Javascript

This question is old but maybe there's still someone interested in a state-of-the-art answer:

You can use getDisplayMedia:

https://github.com/ondras/browsershot

Solution 10 - Javascript

You can achieve that using HTA and VBScript. Just call an external tool to do the screenshotting. I forgot what the name is, but on Windows Vista there is a tool to do screenshots. You don't even need an extra install for it.

As for as automatic - it totally depends on the tool you use. If it has an API, I am sure you can trigger the screenshot and saving process through a couple of Visual Basic calls without the user knowing that you did what you did.

Since you mentioned HTA, I am assuming you are on Windows and (probably) know your environment (e.g. OS and version) very well.

Solution 11 - Javascript

I found that dom-to-image did a good job (much better than html2canvas). See the following question & answer: https://stackoverflow.com/a/32776834/207981

This question asks about submitting this back to the server, which should be possible, but if you're looking to download the image(s) you'll want to combine it with FileSaver.js, and if you want to download a zip with multiple image files all generated client-side take a look at jszip.

Solution 12 - Javascript

If you are willing to do it on the server side, there are options like PhantomJS, which is now deprecated. The best way to go would be Headless Chrome with something like Puppeteer on Node.JS. Capturing a web page using Puppeteer would be as simple as follows:

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://example.com');
  await page.screenshot({path: 'example.png'});

  await browser.close();
})();

However it requires headless chrome to be able to run on your servers, which has some dependencies and might not be suitable on restricted environments. (Also, if you are not using Node.JS, you might need to handle installation / launching of browsers yourself.)

If you are willing to use a SaaS service, there are many options such as

Solution 13 - Javascript

A great solution for screenshot taking in Javascript is the one by https://grabz.it.

They have a flexible and simple-to-use screenshot API which can be used by any type of JS application.

If you want to try it, at first you should get the authorization app key + secret and the free SDK

Then, in your app, the implementation steps would be:

// include the grabzit.min.js library in the web page you want the capture to appear
<script src="grabzit.min.js"></script>

//use the key and the secret to login, capture the url
<script>
GrabzIt("KEY", "SECRET").ConvertURL("http://www.google.com").Create();
</script>

Screenshot could be customized with different parameters. For example:

GrabzIt("KEY", "SECRET").ConvertURL("http://www.google.com", 
{"width": 400, "height": 400, "format": "png", "delay", 10000}).Create();
</script>

That's all. Then simply wait a short while and the image will automatically appear at the bottom of the page, without you needing to reload the page.

There are other functionalities to the screenshot mechanism which you can explore here.

It's also possible to save the screenshot locally. For that you will need to utilize GrabzIt server side API. For more info check the detailed guide here.

Solution 14 - Javascript

As of today Apr 2020 GitHub library html2Canvas https://github.com/niklasvh/html2canvas

GitHub 20K stars | Azure pipeles : Succeeded | Downloads 1.3M/mo |

quote : " JavaScript HTML renderer The script allows you to take "screenshots" of webpages or parts of it, directly on the users browser. The screenshot is based on the DOM and as such may not be 100% accurate to the real representation as it does not make an actual screenshot, but builds the screenshot based on the information available on the page.

Solution 15 - Javascript

I made a simple function that uses rasterizeHTML to build a svg and/or an image with page contents.

Check it out :

https://github.com/orisha/tdg-screen-shooter-pure-js

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionScott Bennett-McLeishView Question on Stackoverflow
Solution 1 - JavascriptJAAuldeView Answer on Stackoverflow
Solution 2 - JavascriptJoel AnairView Answer on Stackoverflow
Solution 3 - JavascriptScott Bennett-McLeishView Answer on Stackoverflow
Solution 4 - JavascriptRobertPittView Answer on Stackoverflow
Solution 5 - JavascriptRobertPittView Answer on Stackoverflow
Solution 6 - JavascriptnikhilView Answer on Stackoverflow
Solution 7 - JavascriptGulzar NazimView Answer on Stackoverflow
Solution 8 - JavascriptxmedekoView Answer on Stackoverflow
Solution 9 - JavascriptshaedrichView Answer on Stackoverflow
Solution 10 - JavascriptTillView Answer on Stackoverflow
Solution 11 - JavascriptbszomView Answer on Stackoverflow
Solution 12 - JavascriptEkin KocView Answer on Stackoverflow
Solution 13 - JavascriptJohnnyView Answer on Stackoverflow
Solution 14 - JavascriptzennniView Answer on Stackoverflow
Solution 15 - JavascriptDiego FaveroView Answer on Stackoverflow