@@ -108,12 +108,7 @@ public async Task<IBrowserContext> InitContext(string ctxId, BrowserActionArgs a
108108 return _contexts [ ctxId ] ;
109109 }
110110
111- public async Task < IPage > NewPage ( MessageInfo message ,
112- bool enableResponseCallback = false ,
113- bool responseInMemory = false ,
114- List < WebPageResponseData > ? responseContainer = null ,
115- string [ ] ? excludeResponseUrls = null ,
116- string [ ] ? includeResponseUrls = null )
111+ public async Task < IPage > NewPage ( MessageInfo message , PageActionArgs args )
117112 {
118113 var context = await GetContext ( message . ContextId ) ;
119114 var page = await context . NewPageAsync ( ) ;
@@ -123,68 +118,73 @@ public async Task<IPage> NewPage(MessageInfo message,
123118 var js = @"Object.defineProperties(navigator, {webdriver:{get:()=>false}});" ;
124119 await page . AddInitScriptAsync ( js ) ;
125120
126- if ( ! enableResponseCallback )
121+ if ( ! args . EnableResponseCallback )
127122 {
128123 return page ;
129124 }
130125
131126 page . Response += async ( sender , e ) =>
132127 {
133- if ( e . Status != 204 &&
134- e . Headers . ContainsKey ( "content-type" ) &&
135- ( e . Request . ResourceType == "fetch" || e . Request . ResourceType == "xhr" ) &&
136- ( excludeResponseUrls == null || ! excludeResponseUrls . Any ( url => e . Url . ToLower ( ) . Contains ( url ) ) ) &&
137- ( includeResponseUrls == null || includeResponseUrls . Any ( url => e . Url . ToLower ( ) . Contains ( url ) ) ) )
138- {
139- Serilog . Log . Information ( $ "{ e . Request . Method } : { e . Url } ") ;
140-
141- try
142- {
143- var result = new WebPageResponseData
144- {
145- Url = e . Url . ToLower ( ) ,
146- PostData = e . Request ? . PostData ?? string . Empty ,
147- ResponseInMemory = responseInMemory
148- } ;
128+ await HandleFetchResponse ( e , message , args ) ;
129+ } ;
149130
150- if ( e . Headers [ "content-type" ] . Contains ( "application/json" ) )
151- {
152- if ( e . Status == 200 && e . Ok )
153- {
154- var json = await e . JsonAsync ( ) ;
155- result . ResponseData = JsonSerializer . Serialize ( json ) ;
156- }
157- }
158- else
159- {
160- var html = await e . TextAsync ( ) ;
161- result . ResponseData = html ;
162- }
131+ return page ;
132+ }
163133
164- if ( responseContainer != null && responseInMemory )
165- {
166- responseContainer . Add ( result ) ;
167- }
134+ public async Task HandleFetchResponse ( IResponse response , MessageInfo message , PageActionArgs args )
135+ {
136+ if ( response . Status != 204 &&
137+ response . Headers . ContainsKey ( "content-type" ) &&
138+ ( response . Request . ResourceType == "fetch" || response . Request . ResourceType == "xhr" ) &&
139+ ( args . ExcludeResponseUrls == null || ! args . ExcludeResponseUrls . Any ( url => response . Url . ToLower ( ) . Contains ( url ) ) ) &&
140+ ( args . IncludeResponseUrls == null || args . IncludeResponseUrls . Any ( url => response . Url . ToLower ( ) . Contains ( url ) ) ) )
141+ {
142+ Serilog . Log . Information ( $ "{ response . Request . Method } : { response . Url } ") ;
143+
144+ try
145+ {
146+ var result = new WebPageResponseData
147+ {
148+ Url = response . Url . ToLower ( ) ,
149+ PostData = response . Request ? . PostData ?? string . Empty ,
150+ ResponseInMemory = args . ResponseInMemory
151+ } ;
168152
169- Serilog . Log . Warning ( $ "Response status: { e . Status } { e . StatusText } , OK: { e . Ok } " ) ;
170- var webPageResponseHooks = _services . GetServices < IWebPageResponseHook > ( ) ;
171- foreach ( var hook in webPageResponseHooks )
153+ if ( response . Headers [ "content-type" ] . Contains ( "application/json" ) )
154+ {
155+ if ( response . Status == 200 && response . Ok )
172156 {
173- hook . OnDataFetched ( message , result ) ;
157+ var json = await response . JsonAsync ( ) ;
158+ result . ResponseData = JsonSerializer . Serialize ( json ) ;
174159 }
175160 }
176- catch ( ObjectDisposedException ex )
161+ else
177162 {
178- Serilog . Log . Information ( ex . Message ) ;
163+ var html = await response . TextAsync ( ) ;
164+ result . ResponseData = html ;
179165 }
180- catch ( Exception ex )
166+
167+ if ( args . ResponseContainer != null && args . ResponseInMemory )
181168 {
182- Serilog . Log . Error ( $ " { e . Url } \r \n " + ex . ToString ( ) ) ;
169+ args . ResponseContainer . Add ( result ) ;
183170 }
184- }
185- } ;
186171
187- return page ;
172+ Serilog . Log . Warning ( $ "Response status: { response . Status } { response . StatusText } , OK: { response . Ok } ") ;
173+ var webPageResponseHooks = _services . GetServices < IWebPageResponseHook > ( ) ;
174+ foreach ( var hook in webPageResponseHooks )
175+ {
176+ hook . OnDataFetched ( message , result ) ;
177+ }
178+ }
179+ catch ( ObjectDisposedException ex )
180+ {
181+ Serilog . Log . Information ( ex . Message ) ;
182+ }
183+ catch ( Exception ex )
184+ {
185+ Serilog . Log . Error ( $ "{ response . Url } \r \n " + ex . ToString ( ) ) ;
186+ }
187+ }
188188 }
189189
190190 /// <summary>
0 commit comments