You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I think it would be cool if we allowed customization of default dialogs (alert, confirm and prompt), by registering a handler. So for example if I want my alerts to have a custom title and icon, I could do something like this:
You could even translate the title, labels etc. (see #2518 for a related issue).
If we will have support for default "confirm navigation" dialog (see #2481), it will be great if we can customize that too.
The handlers should probably always use dialog.showMessageBox, because these dialogs are supposed to be modal and synchronous. So maybe it would make more sense for the handlers to just return the options parameter for dialog.showMessageBox. For example:
To be fair, you could accomplish some of these stuff by hooking the methods or using custom methods. That said, I think it's pretty common to want to customize them and Electron as a framework for building applications should expose an API for that.
Some users are running multi-apps through a single app. They basically just open new windows for each app from a single master process (see for example #2415 and #2438). So I think it's better to leave the configuration on the BrowserWindow which will give the option to configure the look differently per window (e.g. they can set different dialog title per window). If you want to set the same options for all windows, you could still do that by wrapping the window creation with code to set up the options.
The more I think about it, I don't see a reason to treat this like setting an event handler. We just want to allow customization of the modal dialog, which means the options used to create it. So maybe something like this will be better:
When an alert is called, the modal dialog creation code, will check if the developer supplied an options object and if so it will customize the dialog according to it and then display it.
This approach seems simpler and cleaner to me.
There are requirements other than just changing the look and feel of dialogs, e.g.:
Block dialogs from all or some pages, according to user's setting.
Fill authentication dialogs with saved usernames/passwords.
Additionally, one may want to set the title/icon of dialogs according to the title/favicon of the web page that triggered it.
I think the event handler approach provides all the flexibility. We may add properties such as browserWindow (maybe window too for iframes) to the event object, so that developer can get all context info about the dialog to be shown.
There is one difference between native and custom alert – custom one is not blocking, although it's better UX in most cases, might be a problem for some.
@alexindigo, what do you mean when you say that custom alert doesn't block?
If you implement it using dialog.showMessageBox(), just don't provide a callback and the call will be synchronous/blocking. For alert it doesn't matter that match, but confirm must be synchronous because you need to return the result to the caller.
@zcbenz preload script looks awesome, but how about browsing contexts that are not created by new BrowserWindow(...), e.g. window.open(), <a target="_blank">, <iframe>, etc.?
@duanyao Good question, I can't find a existing way to solve that. One possible solution is allow setting preload script for the whole session, so ever page created for the session will load the preload script:
Another (more general) solution is "browsing context life circle events", so you can inject different scripts to different pages, at different loading states (e.g. before parsing HTML, ondomcontentloaded, or onload). One example is Geckofx, it has Navigating, Navigated, DocumentCompleted, CreateWindow events, and you can retrieval the URL and call EvaluateScript() to inject scripts in the handlers.
I noticed that Electron class WebContents has did-start-loading, did-frame-finish-load, new-window events and executeJavaScript(), seems potentially being able to achive this purpose?
I noticed that Electron class WebContents has did-start-loading, did-frame-finish-load, new-window events and executeJavaScript(), seems potentially being able to achive this purpose?
We still need to add all those events to Session though, otherwise we can still not handle pages opened by window.open.
Currently there's no support for alert and confirm in iframes. The preload script for Sessions sounds like a good solution for customizing the dialogs, but I think we also need a default preload (kind of like what override.coffee does for the parent window).
Will this work even when websecurity is set to true and the child window(s) belongs to a different origin?
Activity
duanyao commentedon Aug 18, 2015
I think it is even better to be able to intercept dialogs globally, so you don't have to change every site that creates
BrowserWindow:etiktin commentedon Aug 19, 2015
Some users are running multi-apps through a single app. They basically just open new windows for each app from a single master process (see for example #2415 and #2438). So I think it's better to leave the configuration on the
BrowserWindowwhich will give the option to configure the look differently per window (e.g. they can set different dialog title per window). If you want to set the same options for all windows, you could still do that by wrapping the window creation with code to set up the options.The more I think about it, I don't see a reason to treat this like setting an event handler. We just want to allow customization of the modal dialog, which means the options used to create it. So maybe something like this will be better:
When an alert is called, the modal dialog creation code, will check if the developer supplied an options object and if so it will customize the dialog according to it and then display it.
This approach seems simpler and cleaner to me.
duanyao commentedon Aug 20, 2015
There are requirements other than just changing the look and feel of dialogs, e.g.:
Additionally, one may want to set the title/icon of dialogs according to the title/favicon of the web page that triggered it.
I think the event handler approach provides all the flexibility. We may add properties such as
browserWindow(maybewindowtoo for iframes) to the event object, so that developer can get all context info about the dialog to be shown.zcbenz commentedon Aug 25, 2015
For overriding the
alertbox you can simply do it in preload script:That's also how we implemented
alertin Electron:https://github.com/atom/electron/blob/master/atom/renderer/lib/override.coffee#L78-L88
alexindigo commentedon Aug 25, 2015
There is one difference between native and custom
alert– custom one is not blocking, although it's better UX in most cases, might be a problem for some.etiktin commentedon Aug 25, 2015
@alexindigo, what do you mean when you say that custom
alertdoesn't block?If you implement it using
dialog.showMessageBox(), just don't provide a callback and the call will be synchronous/blocking. Foralertit doesn't matter that match, butconfirmmust be synchronous because you need to return the result to the caller.alexindigo commentedon Aug 25, 2015
@etiktin Hey, sorry for the confusion, I missed that part completely. Thank you for the clarification.
duanyao commentedon Aug 26, 2015
@zcbenz preload script looks awesome, but how about browsing contexts that are not created by
new BrowserWindow(...), e.g.window.open(), <a target="_blank">, <iframe>, etc.?zcbenz commentedon Aug 26, 2015
@duanyao Good question, I can't find a existing way to solve that. One possible solution is allow setting preload script for the whole session, so ever page created for the session will load the preload script:
How does it sound?
duanyao commentedon Aug 26, 2015
Session may work.
Another (more general) solution is "browsing context life circle events", so you can inject different scripts to different pages, at different loading states (e.g. before parsing HTML, ondomcontentloaded, or onload). One example is Geckofx, it has
Navigating,Navigated,DocumentCompleted,CreateWindowevents, and you can retrieval the URL and callEvaluateScript()to inject scripts in the handlers.I noticed that Electron class
WebContentshasdid-start-loading,did-frame-finish-load,new-windowevents andexecuteJavaScript(), seems potentially being able to achive this purpose?zcbenz commentedon Aug 26, 2015
We still need to add all those events to
Sessionthough, otherwise we can still not handle pages opened bywindow.open.duanyao commentedon Aug 26, 2015
Doesn't
new-windowevent report windows created bywindow.open?etiktin commentedon Aug 26, 2015
Currently there's no support for
alertandconfirmin iframes. Thepreloadscript forSessionssounds like a good solution for customizing the dialogs, but I think we also need a default preload (kind of like what override.coffee does for the parent window).Will this work even when
websecurityis set to true and the child window(s) belongs to a different origin?zcbenz commentedon Aug 27, 2015
Yeah preload script is loaded regardless of web security, and child windows always have the same session.