How can I capture webpage screenshots with JavaScript? - javascript

I want to take a screenshot of the webpage so that it can be used to share on various social media platforms. The webpage has visual elements displayed in iframe which are from different domain (not same origin).
So far i have checked
html2canvas, but looks like there is a limitation where the screenshot wolud not be generated properly if there are visual elements on the page loaded from different origin
getUserMedia: but looks like this triggers a user image capture and not webpage capture. here is link to codepen i found https://codepen.io/jgalazm/pen/bGEgEGW
Here are couple of my questions:
Is my understanding correct that getUserMedia cannot be used to capture webpage screenshot?
What other alternatives i can use to capture the webpage screenshot?
Thanks.

what I've found best is to handle this server side.
So you use selenium with chrome / firefox / phantom js and you render and then take the screen shot through the selenium api.
selenium is primarly Java or Python
https://pythonspot.com/selenium-take-screenshot/
if javascript is more of your thing than opt for puppeteer
https://buddy.works/guides/how-take-screenshots-with-puppeteer-headless-chrome
Deployment
If I knew more about your backend then I could answer more thoroughly.
If you are using a cloud provider, I'd recommend a serverless function.
If you have a node server than Puppeteer
If you have a python server than selenium.

Related

How to encrypt/prevent images from being downloaded straight from browser?

I am creating an application that is transmitting images but the way basic javascript works the images can potentially be downloaded straight from the console in JS. I wanted to know if there is a solution for transmitting images that can't be downloaded from the console. I have seen topics about encrypted media extensions that are used by such as netflix. Is this a solution for what I have described? If so how do you use it with a library such as React?
I don't know if it's appropriate to answer this way, but there is not a true way of preventing images from being downloaded from the browser. For the user to see an image served by your website, it has to be loaded into the browser's memory. While there are tricks to prevent a simple right-click and "Save," your question is asking if it's possible to prevent transmitted images from being saved via console (or in general, it seems). The answer to that is "no", since its presence in memory opens it up to all manner of copying it. Could be a simple screenshot or getting it from the dev tools sources tab or via console script.

Javascript API to screen scrape page flow, including button click

We are looking for Javascript API to screen scrape page flow, including button click. If it were on server side, WebDriver of Selenium would have been a great choice, but we want the screen scraping to run on the client browser. The screens to be scrapped is a transaction in itself (login to third party website, transaction step 1, step 2 and then final confirmation). Any javascript API available?
AFAIK, both nodeJS and phantom JS don't have capability to click a button from the scrapped page.
thanks in advance,
abbas
Webdriver is an HTTP based protocol, something that every browser speaks, so it is possible to control one browser from another. I've written a tutorial some weeks ago on that topic here
I recently ran into DalekJS (http://dalekjs.com/docs/actions.html) which allows for taking screenshots of pages and clicking on the elements as well. I think it even supports multiple browsers -- although they have to be installed (http://dalekjs.com/pages/documentation.html#browsers).
here is the sample code directly from the link:
test.open('http://doctorwhotv.co.uk/')
.assert.text('#nav').is('Navigation')
.assert.visible('#nav')
.assert.attr('#nav', 'data-nav', 'true')
.click('#nav')
.done();

Use JS or some applet to create a screenshot in Firefox?

I'm creating a personal home page, due to the fact that iGoogle will be discontinued. One of the things I'm trying to create, is a speed dial-type interface, with website thumbnails as links, and I'd like to automate this process.
I've attempted screenshot automation a few years back with linux and the webkit engine. And it's fine. But my problem is, that I want the screenshots to be from my browser, i.e. my Gmail inbox, not the login page I'd get if attempting a remote screenshot.
I thought of using html2canvas but again, I'd have to load the source of the webpages remotely using a proxy, and that's not what I want. Another attempt of mine, was to load the website in an iframe, extract the source, and pass it on to html2canvas. Unfortunately most websites like google, facebook etc don't allow embeding their websites into iframes, so I'm still stuck.
How do plugins like FoxTab, and SpeedDial make the screenshots from within the browser without popups etc? They do it "browser side" silently, is it possible to duplicate this using just JavaScript? Or is there a way I could accomplish the same in another way, perhaps with a custom addon or something?..
Have you considered using a service like http://webthumbnail.org/ ?
http://phantomjs.org/ is also a great service for that if you want to do it yourself.
Take a look at phatomjs. We use it to take screenshots of all our hosted sites periodically. Phantomjs is a headless Webkit implementation.

Convert HTML page into image using Javascript

Hi,
EDIT: I want to achieve it by with out using any third party software..My application is a SAP product and I cannot go to every customer and install that software in his system.
I have the following scenario.. There is a button in my webiste(ofcourse,its a business applicaion) named "Save as image".so whenever user presses that button the content of the page has to be converted to image file and saved in his system.
Can we achieve it by either javascript or jquery?
If we cannot do it in javascript ,can we do it in SAP BSP,since my application is being developed in SAP BSP?
I had already searched in this site and found one solution which only works in Firefox extesnion. But I need a cross-browser solution which must work for IE,Chromer,ect.
The way to do this would be render the HTML page in hidden <canvas> element and then saving the canvas content as an image.
It is possible, but you won't have perfect rendering output or a solution working on legacy browsers.
Please see
https://stackoverflow.com/a/12660867/315168
for more information.
Alternatively submit a stateless page URL to the server-side where a headless browser renders the page and takes a screenshot using Selenium automation. If the page is public some web services exist for this too.
One easy but partial IE solution, it uses ActiveX so not crosswbrowser and a general one that is a bit more cumbersome
The IE solution
function printScreen(){
var oWordBasic;
if (window.ActiveXObject){
oWordBasic = new ActiveXObject("Word.Basic");
oWordBasic.SendKeys('%{1068}');
oWordBasic.SendKeys('%{PRTSC}'); //or if the above doesnt work..
//save or transfer the clipboard contensts
}
}
The general solution:
Use a screen capture utility like Gadwin PrintScreen http://www.gadwin.com/printscreen/, it's for windows but i'm sure there are the like for Linux and Mac. You can define a hotkey and let this save the image to a fixed location with autonumbering. The program doesn't need to be installed, it's portable so it can reside on a networkshare.
you may also try to create java applet or jar included into site that would capture some part of the screen or browser. i saw mechanism for real time screen sharing based on jar loaded files, but i didn't see the code.
here is some lib for java html into image conversion
http://code.google.com/p/java-html2image/
heres simmilar topic
How to capture selected screen of other application using java?

Does anyone know of a GUI-less application that can be called from JavaScript to create and save desktop screen shots?

While the subject could sound like I'm looking to do something shifty, I'm not; I maintain an internal web site used by several hundred phone operators, and would like to add the following functionality:
I would like to add a control in the header of all of the web pages that would capture an image of the entire desktop and save the image as a file to a shared network drive to assist in troubleshooting production problems. This screen capture app would be called by JavaScript.
I've researched many threads on this site pertaining to capturing screenshots, and all of the offered applications don't meet my need in one of two ways:
The screen capture application has a GUI that pops up and the user sizes some sort of capture control or interacts with a window to do the capture. The users are not very computer literate, and could not be trusted with using a "pop-up" application correctly-- and it would be impossible to enforce them to save the image file with a common file naming convention. I would like the user to press a single UI control on a web site and have JavaScript make the calls to obtain a screen shot and save the image without any further user interaction.
Some automated applications save the HTML by re-posting to the site and "re-assembles" the individual HTML elements into an image. This will not work as the input data that the operator has typed in needs to be in the image, the site uses AJAX so the visual "state" of the web page will be different from one re-obtained from a POST, and some applications have had (active directory) security issues when trying to interact with our (secured) web sites.
If there isn't an application that will meet this need, I'll just roll my own control in C#. But I'd rather obtain a third-party control. so I don't have to support my own control for life. :-)
Javascript does not provide access to the local system for this functionality due to the security risk.
I believe the only way of doing this on a web page is possibly via an active X control or Java applet (similar to screencast-o-matic) but even then security may be an issue.
I also image unless your own c# control is in a windows forms application it would not work as c# controls that are part of a web site will be run on the server and not the client.
Hope this is of some help.
Well to do that you'll need something to interact with the desktop which can't be done by javascript alone.
You'll need to have an Active X Object to be able to interact with the client's machine. Once you have that, it is easy to just have the object take a picture and save it to the directory.
I believe I have the C# code to take a screen shot. If you want I'll post it.
Some alternatives to ActiveX / Java:
You can create a flash program to do the screen capture. I haven't done this and don't know the details, but I'm fairly sure it can be done.
You can setup a custom protocol for screen capture (eg. screencapture:// ), and in your header include a
Screen Capture
link. You'll need to find a gui-less screen capture program to do the actual capturing and set it up as the handler for that custom protocol.
Did it once. I couldn't find a direct way so did it rendering HTML to canvas, then canvas to image using html2canvas. Canvas to image is a much more common theme so you'll find many tutorials about.
HTML to Canvas
Canvas to image
NOTE: rendering from HTML to canvas may leave some elements behind, like SVG. But overall it did as much as expected.
PSR (problem step recorder) is a great tool for debugging purposes on client machines and it is available on all windows machines by default.
Try below and see if it works for you.
Go to Start->Run and type psr.exe (or just psr) this should open the recorder. This is fairly easy to use, so even users/clients can use it. If it works then try below steps to setup.
Step 1: Register PSR protocol. This tells the browser what to do when a link on the page is referring to PSR protocol (which we will use to open this recorder).
- Create registry values as below
HKEY_CLASSES_ROOT/
PSR/
(Default) "URL:PSR Protocol"
URL Protocol ""
shell/
open/
command/
(Default) psr.exe
If you are not comfortable with creating registry values, download and double click this file.
Use this link for more information.
Step 2: Now create an example html file to test launching PSR.
Save the below code to index.html file and open it.
<html>
<head>
<head>
</head>
<body>
Start Recording
</body>
</head>
</html>
Or download this file and open it.
This should open your PSR. It is easy to use and can record all the information you need for debugging.

Categories

Resources