Created
June 2, 2017 13:14
-
-
Save tarruda/947fe6509b8c47a6dbf76866b3278f40 to your computer and use it in GitHub Desktop.
electron sandbox webview test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
}) | |
}) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "webview-sandbox-demo", | |
"version": "1.0.0", | |
"description": "A demo app for using webview with sandbox", | |
"main": "main.js", | |
"scripts": { | |
"start": "electron ." | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const {ipcRenderer} = require('electron') | |
ipcRenderer.send('preload-message', `hello from <webview>(${window.location.toString()})`, window.open.toString()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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