@@ -43,8 +43,9 @@ interface InterpreterOptions {
4343 binaryCallback : ( output : any , mimeType : string ) => ( void | Promise < void > ) ;
4444 debug : boolean ;
4545 debugChannel : Partial < {
46- activeId : Function ,
47- debugMessage : Function ,
46+ activeId : ( id : number ) => void ,
47+ debugMessage : ( msg : string ) => void ,
48+ setActionType : ( type : string ) => void ,
4849 } >
4950}
5051
@@ -377,12 +378,20 @@ export default class Interpreter extends EventEmitter {
377378 */
378379 const wawActions : Record < CustomFunctions , ( ...args : any [ ] ) => void > = {
379380 screenshot : async ( params : PageScreenshotOptions ) => {
381+ if ( this . options . debugChannel ?. setActionType ) {
382+ this . options . debugChannel . setActionType ( 'screenshot' ) ;
383+ }
384+
380385 const screenshotBuffer = await page . screenshot ( {
381386 ...params , path : undefined ,
382387 } ) ;
383388 await this . options . binaryCallback ( screenshotBuffer , 'image/png' ) ;
384389 } ,
385390 enqueueLinks : async ( selector : string ) => {
391+ if ( this . options . debugChannel ?. setActionType ) {
392+ this . options . debugChannel . setActionType ( 'enqueueLinks' ) ;
393+ }
394+
386395 const links : string [ ] = await page . locator ( selector )
387396 . evaluateAll (
388397 // @ts -ignore
@@ -409,55 +418,51 @@ export default class Interpreter extends EventEmitter {
409418 await page . close ( ) ;
410419 } ,
411420 scrape : async ( selector ?: string ) => {
421+ if ( this . options . debugChannel ?. setActionType ) {
422+ this . options . debugChannel . setActionType ( 'scrape' ) ;
423+ }
424+
412425 await this . ensureScriptsLoaded ( page ) ;
413426
414427 const scrapeResults : Record < string , string > [ ] = await page . evaluate ( ( s ) => window . scrape ( s ?? null ) , selector ) ;
415428 await this . options . serializableCallback ( scrapeResults ) ;
416429 } ,
417430
418431 scrapeSchema : async ( schema : Record < string , { selector : string ; tag : string , attribute : string ; shadow : string } > ) => {
432+ if ( this . options . debugChannel ?. setActionType ) {
433+ this . options . debugChannel . setActionType ( 'scrapeSchema' ) ;
434+ }
435+
419436 await this . ensureScriptsLoaded ( page ) ;
420437
421438 const scrapeResult = await page . evaluate ( ( schemaObj ) => window . scrapeSchema ( schemaObj ) , schema ) ;
422439
423- const newResults = Array . isArray ( scrapeResult ) ? scrapeResult : [ scrapeResult ] ;
424- newResults . forEach ( ( result ) => {
425- Object . entries ( result ) . forEach ( ( [ key , value ] ) => {
426- const keyExists = this . cumulativeResults . some (
427- ( item ) => key in item && item [ key ] !== undefined
428- ) ;
429-
430- if ( ! keyExists ) {
431- this . cumulativeResults . push ( { [ key ] : value } ) ;
432- }
433- } ) ;
440+ if ( ! this . cumulativeResults || ! Array . isArray ( this . cumulativeResults ) ) {
441+ this . cumulativeResults = [ ] ;
442+ }
443+
444+ if ( this . cumulativeResults . length === 0 ) {
445+ this . cumulativeResults . push ( { } ) ;
446+ }
447+
448+ const mergedResult = this . cumulativeResults [ 0 ] ;
449+ const resultToProcess = Array . isArray ( scrapeResult ) ? scrapeResult [ 0 ] : scrapeResult ;
450+
451+ Object . entries ( resultToProcess ) . forEach ( ( [ key , value ] ) => {
452+ if ( value !== undefined ) {
453+ mergedResult [ key ] = value ;
454+ }
434455 } ) ;
435-
436- const mergedResult : Record < string , string > [ ] = [
437- Object . fromEntries (
438- Object . entries (
439- this . cumulativeResults . reduce ( ( acc , curr ) => {
440- Object . entries ( curr ) . forEach ( ( [ key , value ] ) => {
441- // If the key doesn't exist or the current value is not undefined, add/update it
442- if ( value !== undefined ) {
443- acc [ key ] = value ;
444- }
445- } ) ;
446- return acc ;
447- } , { } )
448- )
449- )
450- ] ;
451-
452- // Log cumulative results after each action
453- console . log ( "CUMULATIVE results:" , this . cumulativeResults ) ;
454- console . log ( "MERGED results:" , mergedResult ) ;
455-
456- await this . options . serializableCallback ( mergedResult ) ;
457- // await this.options.serializableCallback(scrapeResult);
456+
457+ console . log ( "Updated merged result:" , mergedResult ) ;
458+ await this . options . serializableCallback ( [ mergedResult ] ) ;
458459 } ,
459460
460461 scrapeList : async ( config : { listSelector : string , fields : any , limit ?: number , pagination : any } ) => {
462+ if ( this . options . debugChannel ?. setActionType ) {
463+ this . options . debugChannel . setActionType ( 'scrapeList' ) ;
464+ }
465+
461466 await this . ensureScriptsLoaded ( page ) ;
462467 if ( ! config . pagination ) {
463468 const scrapeResults : Record < string , any > [ ] = await page . evaluate ( ( cfg ) => window . scrapeList ( cfg ) , config ) ;
@@ -469,6 +474,10 @@ export default class Interpreter extends EventEmitter {
469474 } ,
470475
471476 scrapeListAuto : async ( config : { listSelector : string } ) => {
477+ if ( this . options . debugChannel ?. setActionType ) {
478+ this . options . debugChannel . setActionType ( 'scrapeListAuto' ) ;
479+ }
480+
472481 await this . ensureScriptsLoaded ( page ) ;
473482
474483 const scrapeResults : { selector : string , innerText : string } [ ] = await page . evaluate ( ( listSelector ) => {
@@ -479,6 +488,10 @@ export default class Interpreter extends EventEmitter {
479488 } ,
480489
481490 scroll : async ( pages ?: number ) => {
491+ if ( this . options . debugChannel ?. setActionType ) {
492+ this . options . debugChannel . setActionType ( 'scroll' ) ;
493+ }
494+
482495 await page . evaluate ( async ( pagesInternal ) => {
483496 for ( let i = 1 ; i <= ( pagesInternal ?? 1 ) ; i += 1 ) {
484497 // @ts -ignore
@@ -488,6 +501,10 @@ export default class Interpreter extends EventEmitter {
488501 } ,
489502
490503 script : async ( code : string ) => {
504+ if ( this . options . debugChannel ?. setActionType ) {
505+ this . options . debugChannel . setActionType ( 'script' ) ;
506+ }
507+
491508 const AsyncFunction : FunctionConstructor = Object . getPrototypeOf (
492509 async ( ) => { } ,
493510 ) . constructor ;
@@ -496,6 +513,10 @@ export default class Interpreter extends EventEmitter {
496513 } ,
497514
498515 flag : async ( ) => new Promise ( ( res ) => {
516+ if ( this . options . debugChannel ?. setActionType ) {
517+ this . options . debugChannel . setActionType ( 'flag' ) ;
518+ }
519+
499520 this . emit ( 'flag' , page , res ) ;
500521 } ) ,
501522 } ;
@@ -526,6 +547,10 @@ export default class Interpreter extends EventEmitter {
526547 const params = ! step . args || Array . isArray ( step . args ) ? step . args : [ step . args ] ;
527548 await wawActions [ step . action as CustomFunctions ] ( ...( params ?? [ ] ) ) ;
528549 } else {
550+ if ( this . options . debugChannel ?. setActionType ) {
551+ this . options . debugChannel . setActionType ( String ( step . action ) ) ;
552+ }
553+
529554 // Implements the dot notation for the "method name" in the workflow
530555 const levels = String ( step . action ) . split ( '.' ) ;
531556 const methodName = levels [ levels . length - 1 ] ;
0 commit comments