Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
electron sandbox webview test
<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">
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 = ''
console.log(`loading ${url} on <webview>`)
setTimeout(() => {
}, 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()})`,
const {ipcRenderer} = require('electron')
ipcRenderer.send('preload-message', `hello from main window(${window.location.toString()})`,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.