-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathscreenshot.js
280 lines (231 loc) · 7.52 KB
/
screenshot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
function fixPageBackground(page) {
page.evaluate(function() {
var style = document.createElement('style'),
text = document.createTextNode('BODY { background-color: #fff }');
style.setAttribute('type', 'text/css');
style.appendChild(text);
document.head.insertBefore(style, document.head.firstChild);
});
}
function generateJSON() {
console.log('generate json');
var devicesScreens = new Object();
for (var deviceIndex in devices) {
var currentDevice = devices[deviceIndex];
devicesScreens[currentDevice.getDeviceName()] = currentDevice.getScreenshot();
}
return JSON.stringify(devicesScreens);
}
function allDevicesDone() {
var doneDevices = 0;
for (var deviceIndex in devices) {
var currentDevice = devices[deviceIndex];
if (currentDevice.getStatus() == 'done') {
doneDevices += 1;
}
}
if (doneDevices == devices.length) {
return true;
} else {
return false;
}
}
function onDeviceScreenshot(status, device) {
device.setStatus('done');
if (allDevicesDone() == true) {
console.log('send for resizing');
recreatePage();
page.open(serverUrl + 'resize?screenshots='+generateJSON()+'&domain='+domain, function() {
resetDevices();
readServerResponse();
});
}
}
function screenshotInProgress() {
for (var deviceIndex in devices) {
var currentDevice = devices[deviceIndex];
if (currentDevice.getStatus() == 'working') {
return true;
}
}
return false;
}
function recreatePage() {
try {
page.close()
page = require('webpage').create();
} catch (error) {
page = require('webpage').create();
}
}
function takeScreenshot(device) {
if (screenshotInProgress() == true) {
console.log(' waiting....');
//wait until page is free
//because we can't use async webpage in phantomjs
setTimeout(function () { takeScreenshot(device) }, 1000);
return;
}
device.setStatus('working');
recreatePage();
//generate screenshot path
var screenPath = screenshotsPath + domain + '_' + device.getDeviceName() + '.png';
//settings
page.clipRect = { top: 0, left: 0, width: device.getWidth(), height: device.getHeight() };
page.viewportSize = { width: device.getWidth(), height: device.getHeight() };
page.settings.userAgent = device.getUserAgent()
page.settings.resourceTimeout = 4000;
//hotfix, if requests fails more than one time, mark screenshot as done and
//move on, other wise it is stuck on forever waiting, example page:
//http://jangforum.com
var lastFailedUrl = null;
page.onResourceTimeout = function(request) {
console.log('Response (#' + request.id + '): ' + JSON.stringify(request));
if (lastFailedUrl == request.url) {
onDeviceScreenshot(null, device);
}
lastFailedUrl = request.url;
};
//open page
page.open('http://'+domain+'/', function(status) {
if (status != 'success') {
console.log('Can\'t open ' + domain);
} else {
console.log(' Rendering ' + device.getDeviceName() + ' screenshot..');
fixPageBackground(page);
page.render(screenPath);
console.log(' Done');
device.setScreenshot(screenPath);
}
onDeviceScreenshot(null, device);
});
}
function resetDevices() {
for (var deviceIndex in devices) {
var currentDevice = devices[deviceIndex];
//reset device status and screenshotpath
currentDevice.reset();
}
}
function runDevicesScreenshots() {
console.log('runDevicesScreenshots() for: ' + domain);
resetDevices();
for (var deviceIndex in devices) {
var currentDevice = devices[deviceIndex];
takeScreenshot(currentDevice);
}
}
function readServerResponse() {
if (screenshotInProgress() == true) {
//console.log('Waiting for screenshots...');
setTimeout(function () { readServerResponse() }, 2000);
return;
}
screenshotsCount += 1;
if (screenshotsCount >= 10) {
console.log('Count reached max...restart this process');
phantom.exit();
}
domain = null;
recreatePage();
page.settings.resourceTimeout = 10000;
page.onResourceTimeout = function(e) {
console.log('Server timeout.');
setTimeout(function () { readServerResponse() }, 2000);
return;
};
page.open(serverUrl + 'get_domain', function(status) {
if (status != 'success') {
//http request to the server failed
console.log('ERROR: Communication to the server failed');
setTimeout(function () { readServerResponse() }, 2000);
return;
} else {
var serverResponse = page.evaluate(function () {
return document.body.innerHTML
});
if (serverResponse == 'empty') {
console.log('No domains left in the queue. Retry..');
setTimeout(function () { readServerResponse() }, 2000);
return;
} else {
domain = serverResponse;
runDevicesScreenshots(domain);
}
}
});
}
function Device() {
this.width = 0;
this.height = 0;
this.userAgent = '';
this.deviceName = '';
this.screenshotPath = null;
this.status = null;
this.reset = function () {
this.status = null;
this.screenshotPath = null;
}
this.setStatus = function (status) {
this.status = status;
}
this.getStatus = function () {
return this.status;
}
this.setDeviceName = function (name) {
this.deviceName = name;
}
this.getDeviceName = function () {
return this.deviceName;
}
this.setWidth = function (width) {
this.width = width;
}
this.getWidth = function () {
return this.width;
}
this.setHeight = function (height) {
this.height = height;
}
this.getHeight = function () {
return this.height;
}
this.setUserAgent = function (userAgent) {
this.userAgent = userAgent;
}
this.getUserAgent = function () {
return this.userAgent;
}
this.setScreenshot = function (path) {
this.screenshotPath = path;
}
this.getScreenshot = function () {
return this.screenshotPath;
}
}
var screenshotsCount = 0;
var domain = null;
var page = require('webpage').create();
var devices = new Array();
var iPhone = new Device();
iPhone.setDeviceName('iPhone');
iPhone.setWidth(640);
iPhone.setHeight(1136);
iPhone.setUserAgent('Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3');
devices.push(iPhone);
var iPad = new Device();
iPad.setDeviceName('iPad');
iPad.setWidth(1024);
iPad.setHeight(768);
iPad.setUserAgent('Mozilla/5.0 (iPad; CPU OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B176 Safari/7534.48.3');
devices.push(iPad);
var laptop = new Device();
laptop.setDeviceName('laptop');
laptop.setWidth(1280);
laptop.setHeight(768);
laptop.setUserAgent('Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/30.0.1599.114 Chrome/30.0.1599.114 Safari/537.36');
devices.push(laptop);
var serverUrl = 'http://0.0.0.0:8088/';
var args = require('system').args;
var screenshotsPath = args[1]
readServerResponse();