Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Take Screenshot of Browser via JavaScript (or something else)

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) ideas?

like image 319
Thorben Croisé Avatar asked Jul 23 '10 07:07

Thorben Croisé


People also ask

How do I take a screenshot of an entire browser?

Also, you can press Ctrl+Shift+P on Windows or Command+Shift+P on Mac. Type screenshot into the search box. Select Capture full-size screenshot. Once Chrome takes the screenshot, it should save it into your Downloads folder.

How do I take a screenshot of a webpage with inspect element?

Inspect the element you wish to capture. Open the Command Menu with Cmd + Shift + P / Ctrl + Shift + P. Type in screenshot within the Command Menu. You can now capture the screenshot of only the specific element, a viewport screenshot, or a full-page screenshot.


11 Answers

That would appear to be a pretty big security hole in JavaScript if you could do this. Imagine a malicious user installing that code on your site with a XSS attack and then screenshotting all of your daily work. Imagine that happening with your online banking...

However, it is possible to do this sort of thing outside of JavaScript. I developed a Swing application that used screen capture code like this which did a great job of sending an email to the helpdesk with an attached screenshot whenever the user encountered a RuntimeException.

I suppose you could experiment with a signed Java applet (shock! horror! noooooo!) that hung around in the corner. If executed with the appropriate security privileges given at installation it might be coerced into executing that kind of screenshot code.

For convenience, here is the code from the site I linked to:

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;

...

public void captureScreen(String fileName) throws Exception {

   Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
   Rectangle screenRectangle = new Rectangle(screenSize);
   Robot robot = new Robot();
   BufferedImage image = robot.createScreenCapture(screenRectangle);
   ImageIO.write(image, "png", new File(fileName));
}
...
like image 167
Gary Rowe Avatar answered Oct 17 '22 06:10

Gary Rowe


Please see the answer shared here for a relatively successful implementation of this: https://stackoverflow.com/a/6678156/291640

Utilizing: https://github.com/niklasvh/html2canvas

like image 27
whoughton Avatar answered Oct 17 '22 07:10

whoughton


You could try to render the whole page in canvas and save this image back to server. have fun :)

like image 41
Andreas Köberle Avatar answered Oct 17 '22 07:10

Andreas Köberle


A webpage can't do this (or at least, I would be very surprised if it could, in any browser) but a Firefox extension can. See https://developer.mozilla.org/en/Drawing_Graphics_with_Canvas#Rendering_Web_Content_Into_A_Canvas -- when that page says "Chrome privileges" that means an extension can do it, but a web page can't.

like image 36
Tyler Avatar answered Oct 17 '22 06:10

Tyler


Seems to me that support needs (at least) the answers for two questions:

  1. What does the screen look like? and
  2. Why does it look that way?

A screenshot -- a visual -- is very necessary and answers the first question, but it can't answer the second.

As a first attempt, I'd try to send the entire page up to support. The support tech could display that page in his browser (answers the first question); and could also see the current state of the customer's html (helps to answer the second question).

I'd try to send as much of the page as is available to the client JS by way of AJAX or as the payload of a form. I'd also send info not on the page: anything that affects the state of the page, like cookies or session IDs or whatever.

The cust might have a submit-like button to start the process.

I think that would work. Let's see: it needs some CGI somewhere on the server that catches the incoming user page and makes it available to support, maybe by writing a disk file. Then the support person can load (or have loaded automatically) that same page. All the other info (cookies and so on) can be put into the page that support sees.

PLUS: the client JS that handles the submit-button onclick( ) could also include any useful JS variable values!

Hey, this can work! I'm getting psyched :-)

HTH

-- pete

like image 23
Pete Wilson Avatar answered Oct 17 '22 05:10

Pete Wilson


I've seen people either do this with two approaches:

  1. setup a separate server for screenshotting and run a bunch of firefox instances on there, check out these two gem if you're doing it in ruby: selenium-webdriver and headless

  2. use a hosted solution like http://url2png.com (way easier)

like image 42
Brian Armstrong Avatar answered Oct 17 '22 07:10

Brian Armstrong


You can also do this with the Fireshot plugin. I use the following code (that I extracted from the API code so I don't need to include the API JS) to make a direct call to the Fireshot object:

    var element = document.createElement("FireShotDataElement");
element.setAttribute("Entire", true);
element.setAttribute("Action", 1);
element.setAttribute("Key", "");
element.setAttribute("BASE64Content", "");
element.setAttribute("Data", "C:/Users/jagilber/Downloads/whatev.jpg");

if (typeof(CapturedFrameId) != "undefined")
        element.setAttribute("CapturedFrameId", CapturedFrameId);


document.documentElement.appendChild(element);

var evt = document.createEvent("Events");
evt.initEvent("capturePageEvt", true, false);

element.dispatchEvent(evt);

Note: I don't know if this functionality is only available for the paid version or not.

like image 38
jamespgilbert Avatar answered Oct 17 '22 05:10

jamespgilbert


Perhaps http://html2canvas.hertzen.com/ could be used. Then you can capture the display and then process it.

like image 39
liftarn Avatar answered Oct 17 '22 07:10

liftarn


You might try PhantomJs, a headlesss browsing toolkit. http://phantomjs.org/

The following Javascript example demonstrates basic screenshot functionality:

var page = require('webpage').create();

page.settings.userAgent = 'UltimateBrowser/100';
page.viewportSize = { width: 1200, height: 1200 };
page.clipRect = { top: 0, left: 0, width: 1200, height: 1200 };

page.open('https://google.com/', function () {
  page.render('output.png');
  phantom.exit();
});
like image 43
Jens A. Koch Avatar answered Oct 17 '22 07:10

Jens A. Koch


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("<base href='" + location.href + "' />");

// 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.

like image 26
Seth Jeffery Avatar answered Oct 17 '22 06:10

Seth Jeffery


Print Screen? Old school and a couple of keypresses, but it works!

like image 42
Toby Avatar answered Oct 17 '22 05:10

Toby