@@ -570,9 +570,39 @@ export class MirrorCloner {
570570 timeout : this . options . timeout ,
571571 } ) ;
572572
573+ // Capture the baseline URL after potentially initial redirects but BEFORE cookie clicking
574+ const baselineUrl = page . url ( ) ;
575+
573576 // Handle cookie consent banners before further processing
574577 await this . browserEngine . handleCookieConsent ( page ) ;
575578
579+ // Give a small window for any triggered navigation to start
580+ await new Promise ( r => setTimeout ( r , 1000 ) ) ;
581+
582+ // Post-cookie click safety check: verify if we navigated away from the original intent
583+ let currentUrl = page . url ( ) ;
584+ if ( currentUrl !== baselineUrl ) {
585+ // If we navigated to what looks like a cookie settings/privacy page, try to go back
586+ const isCookiePage = / c o o k i e | c o n s e n t | p r i v a c y | s e t t i n g s | p o l i c y / i. test ( currentUrl ) ;
587+ const hostChanged = ( new URL ( currentUrl ) . hostname !== new URL ( baselineUrl ) . hostname ) ;
588+
589+ if ( isCookiePage || hostChanged ) {
590+ if ( this . options . debug ) {
591+ console . log ( chalk . yellow ( ` ⚠️ Cookie consent caused navigation to: ${ currentUrl } . Attempting to backtracking to: ${ baselineUrl } ` ) ) ;
592+ }
593+ try {
594+ // Go back or re-navigate
595+ await page . goBack ( { waitUntil : 'domcontentloaded' , timeout : 10000 } ) . catch ( async ( ) => {
596+ await page . goto ( baselineUrl , { waitUntil : 'domcontentloaded' , timeout : 10000 } ) ;
597+ } ) ;
598+ // Ensure we are back
599+ currentUrl = page . url ( ) ;
600+ } catch ( e ) {
601+ if ( this . options . debug ) console . log ( chalk . dim ( ` Backtracking failed: ${ e . message } ` ) ) ;
602+ }
603+ }
604+ }
605+
576606 await this . waitForRootReady ( page ) ;
577607 await this . scrollToBottomAndLoad ( page ) ;
578608 await this . waitForImagesSettled ( page , 8000 ) ;
0 commit comments