diff --git a/main.js b/main.js index ccb3f43..f0c1b3b 100644 --- a/main.js +++ b/main.js @@ -68,6 +68,7 @@ function createWindow() { mainWindow.loadFile('views/index.html'); mainWindow.setMenu(null); + mainWindow.on('closed', () => { headlessBrowser.forceKill(); }); } app.whenReady().then(() => { diff --git a/src/utils/headless-browser.js b/src/utils/headless-browser.js index 9b39ef9..fb06f68 100644 --- a/src/utils/headless-browser.js +++ b/src/utils/headless-browser.js @@ -176,6 +176,22 @@ class HeadlessBrowser { `; await win.webContents.executeJavaScript(script); } + + forceKill() { + try { + if (this.win && !this.win.isDestroyed()) { + const wc = this.win.webContents; + + try { wc.stop(); } catch (_) {} + try { wc.close(); } catch (_) {} + try { wc.destroy(); } catch (_) {} + + this.win.destroy(); + } + } catch (_) {} + + this.win = null; + } } module.exports = new HeadlessBrowser(); \ No newline at end of file