-
Notifications
You must be signed in to change notification settings - Fork 0
/
screenshots.js
119 lines (94 loc) · 2.82 KB
/
screenshots.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
'use strict';
var system = require('system');
var webpage = require('webpage');
var fs = require('fs');
var configFile,
configPath,
currentActionCounter,
scriptArguments,
totalActionsCounter;
function joinUrlElements() {
var isAbsolute = false;
var regex = new RegExp('^\\/|\\/$','g');
var paths = Array.prototype.slice.call(arguments);
var url = '';
if (paths[0][0] === '/') {
isAbsolute = true;
}
url = paths.map(function(element){
return element.replace(regex,"");
}).join('/');
if (isAbsolute) {
url = '/' + url;
}
return url;
}
function takeAScreenshot(address, output, size, zoom, timer) {
var page = webpage.create();
totalActionsCounter++;
console.log('started ' + output + ' screenshot');
page.viewportSize = { width: size.width, height: size.height };
page.zoomFactor = zoom;
page.open(address, function (status) {
if (status !== 'success') {
console.log('Error: Unable to load the address ' + address);
return false;
} else {
window.setTimeout(function () {
page.render(output);
console.log('finished ' + output + ' screenshot');
setTimeout(function() {
page.close();
currentActionCounter++;
if (currentActionCounter === totalActionsCounter) {
phantom.exit();
}
}, 1);
return true;
}, timer);
}
});
}
function launchScreenshotsSeries(conf) {
var currentDevice,
currentView,
fullUrl,
outputFolder,
outputPath,
size,
zoom;
var baseFolder = 'render/' + conf.baseFolder;
var baseUrl = conf.baseUrl;
var devices = conf.devices;
var viewsList = conf.viewsList;
var timer = conf.pageLoadingTimer;
totalActionsCounter = 0;
currentActionCounter = 0;
for (var i = devices.length - 1; i >= 0; i--) {
currentDevice = devices[i];
size = currentDevice.size;
zoom = currentDevice.pixelDensity;
outputFolder = joinUrlElements(baseFolder, currentDevice.name);
for (var j = viewsList.length - 1; j >= 0; j--) {
currentView = viewsList[j];
outputPath = joinUrlElements(outputFolder, currentView.name) + '.png';
fullUrl = joinUrlElements(baseUrl, currentView.url);
takeAScreenshot(fullUrl, outputPath, size, zoom, timer);
}
}
return true;
}
scriptArguments = system.args;
if (scriptArguments.length !== 2) {
console.log('Usage: screenshots.js path-to-config-file');
phantom.exit();
} else {
// system.args contains the current script file name so we get directly the second argument
configPath = scriptArguments[1];
if (!fs.exists(configPath)) {
console.log('Error: couldn\'t find config file at ' + configPath);
} else {
configFile = JSON.parse(fs.read(configPath));
launchScreenshotsSeries(configFile);
}
}