electron: Preload script is not loaded for a window created with window.open

  • Output of node_modules/.bin/electron --version: v4.0.0
  • Operating System (Platform and Version): Windows 10
  • Output of node_modules/.bin/electron --version on last known working Electron version (if applicable): 1.8.8 Started to fail since 2.x versions.

Expected Behavior Preload script should be loaded also for a window created with window.open and nativeWindowOpen: true, nodeIntegration: false I don’t see any warning about nodeIntegration.

Actual behavior Preload script is not loaded. I also see a warning about nodeIntegration enabled.

To Reproduce

$ git clone https://github.com/ficristo/electron-quick-start -b issues/16224
$ npm install
$ npm start || electron .
  • Open devtools and see the warning about nodeIntegration
  • Click the button on the first window
  • Open devtools of the new windows and see only an console.log. Try to type window.process to find it is undefined.

Try again with electron@1.8.8

  • Open devtools and see no warning about nodeIntegration
  • Click the button on the first window
  • Open devtools of the new windows and see a couple of console.log also related to the preload script. Try to type window.process to find it is available.

Screenshots N/A

Additional Information N/A

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 1
  • Comments: 18 (1 by maintainers)

Most upvoted comments

getting the same issue in electron version 20.3.11, where you able to find any solutions to this @hayderma @ficristo ?

I ended up using BrowserWindow (just like the one in main.js) instead of window.open, if you use this approach, which is not optimal, make sure to set parent as main window , this is the complete code for the function:

 function createChildWindow(data) {
    var childOpened=false;
    //i do not want child window opened more than once at a time, so i prevent future requests from creating more windows if one is already open
    BrowserWindow.getAllWindows().forEach(function (win) {
      console.log("window id:"+win.id);
      //the main/parent window will always have id of 1, so I know that is the main
      if (win.id > 1) {
       childOpened=true;
      }
     })
    console.log("child opened ="+childOpened);
    if(!childOpened){
      const childWin = new BrowserWindow({
       width: 800,
       height: 600,
       webPreferences: {
         nodeIntegration: true,
         enableRemoteModule: true,
         contextBridge: true,
         nativeWindowOpen: true,
         nodeIntegrationInSubFrames: true,
         parent:win, //this is the main app window created above looks something like this: win = new BrowserWindow({... make sure it is declared globally to be able to access it here
         preload: path.join(__dirname, 'preload.js')
       }
     })
     childWin.loadFile(data.url)
    }//end if

    else{
      console.log("child window opened already");
    }
  }

Also you can read more on this approach here

@electron-triage , how hard is it to make better documentation that actually can render a solution ? I have been all over this document , tried every single thing it is suggesting with no avail, it is a total contraction of itself. This is a problem that my newly opened window can not call electronAPI no more because of this contextIsolation feature, at this point most of the devs do not care about security, just want the app to work. As above for example @ficristo mentions that this can be resolved by setting contextIsolation: false , this is not even allowed in my app, once i do it then the contextBridge.exposeInMainWorld('electronAPI', { stops working and of course i get errors about it. why is this enforced, why can’t I choose to make my app less secure? And then as @thorsent mentions I can not use the function suggested in the documentation webContents.setWindowOpenHandler to set manually the preload for my new window, why because this does not work when contextIsolation is true. Then what is the solution, i have been trying to resolve for 3 days now, and I can’t rebuild this in a different framework as I got too involved in this app, but it is becoming clear to me why this is a very unpopular framework among devs.