Jamie T Jamie T - 7 months ago 89
Javascript Question

Parent view not setting global variable on <webview> in Electron after updating

I am trying to require a module in a guest page (angular controller).

When the application used an older version of node (0.30.5) it worked perfectly well. However i've had to upgrade electron to the latest version (0.37.6), and ive updated all uses of

ipc
in the electron application, so that it's now
ipcMain
and
ipcRenderer
.

Previously the angular controller managed to require the module using:

var ipc = window['require']('ipc');


So i thought that changing it to:

var ipcRenderer = window['require']('electron').ipcRenderer;


However that does not work and it states that window.require is not a function. I've tried multiple times to get it to work with other methods, however nothing works so far.

The problem is that require is undefined, however require is set in the html file that contains the
<webview>
, so it should be defined.

The
<webview>
goes to a URL which returns a
<div>
that uses angular.

The angular controller that is used in that
<div>
is no longer working, as the module I need is the first thing to be set in the file, and it is undefined

EDIT:

I can now see that the property that I set in the html is visible in the application debugger, however it does not appear in the webview debugger.

So I can only assume the parent view does not pass on the value to the
<webview>
. So if anyone knows why it doesn't pass on the value or what I need to do to set it, I would appreciate any information.

Answer

So as of the current version of Electron (0.37.6) the way to set a global variable is to specify a preload script on the preload attribute on the <webview> tag. For example in the preload script:

window['ipcRenderer'] = require('electron').ipcRenderer;

This will allow the guest page that the <webview> is displaying, to use the ipcRenderer. It seems like the previous method i was using, which was setting the global variable in the parent view HTML, does not work anymore

Comments