-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
browser.js
91 lines (78 loc) · 2.95 KB
/
browser.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
const puppeteer = require('puppeteer-core');
const config = require('./config');
const disableTransitionDelayCSS = `*,::after,::before{transition-delay:0s!important;transition-duration:0s!important;animation-delay:-.1ms!important;animation-duration:0s!important;animation-play-state:paused!important;caret-color:transparent!important;color-adjust:exact!important}`;
/* Set browser for desktop */
async function getScreenshot(url, type, quality, fullPage) {
const browser = await puppeteer.connect({
browserWSEndpoint: config.browserWSEndpoint,
});
const page = await browser.newPage();
await page.emulate({
userAgent:
'Mozilla/5.0 (Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36 Statically-Screenshot/1.0 (+https://statically.io/screenshot/)',
viewport: {
width: 1280,
height: 960,
deviceScaleFactor: 1,
isMobile: false,
hasTouch: false,
isLandscape: true,
},
});
await page.goto(url /*{ waitUntil: 'networkidle0' }*/);
await page.addStyleTag({ content: disableTransitionDelayCSS });
const file = await page.screenshot({ type, quality, fullPage });
await browser.close();
console.log('HTTP ' + url);
return file;
}
/* Set browser for mobile */
async function getScreenshotMobile(url, type, quality, fullPage) {
const browser = await puppeteer.connect({
browserWSEndpoint: config.browserWSEndpoint,
});
const page = await browser.newPage();
await page.emulate({
userAgent:
'Mozilla/5.0 (Linux Android 5.0 SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Mobile Safari/537.36 Statically-Screenshot-Mobile/1.0 (+https://statically.io/screenshot/)',
viewport: {
width: 360,
height: 640,
deviceScaleFactor: 1,
isMobile: true,
hasTouch: true,
isLandscape: false,
},
});
await page.goto(url /*{ waitUntil: 'networkidle0' }*/);
await page.addStyleTag({ content: disableTransitionDelayCSS });
console.log('HTTP ' + url);
const file = await page.screenshot({ type, quality, fullPage });
await browser.close();
return file;
}
/* Set browser for PDF */
async function generatePdf(url) {
const browser = await puppeteer.connect({
browserWSEndpoint: config.browserWSEndpoint,
});
const page = await browser.newPage();
await page.emulate({
userAgent:
'Mozilla/5.0 (Linux Android 5.0 SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Mobile Safari/537.36 Statically-Screenshot-PDF/1.0 (+https://statically.io/screenshot/)',
viewport: {
width: 360,
height: 640,
deviceScaleFactor: 1,
isMobile: true,
hasTouch: true,
isLandscape: false,
},
});
await page.goto(url /*{ waitUntil: 'networkidle0' }*/);
const file = await page.pdf({ format: 'A4' });
await browser.close();
console.log('HTTP ' + url);
return file;
}
module.exports = { getScreenshot, getScreenshotMobile, generatePdf };