feat: implement browser open functionality and related tests
This commit is contained in:
@@ -5,30 +5,56 @@ import { isChromeRunning } from './isChromeRunning';
|
||||
import { spawn } from 'child_process';
|
||||
import log from 'electron-log';
|
||||
|
||||
const CHROME_CDP_PORT = 9222;
|
||||
const CHROME_READY_TIMEOUT_MS = 15_000;
|
||||
const CHROME_READY_POLL_MS = 250;
|
||||
|
||||
function sleep(ms: number): Promise<void> {
|
||||
return new Promise((resolve) => {
|
||||
setTimeout(resolve, ms);
|
||||
});
|
||||
}
|
||||
|
||||
async function waitForChromeReady(timeoutMs = CHROME_READY_TIMEOUT_MS): Promise<boolean> {
|
||||
const startedAt = Date.now();
|
||||
|
||||
while (Date.now() - startedAt < timeoutMs) {
|
||||
if (await isChromeRunning()) {
|
||||
return true;
|
||||
}
|
||||
await sleep(CHROME_READY_POLL_MS);
|
||||
}
|
||||
|
||||
return await isChromeRunning();
|
||||
}
|
||||
|
||||
// 启动本地浏览器
|
||||
export async function launchLocalChrome() {
|
||||
const chromePath = getChromePath();
|
||||
if (!chromePath) {
|
||||
throw new Error(`Unsupported platform for Chrome launch: ${process.platform}`);
|
||||
}
|
||||
|
||||
// 多账号隔离
|
||||
const profileDir = getProfileDir('default');
|
||||
log.info(`Launching Chrome with user data dir: ${profileDir}`);
|
||||
|
||||
// 检查端口是否被占用
|
||||
const portInUse = await isPortInUse(9222);
|
||||
const portInUse = await isPortInUse(CHROME_CDP_PORT);
|
||||
const chromeReady = await isChromeRunning();
|
||||
|
||||
if (chromeReady) {
|
||||
log.info('Chrome DevTools endpoint already available, skip launching.');
|
||||
return;
|
||||
}
|
||||
|
||||
if (portInUse) {
|
||||
log.info('Chrome already running on port 9222, skip launching.');
|
||||
return;
|
||||
throw new Error(`Port ${CHROME_CDP_PORT} is already in use, but Chrome DevTools is unavailable`);
|
||||
}
|
||||
|
||||
if (await isChromeRunning()) {
|
||||
log.info('Chrome already running, skip launching.');
|
||||
return;
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
const chromeProcess = spawn(chromePath as string, [
|
||||
'--remote-debugging-port=9222',
|
||||
`--remote-debugging-port=${CHROME_CDP_PORT}`,
|
||||
'--window-size=1920,1080',
|
||||
'--window-position=0,0',
|
||||
'--no-first-run',
|
||||
@@ -38,14 +64,19 @@ export async function launchLocalChrome() {
|
||||
// '--window-maximized',
|
||||
], {
|
||||
detached: true,
|
||||
stdio: 'ignore'
|
||||
stdio: 'ignore',
|
||||
});
|
||||
|
||||
chromeProcess.on('error', reject);
|
||||
chromeProcess.on('error', (error) => {
|
||||
reject(new Error(`Failed to launch Chrome at ${chromePath}: ${error instanceof Error ? error.message : String(error)}`));
|
||||
});
|
||||
|
||||
// 延迟几秒等浏览器起来
|
||||
setTimeout(() => {
|
||||
resolve(0);
|
||||
}, 1000); // 延迟1秒
|
||||
chromeProcess.unref();
|
||||
resolve();
|
||||
});
|
||||
|
||||
const ready = await waitForChromeReady();
|
||||
if (!ready) {
|
||||
throw new Error(`Chrome launched but DevTools endpoint http://127.0.0.1:${CHROME_CDP_PORT} was not ready within ${CHROME_READY_TIMEOUT_MS}ms`);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user