Sie sind auf Seite 1von 4

18/1/2018 Capturing a pop up window using LifeSpanHandler and CefSharp - CodeProject

13,348,514 members (93,772 online) Member 13630893 350   Sign out

Search for articles, questions, tips


articles Q&A forums lounge

Capturing a pop up window using LifeSpanHandler and CefSharp


Sheila Pontes, 4 Jul 2017

   5.00 (9 votes) Rate:

How capture the event open of a pop up, stop this event and open them wherever you wish

Download source - 19.3 KB

Introduction
CefSharp is a powerful embedding of chrome that allows you to customize a browser to perform the most diverse tasks of the day-to-day. As an example we can mention: read
authentication cookies of the sites that must be sign in, intercept pop up loaded by sites, among other necessities.

In the lines below, I'll show you how to simply capture the open event of a pop-up, stop this event and open them wherever you wish.

To do this, we use the CefSharp interface, called IlifeSpanHandler.

ILifeSpanHandler has 4 events (OnBeforePopup, DoClose, OnBeforeClose e OnAfterCreated) and they are responsible for handling all pop up events. For this task I'll use only the
OnBeforePopup event.

Background
For design, I used:

Microsoft .NET Framework 4.5.2.


CefSharp available in the NuGet Package.
Windows Forms Application.
Visual Studio 2012.

Using the code


This article is a complementation for projects created with CefSharp.
If you don't know, how creater a new project. Click here for learning.

For use this interface:

1. Create a class within the project Inheriting this interface.


Hide   Copy Code
public class LifespanHandler: ILifeSpanHandler

2. Create the event for receive the url of the pop up.
Hide   Copy Code

https://www.codeproject.com/Articles/1194609/Capturing-a-pop-up-window-using-LifeSpanHandler-an 1/4
18/1/2018 Capturing a pop up window using LifeSpanHandler and CefSharp - CodeProject

public event Action popup_request;

3. In the OnBeforePopup event, send the url to the event of item 2 and stop the popup that is opening.
Hide   Copy Code
//get url popup
if (this.popup_request != null)
this.popup_request(targetUrl);

//stop open popup window


newBrowser = null;

4. Type the other events in the class. They have no source code.

When we forget to declare the signature of all interface events in the class, even if they are not used, this error "'popup_cefsharp.LifespanHandler' does not implement interface member
'CefSharp.ILifeSpanHandler.OnAfterCreated(CefSharp.IWebBrowser, CefSharp.IBrowser)'" happens.

Below complete code class.

Hide   Shrink   Copy Code


using CefSharp;
using CefSharp.WinForms;

namespace popup_cefsharp
{
public class LifespanHandler: ILifeSpanHandler
{
//event that receive url popup
public event Action popup_request;

bool ILifeSpanHandler.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)
{
//get url popup
if (this.popup_request != null)
this.popup_request(targetUrl);

//stop open popup window


newBrowser = null;
return true;
}
bool ILifeSpanHandler.DoClose(IWebBrowser browserControl, IBrowser browser)
{ return false; }

void ILifeSpanHandler.OnBeforeClose(IWebBrowser browserControl, IBrowser browser){}

void ILifeSpanHandler.OnAfterCreated(IWebBrowser browserControl, IBrowser browser){}


}
}

Inside of the form, the class LifeSpanHandler is initialize and attribute in the variable of the browser.

Hide   Copy Code


LifespanHandler life = new LifespanHandler();
chrome.LifeSpanHandler = life;

Create the event for url of the pop up captured.

Hide   Copy Code


private void life_popup_request(string obj){}

Add the event in the variable of the class LifeSpanHandler.

Hide   Copy Code


life.popup_request += life_popup_request;

Below complete code. This code was typed inside of the windows form.

Hide   Shrink   Copy Code


using CefSharp;
using CefSharp.WinForms;

namespace popup_cefsharp
{
public partial class frm_main : Form
{
public frm_main()
{
InitializeComponent();
}

//variable
ChromiumWebBrowser chrome, chrome_popup;

private void initialize_browser()


{
try
{
CefSettings settings = new CefSettings();
Cef.Initialize(settings);

//main browser
chrome = new ChromiumWebBrowser(this.txt_url.Text.Trim());
LifespanHandler life = new LifespanHandler();
chrome.LifeSpanHandler = life;
life.popup_request += life_popup_request;
this.pan_container.Controls.Add(chrome);
chrome.Dock = DockStyle.Fill;

https://www.codeproject.com/Articles/1194609/Capturing-a-pop-up-window-using-LifeSpanHandler-an 2/4
18/1/2018 Capturing a pop up window using LifeSpanHandler and CefSharp - CodeProject
//second browser (popup browser)
chrome_popup = new ChromiumWebBrowser("");
this.pan_container_popup.Controls.Add(chrome_popup);
chrome_popup.Dock = DockStyle.Fill;

}
catch (Exception ex)
{
MessageBox.Show("Error in initializing the browser. Error: " + ex.Message);
}
}

private void carregar_popup_new_browser(string url)


{
//open pop up in second browser
chrome_popup.Load(url);
}

private void frm_main_FormClosing(object sender, FormClosingEventArgs e)


{
//close o object cef
Cef.Shutdown();
Application.Exit();
}

private void frm_main_Load(object sender, EventArgs e)


{
//initialize the browser
this.initialize_browser();
}

private void life_popup_request(string obj)


{
//function for open pop up in a new browser
this.carregar_popup_new_browser(obj);
}
}
}

Points of Interest
When you try to manipulate an object, other than the browser, in the life_popup_request event, the project generates an exception.

For correct, use invoke delegate. Example:

Hide   Copy Code

private void life_popup_request(string obj)


{
chrome_popup = new ChromiumWebBrowser(url);
this.Invoke((MethodInvoker)delegate()
{
this.pan_container_popup.Controls.Clear();
this.pan_container_popup.Controls.Add(chrome_popup);
});
chrome_popup.Dock = DockStyle.Fill;
}

References
IlifeSpanHandler (CefSharp) - https://github.com/cefsharp/CefSharp/blob/master/CefSharp/Handler/ILifeSpanHandler.cs
Forum (answer 1)- https://stackoverflow.com/questions/30553577/how-to-handle-popup-links-in-cefsharp?noredirect=1&lq=1

License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share
TWITTER FACEBOOK

https://www.codeproject.com/Articles/1194609/Capturing-a-pop-up-window-using-LifeSpanHandler-an 3/4
18/1/2018 Capturing a pop up window using LifeSpanHandler and CefSharp - CodeProject

About the Author


Sheila Pontes
I work fifteen years as a web developer and Windows application on .NET platform using C #.
Web Developer
Brazil

You may also be interested in...


Public, Private, and Hybrid Cloud: What's the difference? SAPrefs - Netscape-like Preferences Dialog

Chromium (CefSharp) Tor Browser Window Tabs (WndTabs) Add-In for DevStudio

Display HTML in WPF and CefSharp Tutorial Part 1 To Heap or not to Heap; That’s the Large Object Question?

Comments and Discussions


 
 
Add a Comment or Question  Search Comments

 
-- There are no messages in this forum --

Permalink | Advertise | Privacy | Terms of Use | Mobile Layout: fixed | fluid Article Copyright 2017 by Sheila Pontes
Seleccionar idioma ▼
Web02 | 2.8.180111.1 | Last Updated 4 Jul 2017 Everything else Copyright © CodeProject, 1999-2018

https://www.codeproject.com/Articles/1194609/Capturing-a-pop-up-window-using-LifeSpanHandler-an 4/4

Das könnte Ihnen auch gefallen