Skip to content

Instantly share code, notes, and snippets.

@tarruda
Created June 2, 2017 13:14
Show Gist options
  • Save tarruda/947fe6509b8c47a6dbf76866b3278f40 to your computer and use it in GitHub Desktop.
Save tarruda/947fe6509b8c47a6dbf76866b3278f40 to your computer and use it in GitHub Desktop.
electron sandbox webview test
<html>
<body>
<webview id="foo" allowpopups sandbox="true" preload="preload-webview.js" src="index.html" style="display:inline-flex; width:640px; height:480px"></webview>
<script type="text/javascript" charset="utf-8">
</script>
</body>
</html>
const path = require('path')
const {BrowserWindow, ipcMain, app} = require('electron')
const preload = path.join(__dirname, 'preload.js')
const index = path.join(__dirname, 'index.html')
app.on('ready', () => {
w = new BrowserWindow({
webPreferences: {
sandbox: true,
preload: preload
}
})
w.loadURL('file://' + index)
ipcMain.on('preload-message', (e, msg, winOpenStr) => {
const isSandboxed = winOpenStr === 'function open() { [native code] }'
console.log(`message from renderer: "${msg}", is sandboxed: ${isSandboxed}`)
if (/hello from <webview>/.test(msg) && /file/.test(e.sender.getURL())) {
const url = 'https://google.com'
console.log(`loading ${url} on <webview>`)
setTimeout(() => {
e.sender.loadURL(url)
}, 2000)
}
})
})
{
"name": "webview-sandbox-demo",
"version": "1.0.0",
"description": "A demo app for using webview with sandbox",
"main": "main.js",
"scripts": {
"start": "electron ."
}
}
const {ipcRenderer} = require('electron')
ipcRenderer.send('preload-message', `hello from <webview>(${window.location.toString()})`, window.open.toString())
const {ipcRenderer} = require('electron')
process.enableWebViewTag()
ipcRenderer.send('preload-message', `hello from main window(${window.location.toString()})`, window.open.toString())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment