Create a script in a file for Electron

94 Views Asked by At

i have installed electron in global mode and i want to use it to run little script with GUI contained in one file. (I associated the *.js files in windows 10 to be opened with electron.) I wrote this file:

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() {

  var html = `<html>
    <head>
      <title></title>
      <style>
      body {
        background-color: whitesmoke;
      }
      </style>
    </head>
    <body>
      Input1: <input id="ia"><br><br>
      Input2: <input id="ib"><br><br>
      Input3: <input id="ic" type="number"><br><br>
      <button onclick="f()">Ok</button><br><br>
      <div id="output"></div>
      <script>
      function f() {
        var i1 = document.querySelector("#ia").value
        alert(i1)
       }
      </script>
    </body>
  </html>`
  var win = new BrowserWindow({
    width: 450,
    height: 350,
    title: 'scriptElectron',
    show: false
  })
  win.setMenu(null);
  win.loadURL(`javascript:document.body.innerHTML = '${html}'`)
  
  win.show()
}

app.on('ready', createWindow)

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()
  }
})

but javascript doesn't run. If in the definition of "f" i write only "alert("")" i see the alert dialog but with var i1 = document.querySelector("#ia").value it doesn't work. How can i solve this problem?

0

There are 0 best solutions below