0

I created a program that will effectively act like a web browser using the CefSharp.Winforms Chromium browser (version 111.2.20). I have a panel that I use with the Chromium browser:

    public ChromiumWebBrowser chromeBrowser;

    public void InitializeChromium()
    {
        chromeBrowser = new ChromiumWebBrowser(TEXT_FIELD.Text);
        chromeBrowser.Dock = DockStyle.Fill;
        this.PANEL_BROWSER.Controls.Add(chromeBrowser);
    }

This initialization method takes place right after "InitializeComponent();".

When I use the built in WebBrowser (IE), I get pop ups when navigating to a site that requires it. However, there are issues with how certain pages load and I am using the Chromium browser as a result. Unfortunately I do not get the pop up with the Chromium browser. Navigating to the site just skips the log in pop up and the site fails to load. I have looked at the resources online for the tool, but I am very self taught and I do not fully understand the best way to resolve this.

I added the following code to my program (tweaking it to say false), but I get errors on the first line. I also notice that the class has no references, but I'd like some guidance on how to have this take effect and allow the pop up.

chromiumWebBrowser.LifeSpanHandler = new LifeSpanHandler();

public class LifeSpanHandler : CefSharp.Handler.LifeSpanHandler
{
    protected override bool OnBeforePopup(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, string targetUrl, string targetFrameName, WindowOpenDisposition targetDisposition, bool userGesture, IPopupFeatures popupFeatures, IWindowInfo windowInfo, IBrowserSettings browserSettings, ref bool noJavascriptAccess, out IWebBrowser newBrowser)
    {
        newBrowser = null;
        
        //Return true to cancel the popup creation
        return false;
    }
}

I am by no means an expert and any help is appreciated. I can follow up on any questions and do my best to clarify my question if it is not clear.

These are the resources that I looked at trying to resolve this myself.

https://ourcodeworld.com/articles/read/173/how-to-use-cefsharp-chromium-embedded-framework-csharp-in-a-winforms-application

https://github.com/cefsharp/CefSharp/wiki/General-Usage (pop up section)

How to handle popup links in CefSharp

Popup handler in Cefsharp

My program largely follows this guide by Fox Learn (without the tabs): https://www.youtube.com/watch?v=p3trglnFZ9Y&ab_channel=FoxLearn

Thank you for any advice and assistance.

273K
  • 29,503
  • 10
  • 41
  • 64
MKE
  • 11
  • 2
  • *but I get errors on the first line.* What error do you get? – 273K Mar 25 '23 at 22:39
  • *the class has no references* What do you mean? Classes can't have references. – 273K Mar 25 '23 at 22:41
  • Errors: Invalid token '=' in class, record, struct, or interface member declaration Invalid token '=' in class, record, struct, or interface member declaration The modifier 'new' is not valid for this item Method must have a return type The name 'chromiumWebBrowser.LifeSpanHandler' does not exist in the current context. The name 'chromiumWebBrowser' does not exist in the current context. The name 'LifeSpanHandler' does not exist in the current context. – MKE Mar 25 '23 at 23:33
  • All errors are for the line: "chromiumWebBrowser.LifeSpanHandler = new LifeSpanHandler();" I'm not sure what is missing or what I got wrong with the syntax. "the class has no references": That is what appears above the text in Visual Studio. It is not being called anywhere. The protected override bool "OnBeforePopup" is also not referenced anywhere in any methods/events. – MKE Mar 25 '23 at 23:33
  • What is `chromiumWebBrowser`? You showed `chromeBrowser` earlier. Please show a [mcve]. That line may not be outside a method. – 273K Mar 25 '23 at 23:51
  • By default popups are opened in a new `Window`, the `LifeSpanHandler` code you've provided basically implements the default behaviour, I wouldn't expect any difference in what you are seeing. It sounds like there is some sort of exception/error that's happening and preventing your popup from loading, open devtools for debugging https://github.com/cefsharp/CefSharp/wiki/Trouble-Shooting#javascript-debugging or check the log file https://github.com/cefsharp/CefSharp/wiki/Trouble-Shooting#log-file – amaitland Mar 29 '23 at 19:48

0 Answers0