forked from oduwsdl/wdill
-
Notifications
You must be signed in to change notification settings - Fork 2
/
webshots.js
executable file
·329 lines (250 loc) · 9.49 KB
/
webshots.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
A script using PhantomJS http://phantomjs.org to create Web page screenshot at
the given browser resolution and save it as a PNG file.
Written by Ramiro Gómez http://ramiro.org/
MIT licensed: http://rg.mit-license.org/
*/
var system = require('system')
var page = require('webpage').create(),
re_trim = /^https?:\/\/|\/$/g,
re_conv = /[^\w\.-]/g;
var redirectionFlag = 0;
var globalW = 1024;
var globalH = 768;
//var url2filename = function(url, w, h)
//{
//return extractYearFromUrl(url) + '.png';
//return url
//.replace(re_trim, '')
//.replace(re_conv, '-')
//+ '.' + w + 'x' + h + '.png'
//}
function forceClose()
{
phantom.exit();
}
/*
function extractYearFromUrl(url)
{
var lasIndexOfHTTPMarker = url.lastIndexOf("/http://");
var yearExtract = '';
for (var i = lasIndexOfHTTPMarker; i > -1; i--)
{
if(isNaN(url[i]) === false)
{
//yearExtract = url[i] + yearExtract;
}
}
yearExtract = yearExtract.substring(0,4);
return yearExtract;
}
*/
//retrive item all items enclosed by beginMarker and endMarker, with search position at the position of the signatureString
function onClickRedirectionResolver(pageContent, signatureString, beginMarker, endMarker)
{
if(pageContent.length > 0 && signatureString.length > 0 && beginMarker.length > 0 && endMarker.length > 0)
{
var positionOfSignatureString = pageContent.indexOf(signatureString);
if(positionOfSignatureString > -1)
{
var urls = [];
var count = 0;
var url = '';
do
{
url = getRedirectUrl(pageContent, beginMarker, endMarker, positionOfSignatureString);
if(url.length > 0)
{
var offset = pageContent.indexOf(url, positionOfSignatureString);
offset = offset + url.length;
urls[count] = url;
count = count + 1;
positionOfSignatureString = offset;
}
}
while(url.length>0);
return urls;
}
}
return [];
}
/*
function isLetter(c)
{
c = c.toUpperCase();
return c >= "A" && c <= "Z";
}
*/
//resorted to this since response.redirectURL always returns null
function getRedirectUrl(pageContent, startStringSignature, endStringSignature, startSearchPosition)
{
if(pageContent.length > 0 && startStringSignature.length > 0 && endStringSignature.length > 0 && startSearchPosition > -1)
{
//var startStringSignature = '<p class="impatient"><a href="';
//var endStringSignature = '">Impatient?</a></p>';
var indexOfStartSignature = pageContent.indexOf(startStringSignature, startSearchPosition);
if(indexOfStartSignature != -1)
{
var indexOfEndSignature = pageContent.indexOf(endStringSignature, indexOfStartSignature + startStringSignature.length);
var redirectUrl = pageContent.substring(indexOfStartSignature + startStringSignature.length, indexOfEndSignature);
//console.log("*PAGE*");
//console.log(pageContent);
//console.log("*PAGE*");
//console.log("");
return redirectUrl;
}
else
{
return "";
}
}
else
{
return "";
}
}
function getHost(url)
{
if(url.length > 0)
{
var a = document.createElement('a');
a.href = url;
return a.hostname;
}
}
var webshot = function(url, w, h, timeout, folderName, filename)
{
console.log('');
console.log('===> webshot url: ' + url);
page.viewportSize = { width: w, height: h };
//page.onError = function(msg, trace)
//{
//console.log('=onPageError');
//console.log(msg);
//var msgStack = ['ERROR: ' + msg];
//if (trace && trace.length)
//{
// msgStack.push('TRACE:');
// trace.forEach(function(t)
// {
// msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function +'")' : ''));
// });
//}
//console.error(msgStack.join('\n'));
//};
page.open(url, function(status)
{
if (status !== 'success')
{
console.log('Unable to load url: ' + url + ', status: ' + status + ', timeout: ' + timeout);
//phantom.exit(); prematurely kills the redirection of phantomjs therefore delay exit
setTimeout(forceClose, 10000);
//phantom.exit();
}
else
{
//redirection solution 1 - start: timed redirection
var pageContent = page.content;
var redirectUrlValue = getRedirectUrl(pageContent, '<p class="impatient"><a href="', '">Impatient?</a></p>', 0);
if(redirectUrlValue.length > 0)
{
//console.log('redirectUrlValue.length > 0');
//temp = 'abc'
//ATTEMPT 1 - redirection
var hostname = getHost(url);
var indexOfHostname = url.indexOf(hostname);
var fullHostName = url.substring(0,indexOfHostname + hostname.length);
fullHostName = fullHostName.trim();
if(fullHostName.length > 0)
{
var newUrl = fullHostName + redirectUrlValue;
//console.log('redirectUrl: ' + newUrl);
console.log('...type 1 redirect detected');
redirectionFlag = 10000;
//parameters don't matter since redirectionFlag sets correct values
webshot(newUrl, 1024, 768, 10000);
}
}
//redirection solution 1 - end
//redirection solution 2 - start: static onClick redirection
var staticRedirectionMarker = "<h1>Site redirect requested (302)</h1>";
var beginMarker = 'href="';
var endMarker = '"';
//gotoUrlsArray contains all links following staticRedirectionMarker
var gotoUrlsArray = onClickRedirectionResolver(pageContent, staticRedirectionMarker, beginMarker, endMarker);
if(gotoUrlsArray.length > 0)
{
console.log('...type 2 redirect detected');
redirectionFlag = 10000;
//redirect to the first link
//parameters don't matter since redirectionFlag sets correct values
webshot(gotoUrlsArray[0], 1024, 768, 10000);
}
//redirection solution 2 - end
if(redirectionFlag !== 0)
{
console.log('redirectUrl: ' + url);
timeout = redirectionFlag;
w = globalW;
h = globalH;
redirectionFlag = 0;
}
window.setTimeout(function()
{
page.clipRect = { top: 0, left: 0, width: w, height: h};
//filename = url2filename(url, w, h)
//var indexOfHTTP = url.lastIndexOf(".");
//var folderName = '';
//i = indexOfHTTP - 1;
//console.log('fName: ' + foldName)
//review this block, folder name should be passed as argument, not recalculated
//while(i > -1)
//{
// if(isLetter(url[i]))
// {
// folderName = url[i] + folderName;
// }
// else
// {
// break;
// }
// i = i - 1;
//}
filename = './'+folderName+ '/' + filename + '.png';
page.evaluate(function()
{
//annotate file before taking screenshot
//var frag = document.createDocumentFragment(),
//element = document.createElement('div');
//element.innerHTML = '<center><h1> TITLE GOES HERE </h1></center>';
//while (element.firstChild)
//{
// frag.appendChild(element.firstChild);
//}
////Native DOM method can also be used to insert frag
//document.body.insertBefore(frag, document.body.childNodes[0]);
if ('transparent' === document.defaultView.getComputedStyle(document.body).getPropertyValue('background-color'))
{
document.body.style.backgroundColor = '#fff';
}
});
page.render(filename);
//console.log('rendered ' + filename + ',timeout: ' + timeout);
phantom.exit();
}, timeout);
}
});
};
// phantom.args is deprecated in favor of system.args, but version 1.4.0 does
// not seem to support the system module.
if (6 !== system.args.length)
{
console.log('Usage: phantomjs webshots.js http://example.com 1024 768 folderName filename');
phantom.exit();
}
else
{
//setTimeout(forceClose, 10000);
//pass folder name as argument
webshot(system.args[1], system.args[2], system.args[3], 1000, system.args[4], system.args[5]);
}