浏览器对话框弹出块

本文关键字:对话框 浏览器 | 更新日期: 2023-09-27 18:28:59

我正在开发一个程序,该程序具有一个不可见的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)

浏览器对话框弹出块

尝试实现WebBrowser功能控制,特别是Feature_BLOCK_INPUT_PROMPTS和Feature_WBOC_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);
        }
    }
}