Skip to content

Instantly share code, notes, and snippets.

@dtychshenko
Last active May 10, 2023 21:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dtychshenko/d2b07e47988504a7b81865faaa09863c to your computer and use it in GitHub Desktop.
Save dtychshenko/d2b07e47988504a7b81865faaa09863c to your computer and use it in GitHub Desktop.
Electron webview crashes
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
</head>
<body style="display:flex;flex-wrap:wrap;">
<script src="./renderer.js"></script>
</body>
</html>
const { app, ipcMain, webContents, BrowserWindow } = require("electron");
const path = require("path");
const url = "https://codesandbox.io/s/react-new";
function createWindow(i) {
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
x: i * 10,
y: i * 10,
webPreferences: {
nodeIntegration: true,
nodeIntegrationInSubFrames: true,
nodeIntegrationInSubFrames: true,
contextIsolation: false,
webviewTag: true,
preload: path.join(__dirname, "preload.js"),
},
});
mainWindow.loadFile("index.html");
}
app.whenReady().then(() => {
for (let i = 0; i < 10; i++) {
createWindow(i);
}
});
ipcMain.on("webview-is-ready", (e, { webContentId }) => {
const viewWebContents = webContents.fromId(webContentId);
viewWebContents.addListener("render-process-gone", (event, details) => {
const { reason, exitCode } = details;
console.error(
`view ${webContentId} has crashed, reason: ${reason}, exitCode: ${exitCode}`
);
});
viewWebContents.loadURL(url);
});
const { ipcRenderer } = require("electron");
(function () {
for (const _ of Array(10)) {
const viewContainer = document.createElement("webview");
document.body.appendChild(viewContainer);
viewContainer.setAttribute("disablewebsecurity", "");
viewContainer.setAttribute("nodeintegration", "");
viewContainer.setAttribute("webpreferences", "contextIsolation=false");
viewContainer.setAttribute("src", "about:blank");
viewContainer.setAttribute("style", 'style="width: 100px;height: 100px;');
viewContainer.addEventListener("did-finish-load", () => {
const webContentId = viewContainer.getWebContentsId();
ipcRenderer.send("webview-is-ready", { webContentId });
});
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment