Last active
May 10, 2023 21:17
-
-
Save dtychshenko/d2b07e47988504a7b81865faaa09863c to your computer and use it in GitHub Desktop.
Electron webview crashes
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> | |
<meta charset="UTF-8"> | |
<title>Hello World!</title> | |
</head> | |
<body style="display:flex;flex-wrap:wrap;"> | |
<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 { 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); | |
}); |
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"); | |
(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