Skip to content

Instantly share code, notes, and snippets.

@ckerr
Created July 20, 2020 15:59
Show Gist options
  • Save ckerr/315d7b9da1ac0202b72cbe2dff745953 to your computer and use it in GitHub Desktop.
Save ckerr/315d7b9da1ac0202b72cbe2dff745953 to your computer and use it in GitHub Desktop.
const {app, BrowserWindow, ipcMain} = require('electron');
app.on('ready', () => {
const window = new BrowserWindow({ webPreferences: { webviewTag: true } });
window.loadURL('file://' + app.getAppPath() + '/renderer.html');
});
ipcMain.on('visibilityState', (event, evType, visibilityState) => {
console.log('visibilityState', evType, visibilityState);
})
{
"name": "webview-visibilitystate-unload",
"devDependencies": {
"electron": "10.0.0-beta.12"
},
"scripts": {
"run": "electron main.js"
}
}
const { ipcRenderer } = require("electron");
function sendVisibilityState(ev) {
ipcRenderer.send("visibilityState", ev.type, document.visibilityState);
}
document.addEventListener('visibilitychange', sendVisibilityState);
window.addEventListener('beforeunload', sendVisibilityState);
window.addEventListener('unload', sendVisibilityState);
<!DOCTYPE html>
<html>
<head>
<title>webview-visibilitystate-unload</title>
</head>
<body>
<webview id="webview" src="./webview.html" preload="./preload.js" />
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="refresh" content="5;URL=about:blank">
<script>
function logVisibilityState(ev) {
console.log('visibilityState', ev.type, document.visibilityState)
}
document.addEventListener('visibilitychange', logVisibilityState);
window.addEventListener('beforeunload', logVisibilityState);
window.addEventListener('unload', logVisibilityState);
</script>
</head>
<body>
<p>This webview will be unloaded in 5s.</p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment