Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Electron - Processing Input

I recently started to get my feet wet with Electron. I really like the principles behind it but I find it a little confusing to do some things.

For example, how do you process user input? I've an main.js and a BrowserWindow pointing to a local html file (containing some user settings with input field).

How do I access this data when the HTML form is submitted (to either the same file or another one)?

main.js

    const {app, BrowserWindow} = require('electron')
let win

function createWindow () {
  win = new BrowserWindow({width: 800, height: 600})
  win.loadURL('file://' + __dirname + '/index.html')

  // Emitted when the window is closed.
  win.on('closed', () => {
    win = null
  })

  // Open the DevTools.
  // win.webContents.openDevTools()
}

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', () => {
  if (win === 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.

//Start the main window
app.on('ready', createWindow)

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <form action="" method="post">
        <input type="text" name="test-1">
    </form>
</body>
</html>
like image 355
Friso Kluitenberg Avatar asked Oct 28 '16 14:10

Friso Kluitenberg


People also ask

What is Electron process?

Electron takes a main file defined in your package. json file and executes it. This main file creates application windows which contain rendered web pages and interaction with the native GUI (graphical user interface) of your Operating System. As you start an application using Electron, a main process is created.

How does IPC work in Electron?

The ipcMain module is used to communicate asynchronously from the main process to renderer processes. When used in the main process, the module handles asynchronous and synchronous messages sent from a renderer process (web page). The messages sent from a renderer will be emitted to this module.

Where is Electron main process?

Each Electron app has a single main process, which acts as the application's entry point. The main process runs in a Node. js environment, meaning it has the ability to require modules and use all of Node.

What is main process and renderer process in Electron?

Electron uses multi-process architecture to manage the application state and user interface. The main process controls the state of the application while the renderer process controls the user interface. Electron uses GPU process to perform graphically intensive tasks but it's optional.


Video Answer


1 Answers

With Electron, node.js is not acting as a webserver with routes like it would be in a typical web application scenario. Instead of sending requests to routes, you would create a single page application using a javascript framework like Angular, React, Knockout, etc. At that point, you no longer need to handle routing. You would tie your 'Submit' click event to a javascript function directly within the page, and process the input from there.

You can do everything from the page's javascript context that you can do from the node.js main process context. For instance, if you needed to access the file system from your page, you would use the Remote module to gain access to the node.js native APIs.

For example:

// Gain access to the node.js file system api
function useNodeApi() {
  const remote = require('electron').remote;
  const fs = remote.require('fs');
  fs.writeFile('test.txt', 'Hello, I was written by the renderer process!');
}

I've rarely come across a situation where I needed to pass control back to the main process to accomplish something. Once the BrowserWindow launches, anything you could ever need to do could be done from the renderer process. This pretty much eliminates the need to do things like submit form posts via http.

like image 62
Dennis W Avatar answered Oct 17 '22 09:10

Dennis W