Skip to content

Instantly share code, notes, and snippets.

@goldalworming
Forked from tarruda/index.html
Created November 18, 2018 09:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goldalworming/774923b425bc3a06212ac11ebd617b8a to your computer and use it in GitHub Desktop.
Save goldalworming/774923b425bc3a06212ac11ebd617b8a 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