sureone
sureone

Reputation: 851

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

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>

Upvotes: 4

Views: 2533

Answers (2)

ccnokes
ccnokes

Reputation: 7105

How to enable the page to access the node api (like require,ipc, etc.) from the remote page?

Depends on how you're loading the page. If you're using loadUrl on a BrowserWindow instance, node integration is enabled by default (you can disable it via the nodeIntegration option, see the docs).

If you're using a <webview> tag, then it's disabled by default and you can enable it via the nodeintegration attribute. See the docs. Note that with webview you can preload scripts that do have access to node and electron APIs safely, and then access to those objects are destroyed when the script has finished executing (see here).

Like @FelipeBrahm said, I would not give a remote page access to node.js or electron APIs unless it's your own page, and even if you do that, be cautious.

Upvotes: 3

Josh
Josh

Reputation: 3345

AFIAK the ipc api doesn't use http. It essentially is sending a signal from one part of the program running on your local machine to another part of the same program on that machine.

If you want to send a signal over http you might want to use something like socket.io or websockets.

Upvotes: 3

Related Questions