Skip to content

Instantly share code, notes, and snippets.

@mgalla10

mgalla10/main.js Secret

Last active February 20, 2023 21:11
Show Gist options
  • Save mgalla10/cdff38e750ae49d01500445b019493ca to your computer and use it in GitHub Desktop.
Save mgalla10/cdff38e750ae49d01500445b019493ca to your computer and use it in GitHub Desktop.
Destroyed event not emitted on close for custom BrowserView.webContents
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();
});
{
"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"
}
}
<!DOCTYPE html>
<html>
<head>
<title>Page 1</title>
</head>
<body>
<h1>Page 1</h1>
</body>
</html>
<!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>
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