@browserless/screenshot 10.1.13 → 10.1.14
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 +4 -4
- package/src/index.js +69 -70
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.14",
|
|
6
6
|
"main": "src/index.js",
|
|
7
7
|
"author": {
|
|
8
8
|
"email": "hello@microlink.io",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"screenshot"
|
|
29
29
|
],
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@browserless/goto": "^10.1.
|
|
31
|
+
"@browserless/goto": "^10.1.14",
|
|
32
32
|
"debug-logfmt": "~1.0.4",
|
|
33
33
|
"got": "~11.8.6",
|
|
34
34
|
"is-html-content": "~1.0.0",
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
"time-span": "~4.0.0"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
|
-
"@browserless/test": "^10.1.
|
|
47
|
+
"@browserless/test": "^10.1.14",
|
|
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": "bd55f5fbe604d1eb925f6d192f12889baf9099cb",
|
|
64
64
|
"scripts": {
|
|
65
65
|
"coverage": "exit 0",
|
|
66
66
|
"test": "ava"
|
package/src/index.js
CHANGED
|
@@ -19,18 +19,16 @@ const waitForImagesOnViewport = page =>
|
|
|
19
19
|
page.$$eval('img[src]:not([aria-hidden="true"])', elements =>
|
|
20
20
|
Promise.all(
|
|
21
21
|
elements
|
|
22
|
-
.filter(
|
|
23
|
-
el
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
)
|
|
22
|
+
.filter(el => {
|
|
23
|
+
if (el.naturalHeight === 0 || el.naturalWeight === 0) return false
|
|
24
|
+
const { top, left, bottom, right } = el.getBoundingClientRect()
|
|
25
|
+
return (
|
|
26
|
+
top >= 0 &&
|
|
27
|
+
left >= 0 &&
|
|
28
|
+
bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
|
|
29
|
+
right <= (window.innerWidth || document.documentElement.clientWidth)
|
|
30
|
+
)
|
|
31
|
+
})
|
|
34
32
|
.map(el => el.decode())
|
|
35
33
|
)
|
|
36
34
|
)
|
|
@@ -51,75 +49,76 @@ const waitForElement = async (page, element) => {
|
|
|
51
49
|
module.exports = ({ goto, ...gotoOpts }) => {
|
|
52
50
|
goto = goto || createGoto(gotoOpts)
|
|
53
51
|
|
|
54
|
-
return page =>
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
52
|
+
return page =>
|
|
53
|
+
async (
|
|
54
|
+
url,
|
|
55
|
+
{
|
|
56
|
+
element,
|
|
57
|
+
codeScheme = 'atom-dark',
|
|
58
|
+
overlay: overlayOpts = {},
|
|
59
|
+
waitUntil = 'auto',
|
|
60
|
+
...opts
|
|
61
|
+
} = {}
|
|
62
|
+
) => {
|
|
63
|
+
let screenshot
|
|
64
|
+
let response
|
|
66
65
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
66
|
+
const beforeScreenshot = response => {
|
|
67
|
+
const timeout = goto.timeouts.action(goto.timeouts.base(opts.timeout))
|
|
68
|
+
return Promise.all(
|
|
69
|
+
[
|
|
70
|
+
{
|
|
71
|
+
fn: () => page.evaluate('document.fonts.ready'),
|
|
72
|
+
debug: 'beforeScreenshot:fontsReady'
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
fn: () => waitForPrism(page, response, { codeScheme, ...opts }),
|
|
76
|
+
debug: 'beforeScreenshot:waitForPrism'
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
fn: () => waitForImagesOnViewport(page),
|
|
80
|
+
debug: 'beforeScreenshot:waitForImagesOnViewport'
|
|
81
|
+
}
|
|
82
|
+
].map(({ fn, ...opts }) => goto.run({ fn: fn(), ...opts, timeout }))
|
|
83
|
+
)
|
|
84
|
+
}
|
|
86
85
|
|
|
87
|
-
|
|
88
|
-
screenshot = await page.screenshot(opts)
|
|
89
|
-
const isWhite = await isWhiteScreenshot(screenshot)
|
|
90
|
-
if (isWhite) {
|
|
91
|
-
await goto.waitUntilAuto(page, opts)
|
|
86
|
+
const takeScreenshot = async opts => {
|
|
92
87
|
screenshot = await page.screenshot(opts)
|
|
88
|
+
const isWhite = await isWhiteScreenshot(screenshot)
|
|
89
|
+
if (isWhite) {
|
|
90
|
+
await goto.waitUntilAuto(page, opts)
|
|
91
|
+
screenshot = await page.screenshot(opts)
|
|
92
|
+
}
|
|
93
|
+
return { isWhite }
|
|
93
94
|
}
|
|
94
|
-
return { isWhite }
|
|
95
|
-
}
|
|
96
95
|
|
|
97
|
-
|
|
96
|
+
page.on('dialog', dialog => pReflect(dialog.dismiss()))
|
|
98
97
|
|
|
99
|
-
|
|
98
|
+
const timeScreenshot = timeSpan()
|
|
100
99
|
|
|
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 }) {
|
|
100
|
+
if (waitUntil !== 'auto') {
|
|
101
|
+
;({ response } = await goto(page, { ...opts, url, waitUntil }))
|
|
112
102
|
const [screenshotOpts] = await Promise.all([
|
|
113
103
|
waitForElement(page, element),
|
|
114
104
|
beforeScreenshot(response)
|
|
115
105
|
])
|
|
116
|
-
|
|
117
|
-
debug('screenshot', { waitUntil,
|
|
106
|
+
screenshot = await page.screenshot({ ...opts, ...screenshotOpts })
|
|
107
|
+
debug('screenshot', { waitUntil, duration: prettyMs(timeScreenshot()) })
|
|
108
|
+
} else {
|
|
109
|
+
;({ response } = await goto(page, { ...opts, url, waitUntil, waitUntilAuto }))
|
|
110
|
+
async function waitUntilAuto (page, { response }) {
|
|
111
|
+
const [screenshotOpts] = await Promise.all([
|
|
112
|
+
waitForElement(page, element),
|
|
113
|
+
beforeScreenshot(response)
|
|
114
|
+
])
|
|
115
|
+
const { isWhite } = await takeScreenshot({ ...opts, ...screenshotOpts })
|
|
116
|
+
debug('screenshot', { waitUntil, isWhite, duration: prettyMs(timeScreenshot()) })
|
|
117
|
+
}
|
|
118
118
|
}
|
|
119
|
-
}
|
|
120
119
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
120
|
+
return Object.keys(overlayOpts).length === 0
|
|
121
|
+
? screenshot
|
|
122
|
+
: overlay(screenshot, { ...opts, ...overlayOpts, viewport: page.viewport() })
|
|
123
|
+
}
|
|
125
124
|
}
|