openSoftware(software = 'chrome.exe', win) {
import('chrome-launcher').then(async (ChromeLauncher) => {
const chrome = await ChromeLauncher.launch({
startingUrl: 'https://www.baidu.com',
}).then(chrome => {
console.log(`Chrome debugging port running on ${chrome.port}`);
win.webContents.send('softwarePid', chrome.port);
chrome.process.on('exit', () => {
console.log('Chrome 浏览器已关闭');
win.webContents.send('processClose', chrome.port);
});
})
})
}
cnpm i chrome-launcher引入npm包
ChromeLauncher.launch({ startingUrl: 'https://www.baidu.com', }) 可以打开谷歌浏览器,launch方法参数是对象,里面是chrome的可配置参数
chrome.process.on('exit' 可以监听谷歌浏览器退出事件,手动关闭浏览器会进入回调