2
2
import process from 'node:process' ;
3
3
import fs from 'node:fs/promises' ;
4
4
import path from 'node:path' ;
5
+ import { setTimeout } from 'node:timers/promises' ;
5
6
import fileUrl from 'file-url' ;
6
7
import puppeteer , { KnownDevices } from 'puppeteer' ;
7
8
import toughCookie from 'tough-cookie' ;
@@ -111,7 +112,15 @@ const disableAnimations = () => {
111
112
112
113
const getBoundingClientRect = element => {
113
114
const { top, left, height, width, x, y} = element . getBoundingClientRect ( ) ;
114
- return { top, left, height, width, x, y} ;
115
+
116
+ return {
117
+ top,
118
+ left,
119
+ height,
120
+ width,
121
+ x,
122
+ y,
123
+ } ;
115
124
} ;
116
125
117
126
const parseCookie = ( url , cookie ) => {
@@ -130,9 +139,7 @@ const parseCookie = (url, cookie) => {
130
139
returnValue . name = returnValue . key ;
131
140
delete returnValue . key ;
132
141
133
- if ( returnValue . expires ) {
134
- returnValue . expires = Math . floor ( new Date ( returnValue . expires ) / 1000 ) ;
135
- }
142
+ returnValue . expires &&= Math . floor ( new Date ( returnValue . expires ) / 1000 ) ;
136
143
137
144
return returnValue ;
138
145
} ;
@@ -312,7 +319,7 @@ const internalCaptureWebsiteCore = async (input, options, page, browser) => {
312
319
await page . click ( options . clickElement ) ;
313
320
}
314
321
315
- const getInjectKey = ( ext , value ) => isUrl ( value ) ? 'url' : ( value . endsWith ( `.${ ext } ` ) ? 'path' : 'content' ) ;
322
+ const getInjectKey = ( extension , value ) => isUrl ( value ) ? 'url' : ( value . endsWith ( `.${ extension } ` ) ? 'path' : 'content' ) ;
316
323
317
324
if ( ! options . isJavaScriptEnabled ) {
318
325
// Enable JavaScript again for `modules` and `scripts`.
@@ -357,7 +364,7 @@ const internalCaptureWebsiteCore = async (input, options, page, browser) => {
357
364
}
358
365
359
366
if ( options . delay ) {
360
- await page . waitForTimeout ( options . delay * 1000 ) ;
367
+ await setTimeout ( options . delay * 1000 ) ;
361
368
}
362
369
363
370
if ( options . element ) {
@@ -405,23 +412,27 @@ const internalCaptureWebsiteCore = async (input, options, page, browser) => {
405
412
}
406
413
407
414
if ( options . inset && ! screenshotOptions . fullPage ) {
408
- const inset = { top : 0 , right : 0 , bottom : 0 , left : 0 } ;
415
+ const inset = {
416
+ top : 0 ,
417
+ right : 0 ,
418
+ bottom : 0 ,
419
+ left : 0 ,
420
+ } ;
421
+
409
422
for ( const key of Object . keys ( inset ) ) {
410
- inset [ key ] = typeof options . inset === 'number' ? options . inset : options . inset [ key ] || 0 ;
423
+ inset [ key ] = typeof options . inset === 'number' ? options . inset : ( options . inset [ key ] ?? 0 ) ;
411
424
}
412
425
413
426
let clipOptions = screenshotOptions . clip ;
414
427
415
- if ( ! clipOptions ) {
416
- clipOptions = await page . evaluate ( ( ) => ( {
417
- x : 0 ,
418
- y : 0 ,
419
- /* eslint-disable no-undef */
420
- height : window . innerHeight ,
421
- width : window . innerWidth ,
422
- /* eslint-enable no-undef */
423
- } ) ) ;
424
- }
428
+ clipOptions ||= await page . evaluate ( ( ) => ( {
429
+ x : 0 ,
430
+ y : 0 ,
431
+ /* eslint-disable no-undef */
432
+ height : window . innerHeight ,
433
+ width : window . innerWidth ,
434
+ /* eslint-enable no-undef */
435
+ } ) ) ;
425
436
426
437
const x = clipOptions . x + inset . left ;
427
438
const y = clipOptions . y + inset . top ;
@@ -432,7 +443,12 @@ const internalCaptureWebsiteCore = async (input, options, page, browser) => {
432
443
throw new Error ( 'When using the `clip` option, the width or height of the screenshot cannot be equal to 0.' ) ;
433
444
}
434
445
435
- screenshotOptions . clip = { x, y, width, height} ;
446
+ screenshotOptions . clip = {
447
+ x,
448
+ y,
449
+ width,
450
+ height,
451
+ } ;
436
452
}
437
453
438
454
const buffer = await page . screenshot ( screenshotOptions ) ;
0 commit comments