2

I am developing a program which has an invisible web browser control that is used for loading data from certain web pages. However, I am having trouble blocking a certain type of popup.

This is the code I am currently using to block popups

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

I have tested it on http://www.popuptest.com/ and it fails to block the Come & Go test and the Modeless Window test. http://i75.servimg.com/u/f75/13/13/40/49/b11.png

Is there a way to block these popups?

This is the javascript which shows the popups

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)
Kidades
  • 600
  • 2
  • 9
  • 26

1 Answers1

4

Try implementing WebBrowser Feature Control, particularly FEATURE_BLOCK_INPUT_PROMPTS and FEATURE_WEBOC_POPUPMANAGEMENT.

[EDITED] This code works for me with your test site, try it (tested with IE10). Make sure you set features before your WebBrowser gets created (before InitializeComponent below) and you do ScriptErrorsSuppressed = true to suppress script errors caused by blocked pop-ups.

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);
        }
    }
}
Community
  • 1
  • 1
noseratio
  • 59,932
  • 34
  • 208
  • 486
  • Unfortunately it is not blocking the given popups. – Kidades Aug 24 '13 at 10:47
  • Have you clicked on the modeless dialog link? The homepage does not show any popups. I have tested your code and it fails to block that popup. The features do get enabled/disabled because the navigation sounds are gone. Also I am using IE9. – Kidades Aug 24 '13 at 12:44
  • Yes, it does work for a modeless dialog as well. Have you actually tried the code? Create a new WinFrom project in Visual Studio, drop WebBrowser onto the main form and paste the above code as is into Form1.cs. I've changed `10000` to `9000` for `FEATURE_BROWSER_EMULATION` to match IE9, but eventually you should come up with your own logic to handle a specific IE version. – noseratio Aug 24 '13 at 12:53
  • That is what I did. New project, web browser, pasted the code, tested and it failed to block. It didn't block any popups. Changing the IE version to 9000 didn't help. – Kidades Aug 24 '13 at 13:26
  • Here's a working VS2012 project including a binary, which I just verified to work under an IE9 VM: http://goo.gl/w3h3JG. That's as far as I can help. – noseratio Aug 24 '13 at 13:51
  • I have ran the binary and the result is still the same, no popups blocked. Even after upgrading to IE 10. I guess my IE is broken. :( – Kidades Aug 24 '13 at 15:25
  • Try resetting all of your IE settings from Internet Options, Advanced tab. – noseratio Aug 24 '13 at 15:27
  • 1
    Reseting explorer worked. It looks like that the popup blocker in IE was disabled and that was causing the problem. I have experimented a bit and noticed that the web browser control will always reflect the behavior of IE pop up blocker regardless settings passed to it. Try to disable the popup blocker in IE and see if your code will still block it. – Kidades Aug 24 '13 at 16:11
  • Try doing it via [CoInternetSetFeatureEnabled](http://msdn.microsoft.com/en-us/library/ms537168(v=vs.85).aspx) instead of the registry and let us know if that works better. – noseratio Aug 24 '13 at 16:16
  • Does this block download pop up too? if not, is possible do so? how? – Jack Mar 31 '17 at 20:16
  • @Jack, there's also `FileDownload` event, check [this](http://stackoverflow.com/a/18269105/1768303). – noseratio Apr 02 '17 at 22:14
  • 1
    @Noseratio: I knew you would got a solution! Thanks! I mangaed to cancel the download by `activeX.FileDownload` event :) I'm days trying to find out how do that. – Jack Apr 03 '17 at 17:36
  • 1
    @Jack, glad it worked. I'm on vacation hence the delay :) – noseratio Apr 04 '17 at 20:58
  • @Noseratio: You posted once a method where we could wait unti the page on WebBrowser fully loaded, do you know where I can fin this? Have a nice vacation! Hope not be bothering you too much :) – Jack Apr 04 '17 at 21:20