【发布时间】:2018-09-14 18:30:25
【问题描述】:
我正在使用 Electron 2.0.9 完成 Electron 快速入门,我只是想找到一种方法让 webview 标签在沙盒模式下运行。
但是我似乎无法解决这个问题。我已经搜索了可能的解决方案,但我遇到的唯一问题是here,如图所示,这个问题已关闭,并且一年多前合并了一些东西。很明显,那将是在 2.0.9 中,我不会遇到这个问题。
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Sandbox Test</title>
<style>
webview {
width: 100%;
height: 500px;
border: solid 1px black;
}
</style>
</head>
<body>
<input id="urlInput" type="url" value="https://www.google.com/" placeholder="Enter Url">
<button onclick="setUrl();">Load Page</button>
<br>
<div id="webviewDiv">
</div>
<script>
// You can also require other files to run in this process
// require('./renderer.js');
function setUrl()
{
url = document.getElementById("urlInput").value;
document.getElementById("webviewDiv").innerHTML = '<webview src="' + url + '"></webview>';
}
</script>
</body>
</html>
main.js
// Modules to control application life and create native browser window
const {app, BrowserWindow} = require('electron')
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow
function createWindow () {
// Create the browser window.
mainWindow = new BrowserWindow({
webPreferences: {
sandbox: true
},
width: 800,
height: 600
})
// and load the index.html of the app.
mainWindow.loadFile('index.html')
// Open the DevTools.
// mainWindow.webContents.openDevTools()
// Emitted when the window is closed.
mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null
})
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)
// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow()
}
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
package.json
{
"name": "electron-quick-start",
"version": "1.0.0",
"description": "A minimal Electron application",
"main": "main.js",
"scripts": {
"start": "electron . --enable-mixed-sandbox"
},
"repository": "https://github.com/electron/electron-quick-start",
"keywords": [
"Electron",
"quick",
"start",
"tutorial",
"demo"
],
"author": "GitHub",
"license": "CC0-1.0",
"devDependencies": {
"electron": "2.0.9"
}
}
因此,如果有人对这种情况的可能原因有任何想法,或者可以指出我在尝试解决此问题时不知何故错过了一些事情,请告诉。
操作系统 - Windows Server 2008 R2(使用与 Windows 7 相同的内核)(electron 中的其他一切似乎都正常工作,所以我怀疑它是操作系统。
【问题讨论】:
-
无论如何,您可能都想看看这个问题。通常在大型项目中,完全相同的错误可以重新引入多次。
-
我试用了 3.0.0 12 测试版,它似乎工作正常,但不管我做什么,我似乎无法在 2.0.9 中得到任何东西。也许它已合并到回购中,但直到后来才发布。 在输入此内容时,我决定查看所有更改 electronjs.org/releases#3.0.0-beta.3 whelp 我猜我的理论是正确的,他们合并了修复程序,然后在一年多后将其推出 3.0.0 beta 3与我原帖中的 PR 相比。