diff --git a/CefSharp.Core/Internals/ClientAdapter.cpp b/CefSharp.Core/Internals/ClientAdapter.cpp index c3d7d4e6d..0d90ff4a4 100644 --- a/CefSharp.Core/Internals/ClientAdapter.cpp +++ b/CefSharp.Core/Internals/ClientAdapter.cpp @@ -455,14 +455,12 @@ namespace CefSharp return false; } - bool result; - bool handled = false; + bool allowUnload; - String^ resultString = nullptr; - handled = handler->OnJSBeforeUnload(_browserControl, StringUtils::ToClr(message_text), is_reload, resultString); + auto handled = handler->OnJSBeforeUnload(_browserControl, StringUtils::ToClr(message_text), is_reload, allowUnload); if (handled) { - callback->Continue(result, StringUtils::ToNative(resultString)); + callback->Continue(allowUnload, CefString()); } return handled; diff --git a/CefSharp.Example/CefSharp.Example.csproj b/CefSharp.Example/CefSharp.Example.csproj index c60d65e32..2c8d312fa 100644 --- a/CefSharp.Example/CefSharp.Example.csproj +++ b/CefSharp.Example/CefSharp.Example.csproj @@ -85,6 +85,7 @@ + diff --git a/CefSharp.Example/JsDialogHandler.cs b/CefSharp.Example/JsDialogHandler.cs new file mode 100644 index 000000000..df8536f82 --- /dev/null +++ b/CefSharp.Example/JsDialogHandler.cs @@ -0,0 +1,34 @@ +namespace CefSharp.Example +{ + public class JsDialogHandler : IJsDialogHandler + { + public bool OnJSAlert(IWebBrowser browser, string url, string message) + { + return false; + } + + public bool OnJSConfirm(IWebBrowser browser, string url, string message, out bool retval) + { + retval = false; + + return false; + } + + public bool OnJSPrompt(IWebBrowser browser, string url, string message, string defaultValue, out bool retval, out string result) + { + retval = false; + result = null; + + return false; + } + + public bool OnJSBeforeUnload(IWebBrowser browser, string message, bool isReload, out bool allowUnload) + { + //NOTE: Setting allowUnload to false will cancel the unload request + allowUnload = false; + + //NOTE: Returning false will trigger the default behaviour, you need to return true to handle yourself. + return false; + } + } +} diff --git a/CefSharp.WinForms.Example/BrowserTabUserControl.cs b/CefSharp.WinForms.Example/BrowserTabUserControl.cs index 28095a5b4..dbdd9b95b 100644 --- a/CefSharp.WinForms.Example/BrowserTabUserControl.cs +++ b/CefSharp.WinForms.Example/BrowserTabUserControl.cs @@ -25,6 +25,7 @@ namespace CefSharp.WinForms.Example browser.MenuHandler = new MenuHandler(); browser.RequestHandler = new RequestHandler(); + browser.JsDialogHandler = new JsDialogHandler(); //browser.FocusHandler = new FocusHandler(browser, urlTextBox); browser.NavStateChanged += OnBrowserNavStateChanged; browser.ConsoleMessage += OnBrowserConsoleMessage; diff --git a/CefSharp/IJsDialogHandler.cs b/CefSharp/IJsDialogHandler.cs index 267453cb0..b4df276fc 100644 --- a/CefSharp/IJsDialogHandler.cs +++ b/CefSharp/IJsDialogHandler.cs @@ -9,6 +9,17 @@ namespace CefSharp bool OnJSAlert(IWebBrowser browser, string url, string message); bool OnJSConfirm(IWebBrowser browser, string url, string message, out bool retval); bool OnJSPrompt(IWebBrowser browser, string url, string message, string defaultValue, out bool retval, out string result); - bool OnJSBeforeUnload(IWebBrowser browser, string message, out bool isReload, out string result); + + /// + /// When leaving the page a Javascript dialog is displayed asking for user confirmation. + /// Returning True allows you to implement a custom dialog or programatically handle. + /// To cancel the unload return True and set allowUnload to False. + /// + /// the browser object + /// message (optional) + /// indicates a page reload + /// True to allow unload, otherwise False + /// Return false to use the default dialog implementation otherwise return true to handle + bool OnJSBeforeUnload(IWebBrowser browser, string message, bool isReload, out bool allowUnload); } } diff --git a/CefSharp/IRequestHandler.cs b/CefSharp/IRequestHandler.cs index c82e4ba56..4eb66e713 100644 --- a/CefSharp/IRequestHandler.cs +++ b/CefSharp/IRequestHandler.cs @@ -40,7 +40,7 @@ namespace CefSharp /// the browser object /// the request object - can be modified in this callback. /// the request object - can be modified in this callback. - /// To cancel loading of the resource return true or false o allow the resource to load normally. + /// To cancel loading of the resource return true or false to allow the resource to load normally. bool OnBeforeResourceLoad(IWebBrowser browser, IRequest request, IResponse response); ///