update
This commit is contained in:
45
start-browser.js
Normal file
45
start-browser.js
Normal file
@@ -0,0 +1,45 @@
|
||||
import { spawn } from 'node:child_process';
|
||||
import path from 'node:path';
|
||||
|
||||
export const main = async () => {
|
||||
// Chrome 路径和配置
|
||||
const executablePath = 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe';
|
||||
// 使用独立的用户数据目录,避免与 Chrome 冲突
|
||||
const userDataDir = path.join(process.cwd(), 'browser-context');
|
||||
const debugPort = 9223;
|
||||
|
||||
console.log('启动 Chrome...');
|
||||
console.log(`端口: ${debugPort}`);
|
||||
console.log(`用户数据目录: ${userDataDir}`);
|
||||
// console.log('注意:需要手动登录账号和安装插件');
|
||||
|
||||
// 启动 Chrome(带远程调试端口)
|
||||
const chromeProcess = spawn(
|
||||
executablePath,
|
||||
[
|
||||
`--remote-debugging-port=${debugPort}`,
|
||||
`--user-data-dir=${userDataDir}`,
|
||||
// '--kiosk', // 全屏模式,无修改边框
|
||||
],
|
||||
{
|
||||
windowsHide: true, // 隐藏 CMD 窗口
|
||||
detached: false,
|
||||
stdio: ['ignore', 'ignore', 'ignore'],
|
||||
},
|
||||
);
|
||||
|
||||
chromeProcess.on('error', (err) => {
|
||||
console.error('Chrome 启动失败:', err);
|
||||
// 需要重新启动
|
||||
});
|
||||
|
||||
chromeProcess.on('exit', (code, signal) => {
|
||||
console.log(`Chrome 进程退出,代码: ${code}, 信号: ${signal}`);
|
||||
if (code === 0) {
|
||||
// 重启
|
||||
main();
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
main();
|
||||
Reference in New Issue
Block a user