Skip to content

Commit 276f81a

Browse files
committed
fixed the DevToolsActivePort, removed the profile-related checks from the connection path, switched to referencing browser.browserContexts()
1 parent 3d5da8b commit 276f81a

1 file changed

Lines changed: 62 additions & 31 deletions

File tree

src/browser.ts

Lines changed: 62 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import path from 'node:path';
1111
import {logger} from './logger.js';
1212
import type {
1313
Browser,
14+
BrowserContext,
1415
ChromeReleaseChannel,
1516
LaunchOptions,
1617
Target,
@@ -50,6 +51,30 @@ function getProfileNameFromUserDataDir(userDataDir: string): string {
5051
return parts[parts.length - 1] || 'Default';
5152
}
5253

54+
async function getBrowserContextForProfile(
55+
browser: Browser,
56+
profileDirectory?: string,
57+
): Promise<BrowserContext> {
58+
if (!profileDirectory) {
59+
return browser.defaultBrowserContext();
60+
}
61+
62+
try {
63+
const contexts = browser.browserContexts();
64+
logger(`Found ${contexts.length} browser context(s)`);
65+
66+
logger(
67+
`Profile directory "${profileDirectory}" specified. ` +
68+
`Using default browser context. Full profile support will be added in a future update.`,
69+
);
70+
71+
return browser.defaultBrowserContext();
72+
} catch (error) {
73+
logger('Error getting browser contexts: ', error);
74+
return browser.defaultBrowserContext();
75+
}
76+
}
77+
5378
export async function ensureBrowserConnected(options: {
5479
browserURL?: string;
5580
wsEndpoint?: string;
@@ -135,39 +160,12 @@ export async function ensureBrowserConnected(options: {
135160
);
136161
}
137162

138-
if (options.profileDirectory && options.userDataDir) {
139-
try {
140-
const portPath = path.join(options.userDataDir, 'DevToolsActivatePort');
141-
const fileContent = await fs.promises.readFile(portPath, 'utf8');
142-
const lines = fileContent
143-
.split('\n')
144-
.map(line => line.trim())
145-
.filter(line => line);
146-
147-
if (lines.length >= 2) {
148-
const browserPath = lines[1];
149-
const actualProfile = getProfileNameFromUserDataDir(browserPath);
150-
const requestedProfile = options.profileDirectory;
151-
152-
if (actualProfile !== requestedProfile) {
153-
await browser.disconnect();
154-
throw new Error(
155-
`Profile mismatch: Requested profile "${requestedProfile}" but Chrome is running with profile "${actualProfile}". ` +
156-
`Please close Chrome and restart with the correct profile, or remove the --profile-directory flag.`,
157-
);
158-
}
159-
160-
logger(`Successfully validated profile: ${actualProfile}`);
161-
}
162-
} catch (error) {
163-
if ((error as Error).message.includes('Profile mismatch')) {
164-
throw error;
165-
}
163+
logger('Connected Puppeteer');
166164

167-
logger('Could not validate profile directory: ', error);
168-
}
165+
if (options.profileDirectory) {
166+
await getBrowserContextForProfile(browser, options.profileDirectory);
167+
logger(`Using browser context for profile: ${options.profileDirectory}`);
169168
}
170-
logger('Connected Puppeteer');
171169
return browser;
172170
}
173171

@@ -215,6 +213,9 @@ export async function launch(options: McpLaunchOptions): Promise<Browser> {
215213
];
216214
if (options.profileDirectory) {
217215
args.push(`--profile-directory=${options.profileDirectory}`);
216+
logger(
217+
`Launcing Chrome with profile directory: ${options.profileDirectory}`,
218+
);
218219
}
219220
const ignoreDefaultArgs: LaunchOptions['ignoreDefaultArgs'] =
220221
options.ignoreDefaultChromeArgs ?? false;
@@ -260,6 +261,36 @@ export async function launch(options: McpLaunchOptions): Promise<Browser> {
260261
contentHeight: options.viewport.height,
261262
});
262263
}
264+
265+
if (options.profileDirectory && userDataDir) {
266+
try {
267+
await new Promise(resolve => setTimeout(resolve, 500));
268+
269+
const portPath = path.join(userDataDir, 'DevToolsActivePort');
270+
const fileContent = await fs.promises.readFile(portPath, 'utf8');
271+
const lines = fileContent
272+
.split('\n')
273+
.map(line => line.trim())
274+
.filter(line => line);
275+
276+
if (lines.length >= 2) {
277+
const browserPath = lines[1];
278+
const actualProfile = getProfileNameFromUserDataDir(browserPath);
279+
const requestedProfile = options.profileDirectory;
280+
281+
if (actualProfile !== requestedProfile) {
282+
logger(
283+
`Warning: Requested profile "${requestedProfile}" but Chrome may be using profile "${actualProfile}". ` +
284+
`This could happen if Chrome is managing profiles differently.`,
285+
);
286+
} else {
287+
logger(`Successfully validated profile: ${actualProfile}`);
288+
}
289+
}
290+
} catch (error) {
291+
logger('Could not validate profile directory after launch: ', error);
292+
}
293+
}
263294
return browser;
264295
} catch (error) {
265296
if (

0 commit comments

Comments
 (0)