-
-
Save mgalla10/cdff38e750ae49d01500445b019493ca to your computer and use it in GitHub Desktop.
Destroyed event not emitted on close for custom BrowserView.webContents
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const {app, BrowserWindow, BrowserView, ipcMain, dialog} = require('electron') | |
async function createWindow () { | |
const mainWindow = new BrowserWindow(); | |
await mainWindow.loadFile('page1.html'); | |
const view = new BrowserView({ | |
webPreferences: { | |
nodeIntegration: true, | |
contextIsolation: false, | |
} | |
}); | |
mainWindow.addBrowserView(view); | |
view.setBounds({x: 0, y: 50, width: 800, height: 600}); | |
const webContents = view.webContents; | |
await webContents.loadFile('page2.html'); | |
ipcMain.once("close-page", async () => | |
{ | |
await dialog.showMessageBox(null, {message: "Closing page 2"}); | |
const promise = new Promise((resolve) => | |
{ | |
webContents.once("destroyed", () => resolve()); | |
webContents.close(); | |
}) | |
await promise; | |
await dialog.showMessageBox(null, {message: "Page 2 closed"}); | |
}); | |
} | |
app.whenReady().then(() => { | |
createWindow(); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "shiny-angle-adopt-65fzp", | |
"productName": "shiny-angle-adopt-65fzp", | |
"description": "My Electron application description", | |
"keywords": [], | |
"main": "./main.js", | |
"version": "1.0.0", | |
"author": "mgallagh", | |
"scripts": { | |
"start": "electron ." | |
}, | |
"dependencies": {}, | |
"devDependencies": { | |
"electron": "22.3.0" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Page 1</title> | |
</head> | |
<body> | |
<h1>Page 1</h1> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Page 2</title> | |
</head> | |
<body> | |
<h1>Page 2</h1> | |
Click the following button below to close this page: | |
<button id="btnClose">Close</button> | |
<script src="./renderer.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { ipcRenderer } = require("electron"); | |
document.getElementById("btnClose").addEventListener("click", () => | |
{ | |
ipcRenderer.send("close-page"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment