Search code examples
electronsandbox

Webview not rendering when Electron's sandbox is set to true


I am working off the Electron quickstart using Electron 2.0.9 and am just trying to find a way to get a webview tag to run in Sandbox mode.

However I can't seem to remedy this issue. I have searched for possible solutions but the only thing I came across was here and this issue as shown was closed and there was something merged over a year ago. So clearly that'd be in 2.0.9 and I wouldn't be having this issue.

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"
  }
}

So if anyone has any idea on possible reasons as to this being like this or can point to something I somehow missed on my attempts to fix this please do tell.

OS - Windows Server 2008 R2 (uses the same Kernel as Windows 7) (everything else in electron seems to work like normal so I doubt it's the OS.


Solution

  • It seems that the ability to use tag in sandbox didn't work until the 3.0.0 beta 3 release. Even though as in my previous post there seems to have been a merged change from over a year before the 3.0.0 beta 3 was released. Maybe this is somehow related to semantic versioning in someway so they had to wait until the next major release to add support for it.

    https://electronjs.org/releases#3.0.0-beta.3