How to access the node api from a remote page in electron?


#1

In my electron app , I load the remote page(index.html) from remote url like(“http://xxxx/index.html”), then I am trying to send a ipc event to the main process then handle it , and failed, but if I put the the index.html in local fs and it is OK.

So my question is how to enable the page to access the node api (like require,ipc, etc.) from the remote page.

------ (main process)

  mainWindow = new BrowserWindow({width: 800, height: 600});
  // and load the index.html of the app.
  mainWindow.loadUrl('http://localhost:8080');
  //mainWindow.loadUrl('file://' + __dirname + '/index.html');
var ipc = require('ipc');
  ipc.on('spawn-ext-process', function () {
  console.log("spawn-ext-process");
});

--------- http://localhost:8080/index.html (render process)

<script>
    var ipc = require('ipc');
    ipc.send('spawn-ext-process');
</script>

#2

From what I understand, if you host your files outside of Electron, you won’t be able to access the Electron APIs: