@browserless/screenshot 10.1.13 → 10.1.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +6 -6
- package/src/index.js +70 -72
- package/src/pretty/index.js +2 -3
- package/src/time-span.js +1 -0
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@browserless/screenshot",
|
|
3
3
|
"description": "Take a clean screenshot of any website",
|
|
4
4
|
"homepage": "https://browserless.js.org/#/?id=screenshoturl-options",
|
|
5
|
-
"version": "10.1.
|
|
5
|
+
"version": "10.1.15",
|
|
6
6
|
"main": "src/index.js",
|
|
7
7
|
"author": {
|
|
8
8
|
"email": "hello@microlink.io",
|
|
@@ -28,7 +28,8 @@
|
|
|
28
28
|
"screenshot"
|
|
29
29
|
],
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@browserless/goto": "^10.1.
|
|
31
|
+
"@browserless/goto": "^10.1.15",
|
|
32
|
+
"@kikobeats/time-span": "~1.0.2",
|
|
32
33
|
"debug-logfmt": "~1.0.4",
|
|
33
34
|
"got": "~11.8.6",
|
|
34
35
|
"is-html-content": "~1.0.0",
|
|
@@ -40,11 +41,10 @@
|
|
|
40
41
|
"pretty-ms": "~7.0.1",
|
|
41
42
|
"prism-themes": "~1.9.0",
|
|
42
43
|
"sharp": "~0.32.5",
|
|
43
|
-
"svg-gradient": "~1.0.3"
|
|
44
|
-
"time-span": "~4.0.0"
|
|
44
|
+
"svg-gradient": "~1.0.3"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
|
-
"@browserless/test": "^10.1.
|
|
47
|
+
"@browserless/test": "^10.1.15",
|
|
48
48
|
"ava": "latest",
|
|
49
49
|
"cheerio": "latest"
|
|
50
50
|
},
|
|
@@ -60,7 +60,7 @@
|
|
|
60
60
|
"timeout": "2m",
|
|
61
61
|
"workerThreads": false
|
|
62
62
|
},
|
|
63
|
-
"gitHead": "
|
|
63
|
+
"gitHead": "314f90097a623f50a807d403d99313c6f7eb3f54",
|
|
64
64
|
"scripts": {
|
|
65
65
|
"coverage": "exit 0",
|
|
66
66
|
"test": "ava"
|
package/src/index.js
CHANGED
|
@@ -2,12 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
const debug = require('debug-logfmt')('browserless:screenshot')
|
|
4
4
|
const createGoto = require('@browserless/goto')
|
|
5
|
-
const prettyMs = require('pretty-ms')
|
|
6
|
-
const timeSpan = require('time-span')
|
|
7
5
|
const pReflect = require('p-reflect')
|
|
8
6
|
|
|
9
7
|
const isWhiteScreenshot = require('./is-white-screenshot')
|
|
10
8
|
const waitForPrism = require('./pretty')
|
|
9
|
+
const timeSpan = require('./time-span')
|
|
11
10
|
const overlay = require('./overlay')
|
|
12
11
|
|
|
13
12
|
const getBoundingClientRect = element => {
|
|
@@ -19,18 +18,16 @@ const waitForImagesOnViewport = page =>
|
|
|
19
18
|
page.$$eval('img[src]:not([aria-hidden="true"])', elements =>
|
|
20
19
|
Promise.all(
|
|
21
20
|
elements
|
|
22
|
-
.filter(
|
|
23
|
-
el
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
)
|
|
21
|
+
.filter(el => {
|
|
22
|
+
if (el.naturalHeight === 0 || el.naturalWeight === 0) return false
|
|
23
|
+
const { top, left, bottom, right } = el.getBoundingClientRect()
|
|
24
|
+
return (
|
|
25
|
+
top >= 0 &&
|
|
26
|
+
left >= 0 &&
|
|
27
|
+
bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
|
|
28
|
+
right <= (window.innerWidth || document.documentElement.clientWidth)
|
|
29
|
+
)
|
|
30
|
+
})
|
|
34
31
|
.map(el => el.decode())
|
|
35
32
|
)
|
|
36
33
|
)
|
|
@@ -51,75 +48,76 @@ const waitForElement = async (page, element) => {
|
|
|
51
48
|
module.exports = ({ goto, ...gotoOpts }) => {
|
|
52
49
|
goto = goto || createGoto(gotoOpts)
|
|
53
50
|
|
|
54
|
-
return page =>
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
51
|
+
return page =>
|
|
52
|
+
async (
|
|
53
|
+
url,
|
|
54
|
+
{
|
|
55
|
+
element,
|
|
56
|
+
codeScheme = 'atom-dark',
|
|
57
|
+
overlay: overlayOpts = {},
|
|
58
|
+
waitUntil = 'auto',
|
|
59
|
+
...opts
|
|
60
|
+
} = {}
|
|
61
|
+
) => {
|
|
62
|
+
let screenshot
|
|
63
|
+
let response
|
|
66
64
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
65
|
+
const beforeScreenshot = response => {
|
|
66
|
+
const timeout = goto.timeouts.action(goto.timeouts.base(opts.timeout))
|
|
67
|
+
return Promise.all(
|
|
68
|
+
[
|
|
69
|
+
{
|
|
70
|
+
fn: () => page.evaluate('document.fonts.ready'),
|
|
71
|
+
debug: 'beforeScreenshot:fontsReady'
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
fn: () => waitForPrism(page, response, { codeScheme, ...opts }),
|
|
75
|
+
debug: 'beforeScreenshot:waitForPrism'
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
fn: () => waitForImagesOnViewport(page),
|
|
79
|
+
debug: 'beforeScreenshot:waitForImagesOnViewport'
|
|
80
|
+
}
|
|
81
|
+
].map(({ fn, ...opts }) => goto.run({ fn: fn(), ...opts, timeout }))
|
|
82
|
+
)
|
|
83
|
+
}
|
|
86
84
|
|
|
87
|
-
|
|
88
|
-
screenshot = await page.screenshot(opts)
|
|
89
|
-
const isWhite = await isWhiteScreenshot(screenshot)
|
|
90
|
-
if (isWhite) {
|
|
91
|
-
await goto.waitUntilAuto(page, opts)
|
|
85
|
+
const takeScreenshot = async opts => {
|
|
92
86
|
screenshot = await page.screenshot(opts)
|
|
87
|
+
const isWhite = await isWhiteScreenshot(screenshot)
|
|
88
|
+
if (isWhite) {
|
|
89
|
+
await goto.waitUntilAuto(page, opts)
|
|
90
|
+
screenshot = await page.screenshot(opts)
|
|
91
|
+
}
|
|
92
|
+
return { isWhite }
|
|
93
93
|
}
|
|
94
|
-
return { isWhite }
|
|
95
|
-
}
|
|
96
94
|
|
|
97
|
-
|
|
95
|
+
page.on('dialog', dialog => pReflect(dialog.dismiss()))
|
|
98
96
|
|
|
99
|
-
|
|
97
|
+
const timeScreenshot = timeSpan()
|
|
100
98
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
const [screenshotOpts] = await Promise.all([
|
|
104
|
-
waitForElement(page, element),
|
|
105
|
-
beforeScreenshot(response)
|
|
106
|
-
])
|
|
107
|
-
screenshot = await page.screenshot({ ...opts, ...screenshotOpts })
|
|
108
|
-
debug('screenshot', { waitUntil, duration: prettyMs(timeScreenshot()) })
|
|
109
|
-
} else {
|
|
110
|
-
;({ response } = await goto(page, { ...opts, url, waitUntil, waitUntilAuto }))
|
|
111
|
-
async function waitUntilAuto (page, { response }) {
|
|
99
|
+
if (waitUntil !== 'auto') {
|
|
100
|
+
;({ response } = await goto(page, { ...opts, url, waitUntil }))
|
|
112
101
|
const [screenshotOpts] = await Promise.all([
|
|
113
102
|
waitForElement(page, element),
|
|
114
103
|
beforeScreenshot(response)
|
|
115
104
|
])
|
|
116
|
-
|
|
117
|
-
debug('screenshot', { waitUntil,
|
|
105
|
+
screenshot = await page.screenshot({ ...opts, ...screenshotOpts })
|
|
106
|
+
debug('screenshot', { waitUntil, duration: timeScreenshot() })
|
|
107
|
+
} else {
|
|
108
|
+
;({ response } = await goto(page, { ...opts, url, waitUntil, waitUntilAuto }))
|
|
109
|
+
async function waitUntilAuto (page, { response }) {
|
|
110
|
+
const [screenshotOpts] = await Promise.all([
|
|
111
|
+
waitForElement(page, element),
|
|
112
|
+
beforeScreenshot(response)
|
|
113
|
+
])
|
|
114
|
+
const { isWhite } = await takeScreenshot({ ...opts, ...screenshotOpts })
|
|
115
|
+
debug('screenshot', { waitUntil, isWhite, duration: timeScreenshot() })
|
|
116
|
+
}
|
|
118
117
|
}
|
|
119
|
-
}
|
|
120
118
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
119
|
+
return Object.keys(overlayOpts).length === 0
|
|
120
|
+
? screenshot
|
|
121
|
+
: overlay(screenshot, { ...opts, ...overlayOpts, viewport: page.viewport() })
|
|
122
|
+
}
|
|
125
123
|
}
|
package/src/pretty/index.js
CHANGED
|
@@ -4,11 +4,10 @@ const debug = require('debug-logfmt')('browserless:screenshot')
|
|
|
4
4
|
const isHtmlContent = require('is-html-content')
|
|
5
5
|
const { readFile } = require('fs/promises')
|
|
6
6
|
const { getExtension } = require('mime')
|
|
7
|
-
const prettyMs = require('pretty-ms')
|
|
8
|
-
const timeSpan = require('time-span')
|
|
9
7
|
const path = require('path')
|
|
10
8
|
|
|
11
9
|
const getPrism = readFile(path.resolve(__dirname, 'prism.js'))
|
|
10
|
+
const timeSpan = require('../time-span')
|
|
12
11
|
const getTheme = require('./theme')
|
|
13
12
|
const getHtml = require('./html')
|
|
14
13
|
|
|
@@ -54,5 +53,5 @@ module.exports = async (page, response, { timeout, codeScheme, styles, scripts,
|
|
|
54
53
|
await page.setContent(html)
|
|
55
54
|
await inject(page, { timeout, modules, scripts, styles })
|
|
56
55
|
|
|
57
|
-
debug('pretty', { duration:
|
|
56
|
+
debug('pretty', { duration: timePretty() })
|
|
58
57
|
}
|
package/src/time-span.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('@kikobeats/time-span')({ format: require('pretty-ms') })
|