@@ -40,23 +40,34 @@ export const browseStartTool: Tool<Parameters, ReturnType> = {
4040
4141 execute : async (
4242 { url, timeout = 30000 } ,
43- { logger, headless = true } ,
43+ { logger, headless = true , userSession = false } ,
4444 ) : Promise < ReturnType > => {
4545 logger . verbose ( `Starting browser session${ url ? ` at ${ url } ` : '' } ` ) ;
46+ logger . verbose ( `User session mode: ${ userSession ? 'enabled' : 'disabled' } ` ) ;
4647
4748 try {
4849 const instanceId = uuidv4 ( ) ;
4950
5051 // Launch browser
51- const browser = await chromium . launch ( {
52+ const launchOptions = {
5253 headless,
53- } ) ;
54+ } ;
55+
56+ // Use system Chrome installation if userSession is true
57+ if ( userSession ) {
58+ logger . verbose ( 'Using system Chrome installation' ) ;
59+ // For Chrome, we use the channel option to specify Chrome
60+ launchOptions [ 'channel' ] = 'chrome' ;
61+ }
62+
63+ const browser = await chromium . launch ( launchOptions ) ;
5464
5565 // Create new context with default settings
5666 const context = await browser . newContext ( {
5767 viewport : null ,
5868 userAgent :
5969 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36' ,
70+ serviceWorkers : 'block' , // Block service workers which can cause continuous network activity
6071 } ) ;
6172
6273 // Create new page
@@ -80,11 +91,38 @@ export const browseStartTool: Tool<Parameters, ReturnType> = {
8091 // Navigate to URL if provided
8192 let content = '' ;
8293 if ( url ) {
83- await page . goto ( url , { waitUntil : 'networkidle' } ) ;
84- content = await page . content ( ) ;
94+ try {
95+ // Try with 'domcontentloaded' first which is more reliable than 'networkidle'
96+ logger . verbose (
97+ `Navigating to ${ url } with 'domcontentloaded' waitUntil` ,
98+ ) ;
99+ await page . goto ( url , { waitUntil : 'domcontentloaded' , timeout } ) ;
100+ content = await page . content ( ) ;
101+ logger . verbose ( 'Navigation completed with domcontentloaded strategy' ) ;
102+ } catch ( error ) {
103+ // If that fails, try with no waitUntil option at all (most basic)
104+ logger . warn (
105+ `Failed with domcontentloaded strategy: ${ errorToString ( error ) } ` ,
106+ ) ;
107+ logger . verbose (
108+ `Retrying navigation to ${ url } with no waitUntil option` ,
109+ ) ;
110+
111+ try {
112+ await page . goto ( url , { timeout } ) ;
113+ content = await page . content ( ) ;
114+ logger . verbose ( 'Navigation completed with basic strategy' ) ;
115+ } catch ( innerError ) {
116+ logger . error (
117+ `Failed with basic navigation strategy: ${ errorToString ( innerError ) } ` ,
118+ ) ;
119+ throw innerError ; // Re-throw to be caught by outer catch block
120+ }
121+ }
85122 }
86123
87124 logger . verbose ( 'Browser session started successfully' ) ;
125+ logger . verbose ( `Content: ${ content } ` ) ;
88126
89127 return {
90128 instanceId,
0 commit comments