@@ -14,15 +14,6 @@ const { puppeteerArgs } = require("./puppeteer-constants");
14
14
* @returns {Promise<RunBrowserResult> }
15
15
*/
16
16
function runBrowser ( config ) {
17
- const options = {
18
- viewport : {
19
- width : 500 ,
20
- height : 500 ,
21
- } ,
22
- userAgent : "" ,
23
- ...config ,
24
- } ;
25
-
26
17
return new Promise ( ( resolve , reject ) => {
27
18
/**
28
19
* @type {import('puppeteer').Page }
@@ -44,35 +35,60 @@ function runBrowser(config) {
44
35
. then ( ( launchedBrowser ) => {
45
36
browser = launchedBrowser ;
46
37
47
- return browser . newPage ( ) ;
38
+ return runPage ( launchedBrowser , config ) ;
48
39
} )
49
40
. then ( ( newPage ) => {
50
41
page = newPage ;
51
- page . emulate ( options ) ;
52
-
53
- return page . setRequestInterception ( true ) ;
54
- } )
55
- . then ( ( ) => {
56
- page . on ( "request" , ( interceptedRequest ) => {
57
- if ( interceptedRequest . isInterceptResolutionHandled ( ) ) return ;
58
- if ( interceptedRequest . url ( ) . includes ( "favicon.ico" ) ) {
59
- interceptedRequest . respond ( {
60
- status : 200 ,
61
- contentType : "image/png" ,
62
- body : "Empty" ,
63
- } ) ;
64
- } else {
65
- interceptedRequest . continue (
66
- interceptedRequest . continueRequestOverrides ( ) ,
67
- 10 ,
68
- ) ;
69
- }
70
- } ) ;
71
42
72
43
resolve ( { page, browser } ) ;
73
44
} )
74
45
. catch ( reject ) ;
75
46
} ) ;
76
47
}
77
48
49
+ function runPage ( browser , config ) {
50
+ /**
51
+ * @type {import('puppeteer').Page }
52
+ */
53
+ let page ;
54
+
55
+ const options = {
56
+ viewport : {
57
+ width : 500 ,
58
+ height : 500 ,
59
+ } ,
60
+ userAgent : "" ,
61
+ ...config ,
62
+ } ;
63
+
64
+ return Promise . resolve ( )
65
+ . then ( ( ) => browser . newPage ( ) )
66
+ . then ( ( newPage ) => {
67
+ page = newPage ;
68
+ page . emulate ( options ) ;
69
+
70
+ return page . setRequestInterception ( true ) ;
71
+ } )
72
+ . then ( ( ) => {
73
+ page . on ( "request" , ( interceptedRequest ) => {
74
+ if ( interceptedRequest . isInterceptResolutionHandled ( ) ) return ;
75
+ if ( interceptedRequest . url ( ) . includes ( "favicon.ico" ) ) {
76
+ interceptedRequest . respond ( {
77
+ status : 200 ,
78
+ contentType : "image/png" ,
79
+ body : "Empty" ,
80
+ } ) ;
81
+ } else {
82
+ interceptedRequest . continue (
83
+ interceptedRequest . continueRequestOverrides ( ) ,
84
+ 10 ,
85
+ ) ;
86
+ }
87
+ } ) ;
88
+
89
+ return page ;
90
+ } ) ;
91
+ }
92
+
78
93
module . exports = runBrowser ;
94
+ module . exports . runPage = runPage ;
0 commit comments