Skip to content

CefSharp block popup windows

CefSharp block popup windows

Recently I had to use CefSharp for a project of mine and needed to block popup windows. There are not a lot of up-to-date answers or solutions for issues pertaining to CefSharp, so I thought I should just share mine. You will need to implement your own class which inherits or extends the ILifeSpanHandler interface
public class BrowserLifeSpanHandler : ILifeSpanHandler
 {
     public bool OnBeforePopup(IWebBrowser browserControl, IBrowser browser, IFrame frame, string targetUrl, string targetFrameName,
         WindowOpenDisposition targetDisposition, bool userGesture, IPopupFeatures popupFeatures, IWindowInfo windowInfo,
         IBrowserSettings browserSettings, ref bool noJavascriptAccess, out IWebBrowser newBrowser)
     {
         MessageBox.Show("Popup detected. Target url : " + targetUrl);
         newBrowser = null;
         return true;
     }

     public void OnAfterCreated(IWebBrowser browserControl, IBrowser browser)
     {
         //
     }

     public bool DoClose(IWebBrowser browserControl, IBrowser browser)
     {
         return false;
     }

     public void OnBeforeClose(IWebBrowser browserControl, IBrowser browser)
     {
         //nothing
     }
 }
And in your browser instance, set the LifeSpanHandler member to your own handler
BrowserLifeSpanHandler blsh = new BrowserLifeSpanHandler();
browser.LifeSpanHandler = blsh;
 
Enjoyed the content ? Share it with your friends !
Published inProgramming

Be First to Comment

Leave a Reply

Your email address will not be published.