-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
ee.Screen.js
211 lines (189 loc) · 9.27 KB
/
ee.Screen.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
/***************************************************************************************************
ee.Screen // version 20201108
Takes screenshots of web pages according to a list of URLs
Supports various resolutions and multiple formats - JPG, PDF, PNG and TXT
For more info see file *readme.md*
***************************************************************************************************/
var fs = require('fs');
var os = require('os');
var puppeteer = require(os.homedir() + '\\node_modules\\puppeteer');
var screenshotsDefaultFolder = require('path').resolve(__dirname, '..') + '\\screenshots';
var listOfNotProcessed = 'notProcessed.log';
var whereUrls = 'urls.txt'; if (process.argv[2]) { whereUrls = process.argv[2]; }
var delayBeforeScreening = 1500;
var pageLoadTimeout = 15000;
var devicePixelRatio = 1;
var opSys = process.platform;
if (opSys.includes('darwin'))
{
var customUserAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6)' +
' AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36';
}
else if (opSys.includes('linux'))
{
var customUserAgent = 'Mozilla/5.0 (X11; Linux x86_64)' +
' AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36';
}
else if (opSys.includes('win'))
{
var customUserAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)' +
' AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36';
}
function sleep(ms) { ms += new Date().getTime(); while (new Date() < ms) {} }
var firstbytes = new Buffer.alloc(5);
var fd = fs.openSync(whereUrls);
fs.readSync(fd, firstbytes, 0, 5, 0);
fs.closeSync(fd);
var encoding = false;
if (!encoding && firstbytes[0] === 0xEF && firstbytes[1] === 0xBB && firstbytes[2] === 0xBF)
{ encoding = 'utf8'; }
if (!encoding && firstbytes[0] === 0xFE && firstbytes[1] === 0xFF)
{ encoding = 'utf16be'; }
if (!encoding && firstbytes[0] === 0xFF && firstbytes[1] === 0xFE)
{ encoding = 'utf16le'; }
if (!encoding && firstbytes[0] === 0x00 && firstbytes[1] === 0x00 &&
firstbytes[2] === 0xFE && firstbytes[3] === 0xFF)
{ encoding = 'utf32be'; }
if (!encoding && firstbytes[0] === 0xFF && firstbytes[1] === 0xFE &&
firstbytes[2] === 0x00 && firstbytes[3] === 0x00)
{ encoding = 'utf32le'; }
if (!encoding && firstbytes[0] === 0x2B && firstbytes[1] === 0x2F && firstbytes[2] === 0x76)
{ encoding = 'utf7'; }
if (!encoding && firstbytes[0] === 0xF7 && firstbytes[1] === 0x64 && firstbytes[2] === 0x4C)
{ encoding = 'utf1'; }
if (!encoding)
{ encoding = 'ascii'; }
var list = fs.readFileSync(whereUrls, encoding,
(err) => { if (err) { throw err; } }).toString().split("\n");
;(async () => {
const pupArgs = ['--no-sandbox',
'--disable-setuid-sandbox',
'--disable-infobars',
'--window-position=0,0',
'--ignore-certificate-errors',
'--ignore-certificate-errors-spki-list'];
const pupOptions = { pupArgs, ignoreHTTPSErrors: true }
const browser = await puppeteer.launch(pupOptions);
const page = await browser.newPage();
var notProcessed = [];
for (line in list)
{
try
{
if (list[line].includes('http'))
{
list[line] = list[line].replace("\ufeff", "")
.replace("\ufffe", "")
.replace("\u0000feff", "")
.replace("\u2b2f76", "")
.replace("\uf7644c", "")
var lineUrl = "";
var lineTit = screenshotsDefaultFolder + '\\~flatUrl~.*';
var lineRes = '1280*full';
var lineExt = 'png';
var jpgExists = pdfExists = pngExists = txtExists = 0;
var toJpg = toPdf = toPng = toTxt = 0;
subLines = list[line].split(';');
for (field in subLines)
{
if (subLines[field].includes('"'))
{ lineTit = "" + subLines[field].split("\"").join(""); continue; }
if (subLines[field].includes('/'))
{ lineUrl = "" + subLines[field]; continue; }
if (subLines[field].includes('*') && !subLines[field].includes('"'))
{ lineRes = "" + subLines[field]; continue; }
if (subLines[field].includes('jpg') && !subLines[field].includes('"') &&
!subLines[field].includes('/') && !subLines[field].includes('*'))
{ lineExt = "" + subLines[field]; }
if (subLines[field].includes('pdf') && !subLines[field].includes('"') &&
!subLines[field].includes('/') && !subLines[field].includes('*'))
{ lineExt = "" + subLines[field]; }
if (subLines[field].includes('png') && !subLines[field].includes('"') &&
!subLines[field].includes('/') && !subLines[field].includes('*'))
{ lineExt = "" + subLines[field]; }
if (subLines[field].includes('txt') && !subLines[field].includes('"') &&
!subLines[field].includes('/') && !subLines[field].includes('*'))
{ lineExt = "" + subLines[field]; }
}
if (lineTit.includes('~flatUrl~'))
{
var lineUrlFlat = lineUrl.replace(/[^a-z0-9.]+/gi, "_");
lineTit = lineTit.replace('~flatUrl~', lineUrlFlat);
}
var getPath = lineTit.substring(0, lineTit.lastIndexOf("\\"));
if (!fs.existsSync(getPath)) { fs.mkdirSync(getPath); }
var subLineRes = lineRes.split('\*');
var shotWidth = Number(subLineRes[0]);
if (subLineRes[1].includes('full')) { var shotHeight = 720; }
else { var shotHeight = Number(subLineRes[1]); }
if (lineExt.includes('jpg')) { toJpg = 1; }
if (lineExt.includes('pdf')) { toPdf = 1; }
if (lineExt.includes('png')) { toPng = 1; }
if (lineExt.includes('txt')) { toTxt = 1; }
if (fs.existsSync(lineTit.replace(".*", ".jpg").split("\"").join("")))
{ jpgExists = 1; }
if (fs.existsSync(lineTit.replace(".*", ".pdf").split("\"").join("")))
{ pdfExists = 1; }
if (fs.existsSync(lineTit.replace(".*", ".png").split("\"").join("")))
{ pngExists = 1; }
if (fs.existsSync(lineTit.replace(".*", ".txt").split("\"").join("")))
{ txtExists = 1; }
if ((toJpg == 1 && jpgExists == 0) || (toPdf == 1 && pdfExists == 0) ||
(toPng == 1 && pngExists == 0) || (toTxt == 1 && txtExists == 0))
{
if (typeof customUserAgent !== 'undefined' && customUserAgent)
{ await page.setUserAgent(customUserAgent); }
await page.setViewport({ width: shotWidth,
height: shotHeight,
deviceScaleFactor: devicePixelRatio });
await page.goto(lineUrl, { waitUntil: 'networkidle2', timeout: pageLoadTimeout });
await sleep(delayBeforeScreening);
if (toJpg == 1 && jpgExists == 0 && subLineRes[1] == "full")
{
await page.screenshot({ path: lineTit.replace(".*", "") + '.jpg',
fullPage: true });
}
if (toJpg == 1 && jpgExists == 0 && subLineRes[1] != "full")
{
await page.screenshot({ path: lineTit.replace(".*", "") + '.jpg' });
}
if (toPng == 1 && pngExists == 0 && subLineRes[1] == "full")
{
await page.screenshot({ path: lineTit.replace(".*", "") + '.png',
fullPage: true });
}
if (toPng == 1 && pngExists == 0 && subLineRes[1] != "full")
{
await page.screenshot({ path: lineTit.replace(".*", "") + '.png' });
}
if (toTxt == 1 && txtExists == 0)
{
let shotText = await page.$eval('*', shotAll => shotAll.innerText);
fs.writeFile(lineTit.replace(".*", "") + '.txt', shotText, 'utf8',
(err) => { if (err) { throw err; } });
}
if (toPdf == 1 && pdfExists == 0)
{
if (lineExt.includes('Lpdf')) { pdfOrient = true; }
else { pdfOrient = false; }
await page.pdf({
path: lineTit.replace(".*", "") + '.pdf',
headerTemplate: '',
footerTemplate: '',
printBackground: true,
landscape: pdfOrient,
format: 'A4',
margin: { left: '2cm', top: '1cm', right: '1cm', bottom: '2cm' } });
}
}
}
}
catch (err)
{
notProcessed.push(list[line]);
}
}
fs.writeFile(listOfNotProcessed, notProcessed.join("\n"), 'utf8',
(err) => { if (err) { throw err; } });
await browser.close();
})()