Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Electron ipc using remote website?

I'm creating a electron BrowserWindow using a remote url, so I can't really use the var ipc = require('ipc'); syntax to include ipc. It is possible to send messages from a remote url to the electron main-process? If so where can I get the javascript source for it?

Or maybe there is a better way to pass info to electron main-process? Just need to send the logged in user info.

like image 431
Bill Johnston Avatar asked Nov 05 '15 04:11

Bill Johnston


People also ask

How do I enable remote module in Electron?

Note: In electron >= 14.0. 0 , you must use the new enable API to enable the remote module for each desired WebContents separately: require("@electron/remote/main"). enable(webContents) .

How does IPC work in Electron?

IPC channels​ In Electron, processes communicate by passing messages through developer-defined "channels" with the ipcMain and ipcRenderer modules. These channels are arbitrary (you can name them anything you want) and bidirectional (you can use the same channel name for both modules).

What is Electron remote?

About`@electron/remote` is an [Electron](https://electronjs.org) module that bridges JavaScript objects from the main process to the renderer process. This lets you access main-process-only objects as if they were available in the renderer process.


Video Answer


1 Answers

This question has been asked quite sometime back, but I think this could still be useful. The way you can inject Javascript into the browser window is using the preload lag, in the web preferences, add the options:

const window = new BrowserWindow({
                    webPreferences:{
                     preload: <path_to>/preload.js
                    }
                   })
// preload.js

const ipcRenderer = require('electron').ipcRenderer;
window.ipcRenderer = ipcRenderer

In the webpage of you the external url you are loading you can directly use window.ipcRenderer.

like image 175
Itachi Avatar answered Sep 21 '22 00:09

Itachi