【问题标题】:Get Popup Information With WebBrowser使用 WebBrowser 获取弹出信息
【发布时间】:2011-11-30 03:55:38
【问题描述】:

我正在使用 System.Windows.Forms.WebBrowser 控件来访问 URL。某些 URL 会触发在 IE 中而不是在 WebBrowser 中打开的弹出窗口。我不在乎他们在哪里打开。但是以某种方式找出弹出窗口的 URL 是最重要的目标。我一直在网上看到对“NewWindow2”事件的引用。我不确定它有多相关,但我不知道如何使用它/在哪里找到它,因为 WebBrowser 没有 NewWindow2 事件,只有一个 NewWindow 事件。

有什么想法吗?

谢谢!

编辑

Web_V1.NewWindow 事件仅在一定程度上起作用。并非所有弹出窗口都被检测到。有些 URL 只显示为“about:blank”。前任。在http://www.thedailybeast.com/ 上使用它。 (有谁知道其他有大量弹出窗口的网站吗?)

是否有人知道检测 WebBrowser 弹出窗口的结果页面的万能方法?

【问题讨论】:

    标签: c# winforms webbrowser-control


    【解决方案1】:

    一个好的弹出测试器是http://www.popuptest.com

    WebBrowserNewWindow 事件实际上与底层的NewWindow2 事件挂钩。 NewWindow 工作正常,但它不会为您提供新窗口所在的 url。

    从 IE6 开始,有一个NewWindow3 可用,但是这个没有被 WebBrowser 暴露。但是,这里有一个示例代码,展示了如何使用它。在NewWindow3 事件处理程序中,您将看到所有弹出窗口和相应的 url。

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

    【讨论】:

      【解决方案2】:

      这是一个以前回答过的问题,我相信它会给你解决方案。 (向下滚动 C# 版本的代码窗口)。

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

      也许这个项目将帮助您实现处理弹出窗口的目标: http://www.codeproject.com/KB/cpp/ExtendedWebBrowser.aspx#GoalBlock

      【讨论】:

      • 感谢您的帮助,但我不知道如何使用该代码找出弹出窗口的 URL。添加到 NewWindow 事件后,弹出窗口存储在哪里?
      • URL 应该在事件处理程序中标记为“URL”的字符串中。在示例中,它被称为“Web_V1_NewWindow”
      【解决方案3】:
      webBrowser1.Navigating += (sndr, ee) =>
      {
          MessageBox.Show(ee.Url.DnsSafeHost + "\n" + ee.Url.ToString());
      };
      
      webBrowser1.Navigate("http://www.thedailybeast.com/");
      

      【讨论】:

        【解决方案4】:

        .NET 框架的包装器并未公开底层本机 Web 浏览器控件的所有功能...

        为此,您必须自己包装该控件 - 这是一项相当乏味的任务,谢天谢地已经处理过......完整的源代码和详细信息请参阅http://www.codeproject.com/KB/miscctrl/csEXWB.aspx

        这个“完整的包装”包含例如你上面提到的NewWindow2...

        另一种选择是通过 http://awesomium.com/ 使用 chrome,它甚至支持无窗口模式,因此非常适合以编程方式处理各种浏览器内容...

        【讨论】:

        • 如果 .NET 已经提供了 NewWindow2 处理,为什么我需要新的包装器控件?当前未触发 NewWindow 事件的弹出窗口会被该包装器捕获吗?
        • 该包装器比 .NET 内置包装器更完整...因此缺少 IE 的一些 0day-exploit,这将允许您执行 MS 的 IE 控件允许本机代码执行的任何操作做(这比 .NET 包装器要多得多!)...只需点击链接...作者描述的差异相当详细...
        • NewWindow2 已由 WebBrowser 控件处理并引发 NewWindow .NET 事件。有很多东西可以用标准的 WebBrowser 控件进行调整,而无需重新实现整个东西。
        猜你喜欢
        • 2014-01-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多