Created
April 15, 2021 15:05
-
-
Save Jelmerro/58c79993841cbf749089da173ebfe78e to your computer and use it in GitHub Desktop.
ipcRenderer connection drops when viewing source
This file contains hidden or 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> | |
<h1>Hello World!</h1> | |
<webview src="./index.html"></webview> | |
</body> | |
</html> |
This file contains hidden or 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} = require('electron') | |
const path = require('path') | |
function createWindow () { | |
const mainWindow = new BrowserWindow({ | |
width: 800, | |
height: 600, | |
webPreferences: { | |
preload: path.join(__dirname, 'renderer.js'), | |
contextIsolation: false, | |
webviewTag: true | |
} | |
}) | |
mainWindow.webContents.once("did-finish-load", () => { | |
mainWindow.webContents.on("will-attach-webview", (_, prefs) => { | |
prefs.preload = path.join(__dirname, 'preload.js') | |
prefs.contextIsolation = false | |
}) | |
}) | |
mainWindow.loadFile('index.html') | |
} | |
app.whenReady().then(createWindow) |
This file contains hidden or 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
window.addEventListener("DOMContentLoaded", () => { | |
document.querySelector("h1").textContent = "Hello Webview!" | |
const {ipcRenderer} = require("electron") | |
ipcRenderer.on("request", e => { | |
e.sender.sendToHost("response") | |
}) | |
}) |
This file contains hidden or 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
window.addEventListener("DOMContentLoaded", () => { | |
document.querySelector("h1").textContent = "Hello Renderer!" | |
const webview = document.querySelector("webview") | |
setInterval(() => { | |
document.body.appendChild(document.createTextNode(` - request `)) | |
webview.send("request") | |
}, 1000) | |
webview.addEventListener("ipc-message", e => { | |
document.body.appendChild(document.createTextNode(e.channel)) | |
}) | |
setTimeout(() => { | |
webview.src = "view-source:./index.html" | |
}, 3500) | |
}) |
This file contains hidden or 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
/* Empty */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment