import { getChromePath } from './getChromePath'; import { getProfileDir } from './getProfileDir'; import { isPortInUse } from './isPortInUse'; import { isChromeRunning } from './isChromeRunning'; import { spawn } from 'child_process'; import log from 'electron-log'; // 启动本地浏览器 export async function launchLocalChrome() { const chromePath = getChromePath(); // 多账号隔离 const profileDir = getProfileDir('default'); log.info(`Launching Chrome with user data dir: ${profileDir}`); // 检查端口是否被占用 const portInUse = await isPortInUse(9222); if (portInUse) { log.info('Chrome already running on port 9222, skip launching.'); return; } if (await isChromeRunning()) { log.info('Chrome already running, skip launching.'); return; } return new Promise((resolve, reject) => { const chromeProcess = spawn(chromePath as string, [ '--remote-debugging-port=9222', '--window-size=1920,1080', '--window-position=0,0', '--no-first-run', `--user-data-dir=${profileDir}`, '--no-default-browser-check', 'about:blank' // '--window-maximized', ], { detached: true, stdio: 'ignore' }); chromeProcess.on('error', reject); // 延迟几秒等浏览器起来 setTimeout(() => { resolve(0); }, 1000); // 延迟1秒 }); }