-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathscreengrab.js
228 lines (190 loc) · 6.1 KB
/
screengrab.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
const puppeteer = require('puppeteer');
const NAV_TIMEOUT = 120000;
const sleep = (ms) => {
return new Promise(resolve => setTimeout(resolve, ms))
}
const render = async function(args) {
const { url, out, delay, css, style, width, height } = args;
console.log("hey", args);
let valid = true;
const browser = await puppeteer.launch({args: [
"--disable-gpu",
"--disable-dev-shm-usage",
'--no-sandbox',
'--disable-setuid-sandbox'
]})
const page = await browser.newPage()
console.log(page);
if (width && height) {
await page.setViewport({ width, height })
}
try {
console.log(`visiting ${url}`);
await page.goto(url, {
waitUntil : 'networkidle0',
timeout: NAV_TIMEOUT
});
}
catch(err) {
console.log(err);
}
console.log('here');
if (css || style) {
console.log('applying style');
await page.evaluate((css, style) => {
if (css) {
const head = document.head
const link = document.createElement('link')
link.href = css
link.rel = 'stylesheet'
head.appendChild(link)
}
if (style) {
document.body.setAttribute('style', style)
}
}, css, style)
}
if (delay) {
console.log(`sleep for ${delay}`);
await sleep(delay)
}
var text = await page.evaluate(() => document.querySelector('body').textContent);
if ( text === null || text === undefined || text === "" ) {
console.log("no text!!!!");
valid = false;
}
if ( text.toLowerCase().lastIndexOf("504 Gateway") !== -1 ) {
console.log("timeout!!!");
valid = false;
}
if ( valid === true ) {
console.log('valid page, lets cleanup');
var l = await page.$('#wm-tb-close');
if ( l !== undefined && l !== null ) {
l.click();
}
await sleep(2500);
await page.evaluate(async() => {
var f = document.querySelector('#donato');
if ( f !== undefined && f !== null ) {
f.parentNode.removeChild(f);
}
});
await page.evaluate(async() => {
var f = document.querySelector('#wm-ipp-base');
if ( f !== undefined && f !== null ) {
f.parentNode.removeChild(f);
}
});
await page.evaluate(async() => {
// WM sets 'min-width:800px !important;' on the body, let's try and remove it
// http://stackoverflow.com/questions/33135966/phantomjs-remove-script-tags-from-html-snapshot-except-jsonld
Array.prototype.slice.call(document.getElementsByTagName("style")).filter(function(style) {
style.innerHTML.lastIndexOf("min-width:800px !important;") !== -1
}).forEach(function(style) {
console.log("remove", style);
style.parentNode.removeChild(style);
});
// try and close WM js on any frames
var list = Array.prototype.slice.call( document.getElementsByTagName("frame") );
list.forEach(function(f) {
if ( f && f.contentWindow && f.contentWindow.__wm ) {
f.contentWindow.__wm.h();
}
if ( f && f.contentWindow && f.contentWindow.document ) {
Array.prototype.slice.call(f.contentWindow.document.getElementsByTagName("style")).filter(function(style) {
style.innerHTML.lastIndexOf("min-width:800px !important;") !== -1
}).forEach(function(style) {
style.parentNode.removeChild(style);
});
}
});
//
// inject a function to iterate through all text nodes in the body, so we can clean them up a bit
//
function nativeSelector() {
var walker = document.createTreeWalker(
document.body,
NodeFilter.SHOW_TEXT,
null,
false
);
var node;
var textNodes = [];
// this is silly because there's no logic here, so we could just
// pass in a function to run against the node, but leaving for now
while((node = walker.nextNode())) {
textNodes.push(node);
}
return textNodes;
}
var textnodes = nativeSelector(),
_nv;
for (var i = 0, len = textnodes.length; i<len; i++){
_nv = textnodes[i].textContent;
// attempt to strip email addresses and phone numbers from output
_NV = _nv.replace(/[A-Za-z0-9_\-\.]+\@/g, '░░░░░@').
replace(/[\(\+]?\b[ 0-9\-\(\)+^'^"^<^>]{10,14}\b/g, function(m) { return m.replace(/[0-9]/g, "x"); });
textnodes[i].textContent = _nv;
}
});
}
if ( valid ) {
if (out === '-') {
const screenshot = await page.screenshot()
console.log(screenshot.toString('base64'))
}
else {
console.log("write to " + out);
await page.screenshot({path: out})
}
}
else {
}
console.log("BROWSER CLOSED");
browser.close();
}
module.exports.render = render;
//
// you can run this script directly to do some debugging
// ie:
// node ./screengrab.js --url https://web.archive.org/web/19961221011029/http://www.insurance-life.com/ --width 800 --height 600 -delay 0 --out foo.png
//
if (require.main === module) {
(async () => {
const argv = require('yargs')
.example('$0 --url=https://example.com')
.option('url')
.option('width', {
describe: 'Width of viewport'
})
.option('height', {
describe: 'Height of viewport'
})
.option('out', {
default: '-',
describe: 'File path to save. If `-` specified, outputs to console in base64-encoded'
})
.option('delay', {
default: 1000,
describe: 'Delay to save screenshot after loading CSS. Milliseconds'
})
.option('css', {
describe: 'Additional CSS URL to load'
})
.option('style', {
describe: 'Additional style to apply to body'
})
.demandOption(['url'])
.argv
await render({
url: argv.url,
out: argv.out,
delay: argv.delay,
css: argv.css,
style: argv.style,
width: argv.width,
height: argv.height
});
})();
}