Skip to content

Commit 6744657

Browse files
committed
Probe each BrowserContext
1 parent 276f81a commit 6744657

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

src/browser.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff 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(/Profile Path:\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.`,

0 commit comments

Comments
 (0)