nodejs以app模式启动chrome
const { spawn } = require('child_process');
// 根据你的操作系统更改下面的路径
const chromePath = process.platform === 'darwin'
// /Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge
? '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'
: process.platform === 'win32'
? 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe'
: '/usr/bin/google-chrome';
// 使用 --app 参数启动 Chrome
const chrome = spawn(chromePath, ['--app=http://example.com']); // 或者使用本地文件路径:'--app=file:///path/to/index.html'
// 监听子进程的输出(如果有的话)
chrome.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
chrome.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
});
// 子进程退出时监听
chrome.on('close', (code) => {
console.log(`child process exited with code ${code}`);
});