Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a function/module in Electron from my webpage?

I'll try to describe a minimized question in short paragraphs.

In short, I want to use some logic or call some functions in my Electron App from the webpage that is in my Electron App (I am actually wrapping an electron app 'shell' for my webpage).

Suppose I want to expose a function in my Electron app. Say,

function printNumbers () {
  console.log(1)
}

notice that it should be located in my Electron code.

Then after running my app, I'd like to call this function from my webpage(clicking a button in my webpage which is loaded from a website, then open a new window in my Electron App). For now, I think I can check whether printNumber works by using the developer console.

I have checked how to use remote module to call functions/modules inside electron. But I didn't find a way to call a function I write in my electron code base.

BTW: I can enable the nodeIntegration option.

like image 532
Kulbear Avatar asked Jan 24 '17 03:01

Kulbear


People also ask

Is it possible to call a function from another module?

Yeah this is possible although I am not 100% on the syntax, you could just try calling the function in module 2 from the module you like, thats how VB does it (VBA does it too), it's like the functions in modules are global functions. Or you can preceed the call like this Module2.Function (param). I think both these approaches work.

What is the main process of an electron app?

The main process is the backbone of an Electron App. It can spawn multiple child processes (also called the renderer process). How? We will see that in a minute. Let’s quickly set up a react app with Electron. It is assumed that you have node and npm installed. Create a folder named electron-app. Open the folder in your favorite code editor.

What is electron and how does it work?

It ensures that the heavy I/O and CPU-bound operations are put onto the new threads that would avoid blocking the UI ( main process ). Electron ships with the latest version of Chrome. Its powerful features could be used to orchestrate heavy computations in an application window ( renderer process) that would enable the app to run at 60fps. Wait!

How do I run electron in NPM?

Each BrowserWindow runs its isolated renderer process and gets destroyed when the BrowserWindow is closed. When we run the npm run electron-dev (runs electron . under the hood) command, the main process starts and initializes the Electron Environment. Next, the main process looks for the main entry in the package.json file and runs the main file.


1 Answers

There are two main ways to communicate between the renderer process and the main process.

1. One way would be to use the remote module to require the code you want to take from the main process. This object will contain anything you export from your main process code.

// main process, for example app/main.js
exports.test = () => console.log('Yay');

// renderer process, for example app/renderer.js
const { remote } = require('electron');
const mainProcess = remote.require('./main.js');

mainProcess.test(); // 'Yay'

2. Another way would be to use Inter Process Communication to send/receive events between the main process and the renderer process:

// main process, for example app/main.js
myWindow.webContents.send('my-cool-log-event', 'Yay');

// renderer process, for example app/renderer.js
const { ipcRenderer } = require('electron');
ipcRenderer.on('my-cool-log-event', (evt, msg) => console.log(msg)); // 'Yay'

If you want to call a function from the main process when a click event fires in a renderer process, you can use either approach.

1.

// main process, for example app/main.js
exports.onClick = () => console.log('Yay');

// renderer process, for example app/renderer.js
const { remote } = require('electron');
const mainProcess = remote.require('./main.js');

document
  .querySelector('#elem')
  .addEventListener('click', () => {
    mainProcess.onClick();
  });

2.

// main process, for example app/main.js
const { ipcMain } = require('electron')
ipcMain.on('click', () => console.log('do something'));

// renderer process, for example app/renderer.js
const { ipcRenderer } = require('electron');

document
  .querySelector('#elem')
  .addEventListener('click', () => {
    ipcRenderer.send('click');
  });
like image 124
nem035 Avatar answered Oct 03 '22 13:10

nem035