豆豆友情提示:这是一个非官方 GitHub 代理镜像,主要用于网络测试或访问加速。请勿在此进行登录、注册或处理任何敏感信息。进行这些操作请务必访问官方网站 github.com。 Raw 内容也通过此代理提供。
Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 49 additions & 5 deletions src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export async function ensureBrowserConnected(options: {
interface McpLaunchOptions {
acceptInsecureCerts?: boolean;
executablePath?: string;
customDevTools?: string; // Added back for customDevTools logic in launch
channel?: Channel;
userDataDir?: string;
headless: boolean;
Expand All @@ -73,8 +74,39 @@ interface McpLaunchOptions {
devtools: boolean;
}

function resolveExecutablePath(channel: Channel): string | undefined {
if (os.platform() !== 'win32') {
return undefined;
}
const programFiles = process.env['PROGRAMFILES'];
if (!programFiles) {
return undefined;
}

const channelPathPart =
channel === 'canary'
? 'Chrome SxS'
: channel === 'dev'
? 'Chrome Dev'
: channel === 'beta'
? 'Chrome Beta'
: '';

const chromePath = path.join(
programFiles,
'Google',
'Chrome',
channelPathPart,
'Application',
'chrome.exe',
);

return fs.existsSync(chromePath) ? chromePath : undefined;
}

export async function launch(options: McpLaunchOptions): Promise<Browser> {
const {channel, executablePath, headless, isolated} = options;
// Ensure customDevTools is destructured for use in args below
const {channel, executablePath, customDevTools, headless, isolated} = options;
const profileDirName =
channel && channel !== 'stable'
? `chrome-profile-${channel}`
Expand All @@ -93,29 +125,41 @@ export async function launch(options: McpLaunchOptions): Promise<Browser> {
});
}

// --- Start Merged Args & Path Resolution ---
const args: LaunchOptions['args'] = [
...(options.args ?? []),
'--hide-crash-restore-bubble',
];
if (customDevTools) {
args.push(`--custom-devtools-frontend=file://${customDevTools}`);
}
if (headless) {
args.push('--screen-info={3840x2160}');
}
let puppeteerChannel: ChromeReleaseChannel | undefined;
if (options.devtools) {
args.push('--auto-open-devtools-for-tabs');
}
if (!executablePath) {

let puppeteerChannel: ChromeReleaseChannel | undefined;
let resolvedExecutablePath = executablePath;

if (!resolvedExecutablePath) {
resolvedExecutablePath = resolveExecutablePath(channel as Channel);
}

if (!resolvedExecutablePath) {
puppeteerChannel =
channel && channel !== 'stable'
? (`chrome-${channel}` as ChromeReleaseChannel)
: 'chrome';
}
// --- End Merged Args & Path Resolution ---

try {
const browser = await puppeteer.launch({
channel: puppeteerChannel,
targetFilter: makeTargetFilter(options.devtools),
executablePath,
executablePath: resolvedExecutablePath, // Uses the resolved path
defaultViewport: null,
userDataDir,
pipe: true,
Expand Down Expand Up @@ -167,4 +211,4 @@ export async function ensureBrowserLaunched(
return browser;
}

export type Channel = 'stable' | 'canary' | 'beta' | 'dev';
export type Channel = 'stable' | 'canary' | 'beta' | 'dev';