@@ -354,70 +354,51 @@ export class BrowserSession {
354354 if ( ! this . browser ) {
355355 throw new Error ( "Browser is not launched" )
356356 }
357- // Remove trailing slash for comparison
357+ // Normalize for comparison
358358 const normalizedNewUrl = url . replace ( / \/ $ / , "" )
359359
360- // Extract the root domain from the URL
361- const rootDomain = this . getRootDomain ( normalizedNewUrl )
362-
363- // Get all current pages
360+ // Select a single target page to enforce strict single-tab behavior
364361 const pages = await this . browser . pages ( )
362+ let targetPage : Page | undefined = this . page ?? pages [ 0 ]
365363
366- // Try to find a page with the same root domain
367- let existingPage : Page | undefined
364+ // If no page exists yet, create one directly
365+ if ( ! targetPage ) {
366+ return this . createNewTab ( normalizedNewUrl )
367+ }
368368
369- for ( const page of pages ) {
370- try {
371- const pageUrl = page . url ( )
372- if ( pageUrl && this . getRootDomain ( pageUrl ) === rootDomain ) {
373- existingPage = page
374- break
369+ // Close all other pages to keep exactly one tab
370+ for ( const p of pages ) {
371+ if ( p !== targetPage ) {
372+ try {
373+ await p . close ( ) . catch ( ( ) => { } )
374+ } catch {
375+ // best-effort
375376 }
376- } catch ( error ) {
377- // Skip pages that might have been closed or have errors
378- console . log ( `Error checking page URL: ${ error } ` )
379- continue
380377 }
381378 }
382379
383- if ( existingPage ) {
384- // Tab with the same root domain exists, switch to it
385- console . log ( `Tab with domain ${ rootDomain } already exists, switching to it` )
386-
387- // Update the active page
388- this . page = existingPage
389- existingPage . bringToFront ( )
390-
391- // Navigate to the new URL if it's different]
392- const currentUrl = existingPage . url ( ) . replace ( / \/ $ / , "" ) // Remove trailing / if present
393- if ( this . getRootDomain ( currentUrl ) === rootDomain && currentUrl !== normalizedNewUrl ) {
394- console . log ( `Navigating to new URL: ${ normalizedNewUrl } ` )
395- console . log ( `Current URL: ${ currentUrl } ` )
396- console . log ( `Root domain: ${ this . getRootDomain ( currentUrl ) } ` )
397- console . log ( `New URL: ${ normalizedNewUrl } ` )
398- // Navigate to the new URL
399- return this . doAction ( async ( page ) => {
400- await this . navigatePageToUrl ( page , normalizedNewUrl )
401- } )
402- } else {
403- console . log ( `Tab with domain ${ rootDomain } already exists, and URL is the same: ${ normalizedNewUrl } ` )
404- // URL is the same, just reload the page to ensure it's up to date
405- console . log ( `Reloading page: ${ normalizedNewUrl } ` )
406- console . log ( `Current URL: ${ currentUrl } ` )
407- console . log ( `Root domain: ${ this . getRootDomain ( currentUrl ) } ` )
408- console . log ( `New URL: ${ normalizedNewUrl } ` )
409- return this . doAction ( async ( page ) => {
410- await page . reload ( {
411- timeout : BROWSER_NAVIGATION_TIMEOUT ,
412- waitUntil : [ "domcontentloaded" , "networkidle2" ] ,
413- } )
414- await this . waitTillHTMLStable ( page )
415- } )
416- }
380+ // Use the selected page
381+ this . page = targetPage
382+ try {
383+ targetPage . bringToFront ( )
384+ } catch {
385+ // no-op if bringToFront fails
386+ }
387+
388+ // Navigate if URL changed, otherwise reload to ensure freshness
389+ const currentUrl = ( targetPage . url ?.( ) || "" ) . replace ( / \/ $ / , "" )
390+ if ( currentUrl !== normalizedNewUrl ) {
391+ return this . doAction ( async ( page ) => {
392+ await this . navigatePageToUrl ( page , normalizedNewUrl )
393+ } )
417394 } else {
418- // No tab with this root domain exists, create a new one
419- console . log ( `No tab with domain ${ rootDomain } exists, creating a new one` )
420- return this . createNewTab ( normalizedNewUrl )
395+ return this . doAction ( async ( page ) => {
396+ await page . reload ( {
397+ timeout : BROWSER_NAVIGATION_TIMEOUT ,
398+ waitUntil : [ "domcontentloaded" , "networkidle2" ] ,
399+ } )
400+ await this . waitTillHTMLStable ( page )
401+ } )
421402 }
422403 }
423404
0 commit comments