BlueGit

blue-star danique-bot

Files

danique-bot / node_modules / phantomjs-prebuilt / lib / phantom / examples / responsive-screenshot.js

responsive-screenshot.js

Import template
timfa Tim · pushed by timfa · 4 years ago
c788ad20 History
responsive-screenshot.js 3.9 KiB Download Raw
1/**
2 * Captures the full height document even if it's not showing on the screen or captures with the provided range of screen sizes.
3 *
4 * A basic example for taking a screen shot using phantomjs which is sampled for https://nodejs-dersleri.github.io/
5 *
6 * usage : phantomjs responsive-screenshot.js {url} [output format] [doClipping]
7 *
8 * examples >
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
13 *
14 * @author Salih sagdilek <salihsagdilek@gmail.com>
15 */
16
17/**
18 * http://phantomjs.org/api/system/property/args.html
19 *
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.
22 */
23var args = require('system').args;
24/**
25 * http://phantomjs.org/api/fs/
26 *
27 * file system api
28 */
29var fs = require('fs');
30
31/**
32 * http://phantomjs.org/api/webpage/
33 *
34 * Web page api
35 */
36var page = new WebPage();
37
38/**
39 * if url address does not exist, exit phantom
40 */
41if ( 1 === args.length ) {
42 console.log('Url address is required');
43 phantom.exit();
44}
45
46/**
47 * setup url address (second argument);
48 */
49var urlAddress = args[1].toLowerCase();
50
51
52/**
53 * set output extension format
54 * @type {*}
55 */
56var ext = getFileExtension();
57
58/**
59 * set if clipping ?
60 * @type {boolean}
61 */
62var clipping = getClipping();
63
64/**
65 * setup viewports
66 */
67var viewports = [
68 {
69 width : 1200,
70 height : 800
71 },
72 {
73 width : 1024,
74 height : 768
75 },
76 {
77 width : 768,
78 height : 1024
79 },
80 {
81 width : 480,
82 height : 640
83 },
84 {
85 width : 320,
86 height : 480
87 }
88];
89
90page.open(urlAddress, function (status) {
91 if ( 'success' !== status ) {
92 console.log('Unable to load the url address!');
93 } else {
94 var folder = urlToDir(urlAddress);
95 var output, key;
96
97 function render(n) {
98 if ( !!n ) {
99 key = n - 1;
100 page.viewportSize = viewports[key];
101 if ( clipping ) {
102 page.clipRect = viewports[key];
103 }
104 output = folder + "/" + getFileName(viewports[key]);
105 console.log('Saving ' + output);
106 page.render(output);
107 render(key);
108 }
109 }
110
111 render(viewports.length);
112 }
113 phantom.exit();
114});
115
116/**
117 * filename generator helper
118 * @param viewport
119 * @returns {string}
120 */
121function getFileName(viewport) {
122 var d = new Date();
123 var date = [
124 d.getUTCFullYear(),
125 d.getUTCMonth() + 1,
126 d.getUTCDate()
127 ];
128 var time = [
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(),
132 d.getMilliseconds()
133 ];
134 var resolution = viewport.width + (clipping ? "x" + viewport.height : '');
135
136 return date.join('-') + '_' + time.join('-') + "_" + resolution + ext;
137}
138
139/**
140 * output extension format helper
141 *
142 * @returns {*}
143 */
144function getFileExtension() {
145 if ( 'true' != args[2] && !!args[2] ) {
146 return '.' + args[2];
147 }
148 return '.png';
149}
150
151/**
152 * check if clipping
153 *
154 * @returns {boolean}
155 */
156function getClipping() {
157 if ( 'true' == args[3] ) {
158 return !!args[3];
159 } else if ( 'true' == args[2] ) {
160 return !!args[2];
161 }
162 return false;
163}
164
165/**
166 * url to directory helper
167 *
168 * @param url
169 * @returns {string}
170 */
171function urlToDir(url) {
172 var dir = url
173 .replace(/^(http|https):\/\//, '')
174 .replace(/\/$/, '');
175
176 if ( !fs.makeTree(dir) ) {
177 console.log('"' + dir + '" is NOT created.');
178 phantom.exit();
179 }
180 return dir;
181}