@@ -6,12 +6,13 @@ import type { FastifyBaseLogger } from 'fastify';
66import jsBeautify from 'js-beautify' ;
77import type { Browser , JSHandle , Page , Response } from 'playwright' ;
88
9+ import type { WebPageContext } from './web_page_context.js' ;
910import { createObjectHash } from '../../../utilities/index.js' ;
1011import type { ApiResult } from '../../api_result.js' ;
1112import type { ApiRouteParams } from '../../api_route_params.js' ;
1213import { Diagnostics } from '../../diagnostics.js' ;
1314import { 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' ;
1516import type { SecutilsWindow } from '../index.js' ;
1617
1718// Maximum size of the content in bytes (200KB).
@@ -169,9 +170,7 @@ async function getContent(
169170 if ( scripts ?. extractContent ) {
170171 log . debug ( `[${ url } ] Adding "extractContent" function: ${ scripts . extractContent } .` ) ;
171172 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 } } };` ,
175174 } ) ;
176175 }
177176
@@ -221,7 +220,11 @@ async function getContent(
221220 const externalResources = await fetchInterceptor . stop ( ) ;
222221 extractedContent = jsonStableStringify (
223222 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+ } )
225228 : jsBeautify . html_beautify ( await page . content ( ) ) ,
226229 ) ;
227230 } catch ( err ) {
@@ -256,15 +259,10 @@ async function getContent(
256259 return { type : 'success' , data : { timestamp, content : extractedContent } } ;
257260}
258261
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 > {
265263 const targetWindow = await page . evaluateHandle < Window > ( 'window' ) ;
266264 return await page . evaluate (
267- async ( [ targetWindow , previousContent , externalResources , responseHeaders ] ) => {
265+ async ( [ targetWindow , context ] ) => {
268266 const extractContent = targetWindow . __secutils ?. extractContent ;
269267 if ( extractContent && typeof extractContent !== 'function' ) {
270268 console . error ( `[browser] Invalid "extractContent" function: ${ typeof extractContent } ` ) ;
@@ -274,11 +272,10 @@ async function extractContent(
274272
275273 try {
276274 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
282279 : null ;
283280 } catch ( err : unknown ) {
284281 console . error ( `[browser] Content extractor script has thrown an exception: ${ ( err as Error ) ?. message ?? err } .` ) ;
@@ -287,6 +284,6 @@ async function extractContent(
287284 throw new Error ( `Content extractor script has thrown an exception: ${ ( err as Error ) ?. message ?? err } .` ) ;
288285 }
289286 } ,
290- [ targetWindow as JSHandle < SecutilsWindow > , previousContent , externalResources , responseHeaders ] as const ,
287+ [ targetWindow as JSHandle < SecutilsWindow > , context ] as const ,
291288 ) ;
292289}
0 commit comments