2 * Captures the full height document even if it's not showing on the screen or captures with the provided range of screen sizes.
4 * A basic example for taking a screen shot using phantomjs which is sampled for https://nodejs-dersleri.github.io/
6 * usage : phantomjs responsive-screenshot.js {url} [output format] [doClipping]
9 * phantomjs responsive-screenshot.js https://nodejs-dersleri.github.io/
10 * phantomjs responsive-screenshot.js https://nodejs-dersleri.github.io/ pdf
11 * phantomjs responsive-screenshot.js https://nodejs-dersleri.github.io/ true
12 * phantomjs responsive-screenshot.js https://nodejs-dersleri.github.io/ png true
14 * @author Salih sagdilek <salihsagdilek@gmail.com>
18 * http://phantomjs.org/api/system/property/args.html
20 * Queries and returns a list of the command-line arguments.
21 * The first one is always the script name, which is then followed by the subsequent arguments.
23 var args = require('system').args;
25 * http://phantomjs.org/api/fs/
29 var fs = require('fs');
32 * http://phantomjs.org/api/webpage/
36 var page = new WebPage();
39 * if url address does not exist, exit phantom
41 if ( 1 === args.length ) {
42 console.log('Url address is required');
47 * setup url address (second argument);
49 var urlAddress = args[1].toLowerCase();
53 * set output extension format
56 var ext = getFileExtension();
62 var clipping = getClipping();
90 page.open(urlAddress, function (status) {
91 if ( 'success' !== status ) {
92 console.log('Unable to load the url address!');
94 var folder = urlToDir(urlAddress);
100 page.viewportSize = viewports[key];
102 page.clipRect = viewports[key];
104 output = folder + "/" + getFileName(viewports[key]);
105 console.log('Saving ' + output);
111 render(viewports.length);
117 * filename generator helper
121 function getFileName(viewport) {
129 d.getHours() <= 9 ? '0' + d.getHours() : d.getHours(),
130 d.getMinutes() <= 9 ? '0' + d.getMinutes() : d.getMinutes(),
131 d.getSeconds() <= 9 ? '0' + d.getSeconds() : d.getSeconds(),
134 var resolution = viewport.width + (clipping ? "x" + viewport.height : '');
136 return date.join('-') + '_' + time.join('-') + "_" + resolution + ext;
140 * output extension format helper
144 function getFileExtension() {
145 if ( 'true' != args[2] && !!args[2] ) {
146 return '.' + args[2];
156 function getClipping() {
157 if ( 'true' == args[3] ) {
159 } else if ( 'true' == args[2] ) {
166 * url to directory helper
171 function urlToDir(url) {
173 .replace(/^(http|https):\/\//, '')
176 if ( !fs.makeTree(dir) ) {
177 console.log('"' + dir + '" is NOT created.');