forked from FogosPT/fogosapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
screenshot-script.js
70 lines (59 loc) · 2.41 KB
/
screenshot-script.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
const CDP = require('chrome-remote-interface');
const argv = require('minimist')(process.argv.slice(2));
const fs = require('fs');
const targetURL = argv.url || 'https://fogos.pt';
const viewport = [argv.width, argv.height];
const screenshotDelay = 6000; // ms
const fullPage = argv.fullPage || false;
const ip = argv.ip;
var opt = {
host: 'fogos.chrome'
}
CDP(opt, async function(client){
const {DOM, Emulation, Network, Page, Runtime} = client;
await Page.enable();
await DOM.enable();
await Network.enable();
var device = {
width: viewport[0],
height: viewport[1],
deviceScaleFactor: 0,
mobile: false,
fitWindow: false
};
await Emulation.setDeviceMetricsOverride(device);
await Emulation.setVisibleSize({width: viewport[0], height: viewport[1]});
await Page.navigate({url: targetURL});
await Network.setCookie({
name: "CookieConsent",
value: "{stamp:'m+a2sHQeOOuoPJRBktiiVf5mOGWDtiqvOKiLgCLNxxLwBBxXgfbaWQ=='%2Cnecessary:true%2Cpreferences:true%2Cstatistics:true%2Cmarketing:true%2Cver:1}",
domain: ".fogos.pt"
})
Page.loadEventFired(async() => {
if (fullPage) {
const {root: {nodeId: documentNodeId}} = await DOM.getDocument();
const {nodeId: bodyNodeId} = await DOM.querySelector({
selector: 'body',
nodeId: documentNodeId,
});
const {model: {height}} = await DOM.getBoxModel({nodeId: bodyNodeId});
await Emulation.setVisibleSize({width: device.width, height: height});
await Emulation.setDeviceMetricsOverride({width: device.width, height:height, screenWidth: device.width, screenHeight: height, deviceScaleFactor: 1, fitWindow: false, mobile: false});
await Emulation.setPageScaleFactor({pageScaleFactor:1});
}
});
setTimeout(async function() {
const screenshot = await Page.captureScreenshot({format: "png", fromSurface: true});
const buffer = new Buffer(screenshot.data, 'base64');
fs.writeFile( '/var/www/html/public/screenshots/' +argv.name+'.png', buffer, 'base64', function(err) {
if (err) {
console.error(err);
} else {
console.log('Screenshot saved');
}
});
client.close();
}, screenshotDelay);
}).on('error', err => {
console.error('Cannot connect to browser:', err);
});