File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -63,6 +63,53 @@ async function getBrowserContextForProfile(
6363 const contexts = browser . browserContexts ( ) ;
6464 logger ( `Found ${ contexts . length } browser context(s)` ) ;
6565
66+ for ( const context of contexts ) {
67+ let page ;
68+ try {
69+ page = await context . newPage ( ) ;
70+
71+ await page . goto ( 'chrome://version' , {
72+ waitUntil : 'domcontentloaded' ,
73+ timeout : 3_000 ,
74+ } ) ;
75+
76+ const profilePath : string | null = await page . evaluate ( ( ) => {
77+ const body = document . querySelector ( 'body' ) ;
78+ if ( ! body ) return null ;
79+ const text = ( body . innerText || '' ) ;
80+ const match = text . match ( / P r o f i l e P a t h : \s * ( .+ ) / i) ;
81+ return match ? match [ 1 ] . trim ( ) : null ;
82+ } ) ;
83+
84+ try {
85+ await page . close ( ) ;
86+ } catch {
87+ //ignore close errors
88+ }
89+
90+ if ( ! profilePath ) {
91+ continue ;
92+ }
93+
94+ const actualProfile = getProfileNameFromUserDataDir ( profilePath ) ;
95+ logger ( `Probed context: profilePath=${ profilePath } => profileName=${ actualProfile } ` ) ;
96+
97+ if ( actualProfile === profileDirectory ) {
98+ logger ( `Matched profile directory "${ profileDirectory } " to a browser context` ) ;
99+ return context ;
100+ }
101+ } catch ( error ) {
102+ logger ( 'Error probing a browser context for profile: ' , error ) ;
103+ try {
104+ if ( page && ! page . isClosed ( ) ) {
105+ await page . close ( ) ;
106+ }
107+ } catch {
108+ // ignore
109+ }
110+ }
111+ }
112+
66113 logger (
67114 `Profile directory "${ profileDirectory } " specified. ` +
68115 `Using default browser context. Full profile support will be added in a future update.` ,
You can’t perform that action at this time.
0 commit comments