Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
1N3
GitHub Repository: 1N3/Sn1per
Path: blob/master/bin/webscreenshot.js
2960 views
1
/***
2
# This file is part of webscreenshot.
3
#
4
# Copyright (C) 2014, Thomas Debize <tdebize at mail.com>
5
# All rights reserved.
6
#
7
# webscreenshot is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU Lesser General Public License as published by
9
# the Free Software Foundation, either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# webscreenshot is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU Lesser General Public License for more details.
16
#
17
# You should have received a copy of the GNU Lesser General Public License
18
# along with webscreenshot. If not, see <http://www.gnu.org/licenses/>.
19
***/
20
21
var Page = (function(custom_headers, http_username, http_password) {
22
var opts = {
23
width: 1200,
24
height: 800,
25
ajaxTimeout: 400,
26
maxTimeout: 800,
27
httpAuthErrorCode: 2
28
};
29
30
var requestCount = 0;
31
var forceRenderTimeout;
32
var ajaxRenderTimeout;
33
34
var page = require('webpage').create();
35
page.viewportSize = {
36
width: opts.width,
37
height: opts.height
38
};
39
40
page.settings.userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1944.0 Safari/537.36';
41
page.settings.userName = http_username;
42
page.settings.password = http_password;
43
44
page.customHeaders = custom_headers;
45
46
page.onInitialized = function() {
47
page.customHeaders = {};
48
};
49
// Silence confirmation messages and errors
50
page.onConfirm = page.onPrompt = page.onError = noop;
51
52
page.onResourceRequested = function(request) {
53
requestCount += 1;
54
clearTimeout(ajaxRenderTimeout);
55
};
56
57
page.onResourceReceived = function(response) {
58
if (response.stage && response.stage == 'end' && response.status == '401') {
59
page.failReason = '401';
60
}
61
62
if (!response.stage || response.stage === 'end') {
63
requestCount -= 1;
64
if (requestCount === 0) {
65
ajaxRenderTimeout = setTimeout(renderAndExit, opts.ajaxTimeout);
66
}
67
}
68
};
69
70
var api = {};
71
72
api.render = function(url, file) {
73
opts.file = file;
74
75
page.open(url, function(status) {
76
if (status !== "success") {
77
if (page.failReason && page.failReason == '401') {
78
// Specific 401 HTTP code hint
79
phantom.exit(opts.httpAuthErrorCode);
80
} else {
81
// All other failures
82
phantom.exit(1);
83
}
84
} else {
85
forceRenderTimeout = setTimeout(renderAndExit, opts.maxTimeout);
86
}
87
});
88
};
89
90
function renderAndExit() {
91
// Trick to avoid transparent background
92
page.evaluate(function() {
93
document.body.bgColor = 'white';
94
});
95
96
page.render(opts.file);
97
phantom.exit(0);
98
}
99
100
function noop() {}
101
102
return api;
103
});
104
105
function main() {
106
107
var system = require('system');
108
var p_url = new RegExp('url_capture=(.*)');
109
var p_outfile = new RegExp('output_file=(.*)');
110
var p_header = new RegExp('header=(.*)');
111
112
var p_http_username = new RegExp('http_username=(.*)');
113
var http_username = '';
114
115
var p_http_password = new RegExp('http_password=(.*)');
116
var http_password = '';
117
118
var temp_custom_headers = {
119
// Nullify Accept-Encoding header to disable compression (https://github.com/ariya/phantomjs/issues/10930)
120
'Accept-Encoding': ' '
121
};
122
123
for(var i = 0; i < system.args.length; i++) {
124
if (p_url.test(system.args[i]) === true)
125
{
126
var URL = p_url.exec(system.args[i])[1];
127
}
128
129
if (p_outfile.test(system.args[i]) === true)
130
{
131
var output_file = p_outfile.exec(system.args[i])[1];
132
}
133
134
if (p_http_username.test(system.args[i]) === true)
135
{
136
http_username = p_http_username.exec(system.args[i])[1];
137
}
138
139
if (p_http_password.test(system.args[i]) === true)
140
{
141
http_password = p_http_password.exec(system.args[i])[1];
142
}
143
144
if (p_header.test(system.args[i]) === true)
145
{
146
var header = p_header.exec(system.args[i]);
147
var p_header_split = header[1].split(': ', 2);
148
var header_name = p_header_split[0];
149
var header_value = p_header_split[1];
150
151
temp_custom_headers[header_name] = header_value;
152
153
}
154
}
155
156
if (typeof(URL) === 'undefined' || URL.length == 0 || typeof(output_file) === 'undefined' || output_file.length == 0) {
157
console.log("Usage: phantomjs [options] webscreenshot.js url_capture=<URL> output_file=<output_file.png> [header=<custom header> http_username=<HTTP basic auth username> http_password=<HTTP basic auth password>]");
158
console.log('Please specify an URL to capture and an output png filename !');
159
160
phantom.exit(1);
161
}
162
else {
163
var page = Page(temp_custom_headers, http_username, http_password);
164
page.render(URL, output_file);
165
}
166
}
167
168
main();
169