2011-10-03 19 views
9

Estoy usando el control System.Windows.Forms.WebBrowser para visitar las URL. Ciertas URL activan ventanas emergentes que se abren en IE en lugar de en WebBrowser. No me importa mucho dónde se abren. Pero de alguna manera descubrir las URL de las ventanas emergentes es el objetivo más importante. Sigo viendo referencias al evento "NewWindow2" en la web. No estoy seguro de qué tan relacionado está, pero no sé cómo usarlo/dónde encontrarlo, ya que WebBrowser no tiene un evento NewWindow2, solo un evento NewWindow.Obtener información emergente con WebBrowser

¿Alguna idea?

Gracias!

caso EDITAR

El Web_V1.NewWindow sólo funciona hasta cierto punto. No todas las ventanas emergentes se están detectando. Y algunas URL solo aparecen como "sobre: ​​en blanco". Ex. Úselo en http://www.thedailybeast.com/. (¿Alguien conoce otros sitios con cantidades notorias de ventanas emergentes?)

¿Alguien conoce una forma sencilla de detectar la página resultante de una ventana emergente WebBrowser?

Respuesta

2

Aquí hay una pregunta previamente respondida que creo que le dará la solución. (Desplácese hacia abajo por la ventana de código para la versión de C#).

System.Windows.Forms.WebBrowser open links in same window or new window with same session

Tal vez este proyecto le ayudará con sus metas para manejar ventanas emergentes: http://www.codeproject.com/KB/cpp/ExtendedWebBrowser.aspx#GoalBlock

+0

Gracias por su ayuda, pero no veo cómo puedo usar ese código para averiguar la URL de las ventanas emergentes. Una vez que agregue al evento NewWindow, ¿dónde están almacenadas las ventanas emergentes? – carlbenson

+0

La URL debe estar en la cadena etiquetada como "URL" en el controlador de eventos. En el ejemplo, se llamaba "Web_V1_NewWindow" –

1
webBrowser1.Navigating += (sndr, ee) => 
{ 
    MessageBox.Show(ee.Url.DnsSafeHost + "\n" + ee.Url.ToString()); 
}; 

webBrowser1.Navigate("http://www.thedailybeast.com/"); 
0

la envoltura del marco .NET no expone todas las capacidades del control de navegador web nativo subyacente. ..

Para eso tendrías que ajustar ese control tú mismo, una tarea bastante tediosa que ya se ha tenido en cuenta afortunadamente ... para ver el código fuente completo y detalles ver http://www.codeproject.com/KB/miscctrl/csEXWB.aspx

Esta "envoltura completa" contiene, por ejemplo NewWindow2 la que usted ha mencionado más arriba ...

Otra opción sería el uso de cromo a través http://awesomium.com/ que soporta incluso un modo de ventana-menos y por lo tanto es ideal para hacer todo tipo de las cosas del navegador programatically ...

+0

¿Por qué necesito el nuevo control de envoltura si .NET ya proporciona el manejo de NewWindow2?¿Las ventanas emergentes que actualmente no están desencadenando el evento NewWindow serán atrapadas por ese contenedor? – carlbenson

+0

esa envoltura es MUCHO MÁS COMPLETA que la envoltura incorporada de .NET ... tan corto de algún exploit de 0day de IE esto le permitiría hacer cualquier cosa que el control de IE de MS permita que haga el código nativo (que es mucho más) que el .NET wrapper!) ... simplemente siga el enlace ... el autor describe las diferencias bastante detalladas ... – Yahia

+0

NewWindow2 ya está manejado por el control WebBrowser y plantea el evento NewWindow .NET. Hay muchas cosas que se pueden ajustar con el control estándar de WebBrowser sin volver a implementar todo el material. –

4

Un buen probador de ventanas emergentes es http://www.popuptest.com.

El evento WebBrowser NewWindow de hecho está enganchado al evento subyacente NewWindow2. NewWindow funciona bien, pero no proporciona la URL a la que se dirige la nueva ventana.

Desde IE6, hay un NewWindow3 disponible, pero este no está expuesto por WebBrowser. Sin embargo, aquí hay un código de muestra que muestra cómo usarlo. En el controlador de eventos NewWindow3, verá todas las ventanas emergentes y la URL correspondiente.

// test code 
    private void button1_Click(object sender, EventArgs e) 
    { 
     _events = new WebBrowserEvents(_wb); // register event sink, make sure ActiveXInsance is not null here 

     _wb.Navigate("http://www.popuptest.com/"); 
    } 

// this event sink declares the NewWindow3 event 
public class WebBrowserEvents : StandardOleMarshalObject, DWebBrowserEvents2, IDisposable 
{ 
    private AxHost.ConnectionPointCookie _cookie; 

    public WebBrowserEvents(WebBrowser wb) 
    { 
     _cookie = new AxHost.ConnectionPointCookie(wb.ActiveXInstance, this, typeof(DWebBrowserEvents2)); 
    } 

    void DWebBrowserEvents2.StatusTextChange(string text) 
    { 
     Trace.WriteLine("StatusTextChange text:" + text); 
    } 

    void DWebBrowserEvents2.ProgressChange(int progress, int progressMax) 
    { 
     Trace.WriteLine("ProgressChange progress:" + progress + " progress:" + progressMax); 
    } 

    void DWebBrowserEvents2.CommandStateChange(int command, bool enable) 
    { 
     Trace.WriteLine("CommandStateChange command:" + command + " enable:" + enable); 
    } 

    void DWebBrowserEvents2.DownloadBegin() 
    { 
     Trace.WriteLine("DownloadBegin"); 
    } 

    void DWebBrowserEvents2.DownloadComplete() 
    { 
     Trace.WriteLine("DownloadComplete"); 
    } 

    void DWebBrowserEvents2.TitleChange(string text) 
    { 
     Trace.WriteLine("TitleChange text:" + text); 
    } 

    void DWebBrowserEvents2.PropertyChange(string szProperty) 
    { 
     Trace.WriteLine("PropertyChange szProperty:" + szProperty); 
    } 

    void DWebBrowserEvents2.BeforeNavigate2(object pDisp, ref object URL, ref object flags, ref object targetFrameName, ref object postData, ref object headers, ref bool cancel) 
    { 
     Trace.WriteLine("BeforeNavigate2 URL:" + URL); 
    } 

    void DWebBrowserEvents2.NewWindow2(ref object pDisp, ref bool cancel) 
    { 
     Trace.WriteLine("NewWindow2"); 
    } 

    void DWebBrowserEvents2.NavigateComplete2(object pDisp, ref object URL) 
    { 
     Trace.WriteLine("NavigateComplete2 URL:" + URL); 
    } 

    void DWebBrowserEvents2.DocumentComplete(object pDisp, ref object URL) 
    { 
     Trace.WriteLine("DocumentComplete URL:" + URL); 
    } 

    void DWebBrowserEvents2.OnQuit() 
    { 
     Trace.WriteLine("OnQuit"); 
    } 

    void DWebBrowserEvents2.OnVisible(bool visible) 
    { 
     Trace.WriteLine("OnVisible visible:" + visible); 
    } 

    void DWebBrowserEvents2.OnToolBar(bool toolBar) 
    { 
     Trace.WriteLine("OnToolBar toolBar:" + toolBar); 
    } 

    void DWebBrowserEvents2.OnMenuBar(bool menuBar) 
    { 
     Trace.WriteLine("OnMenuBar menuBar:" + menuBar); 
    } 

    void DWebBrowserEvents2.OnStatusBar(bool statusBar) 
    { 
     Trace.WriteLine("OnStatusBar statusBar:" + statusBar); 
    } 

    void DWebBrowserEvents2.OnFullScreen(bool fullScreen) 
    { 
     Trace.WriteLine("OnFullScreen fullScreen:" + fullScreen); 
    } 

    void DWebBrowserEvents2.OnTheaterMode(bool theaterMode) 
    { 
     Trace.WriteLine("OnTheaterMode theaterMode:" + theaterMode); 
    } 

    void DWebBrowserEvents2.WindowSetResizable(bool resizable) 
    { 
     Trace.WriteLine("WindowSetResizable resizable:" + resizable); 
    } 

    void DWebBrowserEvents2.WindowSetLeft(int left) 
    { 
     Trace.WriteLine("WindowSetLeft left:" + left); 
    } 

    void DWebBrowserEvents2.WindowSetTop(int top) 
    { 
     Trace.WriteLine("WindowSetTop top:" + top); 
    } 

    void DWebBrowserEvents2.WindowSetWidth(int width) 
    { 
     Trace.WriteLine("WindowSetWidth width:" + width); 
    } 

    void DWebBrowserEvents2.WindowSetHeight(int height) 
    { 
     Trace.WriteLine("WindowSetHeight height:" + height); 
    } 

    void DWebBrowserEvents2.WindowClosing(bool isChildWindow, ref bool cancel) 
    { 
     Trace.WriteLine("WindowClosing isChildWindow:" + isChildWindow); 
    } 

    void DWebBrowserEvents2.ClientToHostWindow(ref int cx, ref int cy) 
    { 
     Trace.WriteLine("ClientToHostWindow cx:" + cx + " cy:" + cy); 
    } 

    void DWebBrowserEvents2.SetSecureLockIcon(int secureLockIcon) 
    { 
     Trace.WriteLine("SetSecureLockIcon secureLockIcon:" + secureLockIcon); 
    } 

    void DWebBrowserEvents2.FileDownload(ref bool cancel) 
    { 
     Trace.WriteLine("FileDownload"); 
    } 

    void DWebBrowserEvents2.NavigateError(object pDisp, ref object URL, ref object frame, ref object statusCode, ref bool cancel) 
    { 
     Trace.WriteLine("NavigateError url:" + URL); 
    } 

    void DWebBrowserEvents2.PrintTemplateInstantiation(object pDisp) 
    { 
     Trace.WriteLine("PrintTemplateInstantiation"); 
    } 

    void DWebBrowserEvents2.PrintTemplateTeardown(object pDisp) 
    { 
     Trace.WriteLine("PrintTemplateTeardown"); 
    } 

    void DWebBrowserEvents2.UpdatePageStatus(object pDisp, ref object nPage, ref object fDone) 
    { 
     Trace.WriteLine("UpdatePageStatus"); 
    } 

    void DWebBrowserEvents2.PrivacyImpactedStateChange(bool bImpacted) 
    { 
     Trace.WriteLine("PrivacyImpactedStateChange bImpacted:" + bImpacted); 
    } 

    void DWebBrowserEvents2.NewWindow3(ref object pDisp, ref bool cancel, int dwFlags, ref object bstrUrlContext, ref object bstrUrl) 
    { 
     Trace.WriteLine("NewWindow3 bstrUrlContext:" + bstrUrlContext + " bstrUrl:" + bstrUrl); 
    } 

    public void Dispose() 
    { 
     if (_cookie != null) 
     { 
      _cookie.Disconnect(); 
      _cookie = null; 
     } 
    } 
} 

[ComImport, Guid("34A715A0-6587-11D0-924A-0020AFC7AC4D"), InterfaceType(ComInterfaceType.InterfaceIsIDispatch)] 
internal interface DWebBrowserEvents2 
{ 
    [DispId(0x66)] 
    void StatusTextChange([In] string text); 
    [DispId(0x6c)] 
    void ProgressChange([In] int progress, [In] int progressMax); 
    [DispId(0x69)] 
    void CommandStateChange([In] int command, [In] bool enable); 
    [DispId(0x6a)] 
    void DownloadBegin(); 
    [DispId(0x68)] 
    void DownloadComplete(); 
    [DispId(0x71)] 
    void TitleChange([In] string text); 
    [DispId(0x70)] 
    void PropertyChange([In] string szProperty); 
    [DispId(250)] 
    void BeforeNavigate2([In, MarshalAs(UnmanagedType.IDispatch)] object pDisp, [In] ref object URL, [In] ref object flags, [In] ref object targetFrameName, [In] ref object postData, [In] ref object headers, [In, Out] ref bool cancel); 
    [DispId(0xfb)] 
    void NewWindow2([In, Out, MarshalAs(UnmanagedType.IDispatch)] ref object pDisp, [In, Out] ref bool cancel); 
    [DispId(0xfc)] 
    void NavigateComplete2([In, MarshalAs(UnmanagedType.IDispatch)] object pDisp, [In] ref object URL); 
    [DispId(0x103)] 
    void DocumentComplete([In, MarshalAs(UnmanagedType.IDispatch)] object pDisp, [In] ref object URL); 
    [DispId(0xfd)] 
    void OnQuit(); 
    [DispId(0xfe)] 
    void OnVisible([In] bool visible); 
    [DispId(0xff)] 
    void OnToolBar([In] bool toolBar); 
    [DispId(0x100)] 
    void OnMenuBar([In] bool menuBar); 
    [DispId(0x101)] 
    void OnStatusBar([In] bool statusBar); 
    [DispId(0x102)] 
    void OnFullScreen([In] bool fullScreen); 
    [DispId(260)] 
    void OnTheaterMode([In] bool theaterMode); 
    [DispId(0x106)] 
    void WindowSetResizable([In] bool resizable); 
    [DispId(0x108)] 
    void WindowSetLeft([In] int left); 
    [DispId(0x109)] 
    void WindowSetTop([In] int top); 
    [DispId(0x10a)] 
    void WindowSetWidth([In] int width); 
    [DispId(0x10b)] 
    void WindowSetHeight([In] int height); 
    [DispId(0x107)] 
    void WindowClosing([In] bool isChildWindow, [In, Out] ref bool cancel); 
    [DispId(0x10c)] 
    void ClientToHostWindow([In, Out] ref int cx, [In, Out] ref int cy); 
    [DispId(0x10d)] 
    void SetSecureLockIcon([In] int secureLockIcon); 
    [DispId(270)] 
    void FileDownload([In, Out] ref bool cancel); 
    [DispId(0x10f)] 
    void NavigateError([In, MarshalAs(UnmanagedType.IDispatch)] object pDisp, [In] ref object URL, [In] ref object frame, [In] ref object statusCode, [In, Out] ref bool cancel); 
    [DispId(0xe1)] 
    void PrintTemplateInstantiation([In, MarshalAs(UnmanagedType.IDispatch)] object pDisp); 
    [DispId(0xe2)] 
    void PrintTemplateTeardown([In, MarshalAs(UnmanagedType.IDispatch)] object pDisp); 
    [DispId(0xe3)] 
    void UpdatePageStatus([In, MarshalAs(UnmanagedType.IDispatch)] object pDisp, [In] ref object nPage, [In] ref object fDone); 
    [DispId(0x110)] 
    void PrivacyImpactedStateChange([In] bool bImpacted); 
    [DispId(0x111)] 
    void NewWindow3([In, Out, MarshalAs(UnmanagedType.IDispatch)] ref object pDisp, [In, Out] ref bool cancel, [In] int dwFlags, [In] ref object bstrUrlContext, [In] ref object bstrUrl); 
} 
Cuestiones relacionadas