【问题标题】:Webbrowser dialog popup block网络浏览器对话框弹出块
【发布时间】:2023-03-19 06:11:02
【问题描述】:

我正在开发一个程序,该程序具有一个不可见的 Web 浏览器控件,用于从某些网页加载数据。但是,我无法阻止某种类型的弹出窗口。

这是我目前用来阻止弹出窗口的代码

private void webBrowser1_NewWindow( object sender, 
                                    System.ComponentModel.CancelEventArgs e)
{
  e.Cancel = true;
}

我在http://www.popuptest.com/ 上对其进行了测试,但它无法阻止 Come & Go 测试和无模式窗口测试。 http://i75.servimg.com/u/f75/13/13/40/49/b11.png

有没有办法阻止这些弹出窗口?

这是显示弹出窗口的 javascript

function modelesswin(url,mwidth,mheight){
if (document.all&&window.print) //if ie5
    eval('window.showModelessDialog(url,"","help:0;resizable:1;dialogWidth:'+mwidth+'px;dialogHeight:'+mheight+'px")')
else
eval('window.open(url,"","width='+mwidth+'px,height='+mheight+'px,resizable=1,scrollbars=1")')
}


modelesswin("http://www.popuptest.com/popup1.html",600,600)

【问题讨论】:

    标签: c# popup .net-2.0 webbrowser-control


    【解决方案1】:

    尝试实现WebBrowser Feature Control,尤其是FEATURE_BLOCK_INPUT_PROMPTSFEATURE_WEBOC_POPUPMANAGEMENT

    [EDITED] 此代码适用于您的测试站点,请尝试(使用 IE10 测试)。确保在创建 WebBrowser 之前设置功能(在下面的 InitializeComponent 之前)并执行 ScriptErrorsSuppressed = true 以抑制由阻止弹出窗口导致的脚本错误。

    using System;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    using System.Diagnostics;
    using Microsoft.Win32;
    
    namespace WinformsWB
    {
        public partial class Form1 : Form
        {
             public Form1()
            {
                SetBrowserFeatureControl();
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                this.webBrowser1.ScriptErrorsSuppressed = true;
                this.webBrowser1.Navigate("http://www.popuptest.com/");
            }
    
            private void SetBrowserFeatureControlKey(string feature, string appName, uint value)
            {
                using (var key = Registry.CurrentUser.CreateSubKey(
                    String.Concat(@"Software\Microsoft\Internet Explorer\Main\FeatureControl\", feature),
                    RegistryKeyPermissionCheck.ReadWriteSubTree))
                {
                    key.SetValue(appName, (UInt32)value, RegistryValueKind.DWord);
                }
            }
    
            private void SetBrowserFeatureControl()
            {
                // http://msdn.microsoft.com/en-us/library/ee330720(v=vs.85).aspx
    
                // FeatureControl settings are per-process
                var fileName = System.IO.Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName);
    
                // make the control is not running inside Visual Studio Designer
                if (String.Compare(fileName, "devenv.exe", true) == 0 || String.Compare(fileName, "XDesProc.exe", true) == 0)
                    return;
    
                // TODO: FEATURE_BROWSER_MODE - what is it?
                SetBrowserFeatureControlKey("FEATURE_BROWSER_EMULATION", fileName, 9000); // Webpages containing standards-based !DOCTYPE directives are displayed in IE10 Standards mode.
                SetBrowserFeatureControlKey("FEATURE_DISABLE_NAVIGATION_SOUNDS", fileName, 1);
                SetBrowserFeatureControlKey("FEATURE_WEBOC_POPUPMANAGEMENT", fileName, 1);
                SetBrowserFeatureControlKey("FEATURE_BLOCK_INPUT_PROMPTS", fileName, 1);
            }
        }
    }
    

    【讨论】:

    • 不幸的是它没有阻止给定的弹出窗口。
    • 您是否点击了无模式对话框链接?主页不显示任何弹出窗口。我已经测试了您的代码,但它无法阻止该弹出窗口。这些功能确实会启用/禁用,因为导航声音消失了。我也在使用 IE9。
    • 重置资源管理器有效。看起来 IE 中的弹出窗口阻止程序已被禁用,这导致了问题。我进行了一些实验,并注意到 Web 浏览器控件将始终反映 IE 弹出窗口阻止程序的行为,而不管传递给它的设置。尝试禁用 IE 中的弹出窗口阻止程序,看看您的代码是否仍会阻止它。
    • @Noseratio:我知道你会找到解决方案的!谢谢!我想通过activeX.FileDownload 事件取消下载:) 我几天都想知道怎么做。
    • @Jack,很高兴它成功了。我正在度假,因此延迟:)
    猜你喜欢
    • 2023-03-27
    • 2020-08-31
    • 2016-05-23
    • 2016-11-14
    • 1970-01-01
    • 1970-01-01
    • 2011-03-27
    • 1970-01-01
    • 2012-10-30
    相关资源
    最近更新 更多