Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cordova-Electron : Is there a way to add preload.js script and disable node-integration?

In the Apache Cordova doc (https://cordova.apache.org/docs/en/10.x/guide/platforms/electron/index.html), it is suggested to set "nodeIntegration" to true in settings.json file to use NodeJs and Electron API in the renderer part of my app. However, in the electron doc, this action is not recommended for security reasons, and it is suggested to use a preload script to do so.

Is it possible to do the same with cordova-electron ? Something like this doesn't seems to work :

{
"browserWindow": {
    "width": 1024,
    "height": 768,
    "fullscreen": false,
    "frame": false,
    "webPreferences": {
        "nodeIntegration": false,
        "preload": "file://{Directory}/preload.js",
        "contextIsolation": true
    }
}

The "preload" option isn't read. I'm using cordova 10, with cordova-electron 2.0.

Thanks in advance for any advice. :)

like image 404
Valentin Avatar asked Dec 12 '25 02:12

Valentin


1 Answers

According to the Electron documentation we have to use absolute path for the preload and file:// protocol is considered as a non-absolute. If you change the path to a really absolute one the app will try to preload it. The problem here is defining dynamic path in that json configuration. As far as I can see it couldn't be set using __dirname directive, so the easiest way at least for now is using nodeIntergration: true in cordova-electron build

like image 83
the_will Avatar answered Dec 15 '25 14:12

the_will