2011-10-03 4 views
9

Sto utilizzando il controllo System.Windows.Forms.WebBrowser per visitare gli URL. Alcuni URL attivano i popup che si aprono in IE anziché in WebBrowser. Non mi interessa molto dove aprono. Ma in qualche modo scoprire gli URL dei popup è l'obiettivo più importante. Continuo a vedere i riferimenti all'evento "NewWindow2" sul web. Non sono sicuro di quanto sia correlato, ma non so come usarlo/dove trovarlo, poiché WebBrowser non ha un evento NewWindow2, solo un evento NewWindow.Ottieni informazioni popup con WebBrowser

Qualche idea?

Grazie!

evento Modifica

L'Web_V1.NewWindow funziona solo ad un grado. Non tutti i popup sono stati rilevati. E alcuni URL vengono visualizzati come "about: blank". Ex. Usalo su http://www.thedailybeast.com/. (Qualcuno conosce altri siti con notoriose quantità di popup?)

Qualcuno conosce un modo generico per rilevare la pagina risultante di un popup WebBrowser?

risposta

2

Ecco una domanda a cui ho risposto in precedenza che credo possa darvi la soluzione. (Scorrere la finestra del codice per la versione C#).

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

Forse questo progetto vi aiuterà con i vostri obiettivi per gestire i popup: http://www.codeproject.com/KB/cpp/ExtendedWebBrowser.aspx#GoalBlock

+0

Grazie per il vostro aiuto, ma non vedo come posso usare quel codice per scoprire l'URL dei popup. Una volta aggiunto all'evento NewWindow, dove sono memorizzati i popup? – carlbenson

+0

L'URL deve trovarsi nella stringa con l'etichetta "URL" nel gestore eventi. Nell'esempio è stato chiamato "Web_V1_NewWindow" –

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

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

l'involucro dal framework .NET non espone tutte le funzionalità del sottostante controllo WebBrowser nativo. ..

Per quello si dovrebbe avvolgere quel controllo da soli - un compito piuttosto noioso che è già stato curato per fortuna ... per il codice sorgente completo e dettagli vedi http://www.codeproject.com/KB/miscctrl/csEXWB.aspx

Questo "involucro completo" contiene ad esempio NewWindow2 che lei ha citato sopra ...

Un'altra opzione sarebbe quella di utilizzare Chrome tramite http://awesomium.com/ che supporta anche una modalità finestra-meno ed è quindi ideale per fare tutti i tipi di roba del browser programmaticamente ...

+0

Perché è necessario il nuovo controllo wrapper se .NET fornisce già la gestione di NewWindow2?I popup che attualmente non attivano l'evento NewWindow verranno catturati da quel wrapper? – carlbenson

+0

che il wrapper è MOLTO PIÙ COMPLETO rispetto al wrapper incorporato .NET ... quindi a corto di alcuni 0day-exploit di IE questo ti permetterebbe di fare tutto ciò che il controllo IE di MS consente al codice nativo di fare (che è molto più rispetto al wrapper .NET!) ... basta seguire il link ... l'autore descrive le differenze piuttosto dettagliate ... – Yahia

+0

NewWindow2 è già gestito dal controllo WebBrowser e solleva l'evento NewWindow .NET. Ci sono molte cose che possono essere ottimizzate con il controllo standard di WebBrowser senza re-implementare l'intera roba. –

4

Un buon tester di popup è http://www.popuptest.com.

L'evento WebBrowser NewWindow è infatti collegato all'evento NewWindow2 sottostante. NewWindow funziona bene, ma non ti dà l'url dove si sta dirigendo la nuova finestra.

Da IE6, è disponibile un NewWindow3, ma questo non è esposto dal browser web. Tuttavia, ecco un codice di esempio che mostra come usarlo. Nel gestore di eventi NewWindow3, vedrai tutti i popup e l'url corrispondente.

// 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); 
}