Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue Avoiding Js Alert in Cefsharp browser

I am trying to avoid js alert on a page as it breaks the flow and the browser is stuck on that page until the popup is clicked.

I added Class as shown on sample:

public class JsDialogHandler : IJsDialogHandler
{
    public bool OnJSDialog(IWebBrowser browserControl, IBrowser browser, string originUrl, CefJsDialogType dialogType, string messageText, string defaultPromptText, IJsDialogCallback callback, ref bool suppressMessage)
    {
        return true;
    }

    public bool OnJSBeforeUnload(IWebBrowser browserControl, IBrowser browser, string message, bool isReload, IJsDialogCallback callback)
    {
        return true;
    }

    public void OnResetDialogState(IWebBrowser browserControl, IBrowser browser)
    {

    }

    public void OnDialogClosed(IWebBrowser browserControl, IBrowser browser)
    {

    }
}

And i assign to the Chromium browser as:

  CefSharp.Cef.Initialize(new CefSharp.CefSettings());

                browser = new CefSharp.WinForms.ChromiumWebBrowser(CustomLinks[0].ToString());

                JsDialogHandler jss = new JsDialogHandler();
                browser.JsDialogHandler = jss;

The thing is when alert is supposed to show it does run the OnJSDialog event. But then the browser turns white and is just stuck and trying to find a way around but not much is available online..

Any suggestions?

like image 920
confusedMind Avatar asked Jan 19 '17 18:01

confusedMind


2 Answers

In the OnJSDialog method of your handler, make sure that you call Continue(...) on the callback:

public bool OnJSDialog(IWebBrowser browserControl, IBrowser browser, string originUrl, CefJsDialogType dialogType, string messageText, string defaultPromptText, IJsDialogCallback callback, ref bool suppressMessage) {
  callback.Continue(true);
  return true;
}
like image 170
ralph.mayr Avatar answered Sep 28 '22 14:09

ralph.mayr


If you simply want to disable javascript alerts, there is parameter suppressMessage just for that:

// Implementation of IJsDialogHandler
public bool OnJSDialog(IWebBrowser browserControl, IBrowser browser, string originUrl, CefJsDialogType dialogType, string messageText, string defaultPromptText, IJsDialogCallback callback, ref bool suppressMessage)
{
    suppressMessage = true;
    return false;
}
// All other methods should do nothing or return false.
like image 36
Eric Boumendil Avatar answered Sep 28 '22 15:09

Eric Boumendil