feat: add webContents.setWindowOpenHandler API #24517
Conversation
This comment has been hidden.
This comment has been hidden.
Sorry, something went wrong.
Awesome stuff, love the diagram
patches/chromium/allow_in_process_windows_to_have_different_web_prefs.patch
Outdated
Show resolved
Hide resolved
A couple tests are still failing, but I'm going to get the review ball rolling while I address them. |
I'm in favor of approach (4). Mutating the event feels clunky even if there is precedence to do so. I also like the declarative nature of the return object on (4). |
The API WG approved this PR at our Nov 9, 2020 meeting |
Release Notes Persisted
|
Is it now possible to set |
@nornagon Is there a way to influence how the new window is created? Specifically, we are looking at how to open into a BrowserView |
@loc @nornagon Just wanted to share an idea that @sentialx and I discussed to add support for browser views. We tried to think of a way to do this without changing too much about the new
I believe there might be an outstanding issue re: 'close'? Eryk was going to think about this a bit more and refine the idea, but I wanted to get some initial feedback on the above. |
@stewartlord hm, that proposal raises a few concerns for me, specifically it seems difficult to correctly correlate the window open handler invocation with the However, I like the direction this is going in and to collect discussion I'd encourage you to open a proposal over at https://github.com/electron/governance/tree/master/wg-api/spec-documents (see https://github.com/electron/governance/blob/master/wg-api/spec-documents/api-spec-template.md for a template) |
Is there a way with this new paradigm to pend window creation? I only want to create the window if the URL is in my allowlist (whitelist). Currently, I'm preventing the Is the way to do this using the new paradigm to create the window in a hidden state, then after it's been created, check with the allowlist and close the window if the URL doesn't pass the check; otherwise, show the window? I presume I can do that bit in |
There is not. Chromium code forces that a decision about whether to create the window or not be made synchronously. |
Switch from event 'new-window' to function 'setWindowOpenHandler()'. For more information, see: electron/electron#24517
API changes:
Deprecate webContents
new-window
event in favor ofwebContents.setWindowOpenHandler(handler)
anddid-create-window
new-window
is fired after the contents is already created by Chrome. This is a problem when the application wants to prevent that window from being opened, as killing the window during initialization causes many problems and leads to the inevitable crash. Instead, we'll allow cancellation/customization of the new window before it's created. In congress with a change to set WebPreferences via IPC instead of the command line, this allows us to completely* change the child window's BrowserWindowConstructorOptions, even for in-process windows.*With the exception of a few outstanding preferences that we need early in initialization, like
offscreen
andnodeIntegrationInWorker
. These could be fixed with some effort.Old way:
New way:
(internal API)
webFrame.getWebPreference(prefName)
Previously, preferences were stuffed into command line switches so that the renderer could look and see what settings it was supposed to be using. However, this wouldn't work for in-process child windows. Instead, we'll stuff Electron preferences into the struct that Chrome already uses for this sort of thing and add an API for the renderer to retreive those preferences. At the moment, only Electron-specific WebPreferences are available for lookup, but it would be easy to add Chromium-defined prefs like defaultFontSize, for example.
Implementation details:
Allowing for in-process WebPreference changes
Prior to this change, WebPreferences were set via the command line, which meant that in-process child windows were forced to inherit WebPreferences from their parent. Now, we overload the Chrome WebPreference struct with our own properties so they are accessible in the renderer (via a new
webFrame.getWebPreference
).Some behavior changes that make window opening less, um, surprising.
features
keys without values are now interpreted astrue
, to w3c spec.Checklist
npm test
passesRelease Notes
Notes: Add setWindowOpenHandler API for renderer-created child windows, and deprecate new-window event.
The text was updated successfully, but these errors were encountered: