Skip to content

Instantly share code, notes, and snippets.

@ckerr
Created July 14, 2020 16:37
Show Gist options
  • Save ckerr/4c3d501c4058bd6b600f1c2434c8a57c to your computer and use it in GitHub Desktop.
Save ckerr/4c3d501c4058bd6b600f1c2434c8a57c to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
(() => {
const byId = id => document.getElementById(id)
const post = name => window.electron.ipcRenderer.send(name)
window.onload = () => {
const btnCreate = byId('create')
byId('heap-snapshot').onclick = () => { post('heap-snapshot') }
byId('open-wv-devtools').onclick = () => { post('wv-devtools') }
}
})()
</script>
</head>
<body>
<div>
<h1>Main Window</h1>
<button id="heap-snapshot">save heap snapshot</button>
<button id="open-wv-devtools">open webview devtools</button>
<webview src="https://office.com/" style="height: 600px; border: 1px solid gray;" preload="./preload.js" webpreferences="nativeWindowOpen=yes, sandbox=yes" />
</div>
</body>
</html>
const { app, BrowserWindow, webContents, ipcMain } = require('electron')
const path = require('path')
const { statSync } = require('fs')
let webViewWC = {}
let mainWindow = null
const createWindow = () => {
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
webSecurity: false,
sandbox: true,
nativeWindowOpen: true,
additionalArguments: ['--foobar'],
preload: path.join(__dirname, './preload.js'),
webviewTag: true
}
})
mainWindow.loadURL(`file://${__dirname}/main.html`)
mainWindow.on('closed', () => mainWindow = null)
mainWindow.webContents.on('did-attach-webview', (e, webContents) => {
ipcMain.on('wv-devtools', () => { webContents.openDevTools() })
ipcMain.on('heap-snapshot', () => {
webViewFilePath = path.join(__dirname, `/webview-snap.heapsnapshot`)
const start = Date.now()
console.log('Taking V8 HeapSnapshot')
webContents.takeHeapSnapshot(webViewFilePath)
.then(() => {
const stats = statSync(webViewFilePath)
const fileSizeInMegabytes = stats["size"] / 1000000
console.log(`WebView V8 HeapSnapshot taken successfully, size: ${fileSizeInMegabytes}MB, execution time: ${((Date.now() - start)/1000).toString()}`)
})
.catch(err => { console.log(err) })
})
})
};
app.on('ready', createWindow)
app.on('window-all-closed', () => process.platform !== 'darwin' && app.quit())
app.on('activate', () => mainWindow === null && createWindow())
{
"main": "main.js"
}
window.electron = {
ipcRenderer: require('electron').ipcRenderer,
webFrame: require('electron').webFrame
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment