@@ -6,12 +6,13 @@ import type { FastifyBaseLogger } from 'fastify';
6
6
import jsBeautify from 'js-beautify' ;
7
7
import type { Browser , JSHandle , Page , Response } from 'playwright' ;
8
8
9
+ import type { WebPageContext } from './web_page_context.js' ;
9
10
import { createObjectHash } from '../../../utilities/index.js' ;
10
11
import type { ApiResult } from '../../api_result.js' ;
11
12
import type { ApiRouteParams } from '../../api_route_params.js' ;
12
13
import { Diagnostics } from '../../diagnostics.js' ;
13
14
import { DEFAULT_DELAY_MS , DEFAULT_TIMEOUT_MS } from '../constants.js' ;
14
- import { type FetchedResource , FetchInterceptor } from '../fetch_interceptor.js' ;
15
+ import { FetchInterceptor } from '../fetch_interceptor.js' ;
15
16
import type { SecutilsWindow } from '../index.js' ;
16
17
17
18
// Maximum size of the content in bytes (200KB).
@@ -169,9 +170,7 @@ async function getContent(
169
170
if ( scripts ?. extractContent ) {
170
171
log . debug ( `[${ url } ] Adding "extractContent" function: ${ scripts . extractContent } .` ) ;
171
172
await page . addInitScript ( {
172
- content : `self.__secutils = { async extractContent(previousContent, externalResources, responseHeaders) {
173
- ${ scripts . extractContent } }
174
- }` ,
173
+ content : `self.__secutils = { async extractContent(context) { ${ scripts . extractContent } } };` ,
175
174
} ) ;
176
175
}
177
176
@@ -221,7 +220,11 @@ async function getContent(
221
220
const externalResources = await fetchInterceptor . stop ( ) ;
222
221
extractedContent = jsonStableStringify (
223
222
scripts ?. extractContent
224
- ? await extractContent ( page , previousContent , externalResources , ( await response ?. allHeaders ( ) ) ?? { } )
223
+ ? await extractContent ( page , {
224
+ previous : previousContent ,
225
+ externalResources,
226
+ responseHeaders : ( await response ?. allHeaders ( ) ) ?? { } ,
227
+ } )
225
228
: jsBeautify . html_beautify ( await page . content ( ) ) ,
226
229
) ;
227
230
} catch ( err ) {
@@ -256,15 +259,10 @@ async function getContent(
256
259
return { type : 'success' , data : { timestamp, content : extractedContent } } ;
257
260
}
258
261
259
- async function extractContent (
260
- page : Page ,
261
- previousContent : string | undefined ,
262
- externalResources : FetchedResource [ ] ,
263
- responseHeaders : Record < string , string > ,
264
- ) : Promise < unknown > {
262
+ async function extractContent ( page : Page , context : WebPageContext < string > ) : Promise < unknown > {
265
263
const targetWindow = await page . evaluateHandle < Window > ( 'window' ) ;
266
264
return await page . evaluate (
267
- async ( [ targetWindow , previousContent , externalResources , responseHeaders ] ) => {
265
+ async ( [ targetWindow , context ] ) => {
268
266
const extractContent = targetWindow . __secutils ?. extractContent ;
269
267
if ( extractContent && typeof extractContent !== 'function' ) {
270
268
console . error ( `[browser] Invalid "extractContent" function: ${ typeof extractContent } ` ) ;
@@ -274,11 +272,10 @@ async function extractContent(
274
272
275
273
try {
276
274
return typeof extractContent === 'function'
277
- ? ( await extractContent (
278
- previousContent !== undefined ? JSON . parse ( previousContent ) : previousContent ,
279
- externalResources ,
280
- responseHeaders ,
281
- ) ) ?? null
275
+ ? ( await extractContent ( {
276
+ ...context ,
277
+ previous : context . previous !== undefined ? JSON . parse ( context . previous ) : context . previous ,
278
+ } ) ) ?? null
282
279
: null ;
283
280
} catch ( err : unknown ) {
284
281
console . error ( `[browser] Content extractor script has thrown an exception: ${ ( err as Error ) ?. message ?? err } .` ) ;
@@ -287,6 +284,6 @@ async function extractContent(
287
284
throw new Error ( `Content extractor script has thrown an exception: ${ ( err as Error ) ?. message ?? err } .` ) ;
288
285
}
289
286
} ,
290
- [ targetWindow as JSHandle < SecutilsWindow > , previousContent , externalResources , responseHeaders ] as const ,
287
+ [ targetWindow as JSHandle < SecutilsWindow > , context ] as const ,
291
288
) ;
292
289
}
0 commit comments