Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preload script not run with nativeWindowOpen=true and window.open('') #18070

Closed
3 tasks done
jeremyspiegel opened this issue Apr 30, 2019 · 5 comments
Closed
3 tasks done

Comments

@jeremyspiegel
Copy link
Contributor

jeremyspiegel commented Apr 30, 2019

Preflight Checklist

  • I have read the Contributing Guidelines for this project.
  • I agree to follow the Code of Conduct that this project adheres to.
  • I have searched the issue tracker for an issue that matches the one I want to file, without success.

Issue Details

  • Electron Version:
    • 5.0.0
  • Operating System:
    • macOS 10.14.4
  • Last Known Working Electron version::
    • 4.1.5

Expected Behavior

I would like to open a new child window and render content into it from script running in my mainWindow, rather than perform a navigation in the new child window. This works when setting nativeWindowOpen: true for the main window and using window.open('') to open a window with location about:blank.

I would also like to load a preload script in the new window, so that I can configure the webFrame. In 4.1.5, setting a preload script on the main window caused the preload script to also be loaded for child windows. In 5.0.0, the preload script stopped loading for the child window.

I would actually expect that providing a preload script in the new-window event would work, as described here and pointed out here by @MarshallOfSound, but it doesn't. I've tried various ways of using the options that are passed into the event, but none have worked:

mainWindow.webContents.on('new-window', (event, url, frameName, disposition, options) => {
  event.preventDefault();
  Object.assign(options, {
    webPreferences: {
      preload: `${__dirname}/preload.js`
    }
  });
  event.newGuest = new BrowserWindow(options);
});

and

mainWindow.webContents.on('new-window', (event, url, frameName, disposition, options) => {
  event.preventDefault();
  Object.assign(options.webPreferences, {
    preload: `${__dirname}/preload.js`
  });
  event.newGuest = new BrowserWindow(options);
});

and

mainWindow.webContents.on('new-window', (event, url, frameName, disposition, options) => {
  event.preventDefault();
  event.newGuest = new BrowserWindow({
    webPreferences: {
      preload: `${__dirname}/preload.js`
    }
  });
});

Actual Behavior

There doesn't seem to be a way to load a preload script into a window opened via window.open('') in electron 5.0.0. I'd like some way to get that to work, whether that is by setting the preload script on the main window as worked in previous versions, or making it work when setting it in the new-window event.

To Reproduce

Here is a commit that demonstrates the preload script loaded properly when run with electron 4.1.5, but not loaded when run with electron 5.0.0, and here is the branch:

$ git clone git@github.com:jeremyspiegel/electron-quick-start.git -b preload-not-run-on-window-open
$ npm install
$ npm start || electron .

Additional Information

I think this might be related to #17989 or #16224, but those issues actually involve a window.open with a navigation, while my issue doesn't (if I open a window that performs a navigation, the preload script does still run for me in 5.0.0).

I tried removing the early exit from

// Do not load node if we're aren't a main frame or a devtools extension
// unless node support has been explicitly enabled for sub frames
bool is_main_frame =
render_frame->IsMainFrame() && !render_frame->GetWebFrame()->Opener();
as suggested in #17989 (comment) but that didn't fix the issue either.

@alexstrat
Copy link
Contributor

alexstrat commented May 4, 2019

@jeremyspiegel I was able to have preload scripts running with the suggestion #17989 (comment). If you have a chance to try #18152 to verify that.

@alexstrat
Copy link
Contributor

After some digging, I realized it is an intended behavior after #15213 but that you can get over it by using nodeIntegrationInSubFrames:

--- a/main.js
+++ b/main.js
@@ -12,6 +12,7 @@ function createWindow () {
     height: 600,
     webPreferences: {
       nativeWindowOpen: true,
+      nodeIntegrationInSubFrames: true,
       nodeIntegration: true,
       preload: `${__dirname}/preload.js`
     }

See #18156

@jeremyspiegel
Copy link
Contributor Author

@alexstrat thanks for the clarification, that worked for me!

Is it known what caused the crashes when releasing node environments that required the change to disable nodeIntegration by default (#15076)? I'm wondering if there will be a way to have preload scripts run in new windows without having the memory leak.

@alexstrat
Copy link
Contributor

alexstrat commented May 6, 2019

🤷‍♂️I don't know exactly.

I think at the time of #15076 no one had figured out how to have several node environments in 1 render process, but, later, MarshallOfSound figured it out in #16425.

Reminder to close the issue.

@jeremyspiegel
Copy link
Contributor Author

OK sounds good, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
5.0.x
Unsorted Issues
Development

Successfully merging a pull request may close this issue.

3 participants