Erdnod
Erdnod

Reputation:

Disable Javascript popups when using Windows.Forms.Webbrowser.Navigate()

Ok, I have a very frustrating problem. I am parsing a webpage and need to have it execute the javascript in order to get the information I want.

Form f = new Form();
WebBrowser w = new WebBrowser();
w.Navigate(url);
f.Controls.Add(w);
f.ShowDialog();
HtmlElementCollection hc = w.Document.GetElementsByTagName("button");

This works and I'm able to get the button elements fine, but then I get a popup everytime I run this. Very annoying. The popup is javascript based and I need to run Javascript to get the button element info. Here is the script for the popup.

<script>
var evilPopup = (getCookieVar("markOpen","PromoPop") == 1);
if (evilPopup != 1) 
{
    PromoPop = window.open('/browse/info.aspx?cid=36193','Advertisement', 'width=365,height=262,screenX=100,screenY=100');

if (PromoPop) 
    {
       PromoPop.blur();
       window.focus();
       setCookieVar("markOpen","PromoPop","1");             
    }
}
</script>

I tried in vane to possibly add a cookie to the Forms.Webbrowser control but got frustrated and gave up. I tried setting the NoAllowNavagate property and everything else to no avail.

Can anyone help? Also, there a way to get the DomDocument info from the Console.App without having to open a form?

Thanks

Upvotes: 2

Views: 8094

Answers (4)

ismail
ismail

Reputation:

you should try SHDocVw.dll to automatically catch new window.

        private SHDocVw.WebBrowser_V1 Web_V1;

        // write it on form load event

        Web_V1 = (SHDocVw.WebBrowser_V1)this.webBrowser1.ActiveXInstance;

        Web_V1.NewWindow += new SHDocVw.DWebBrowserEvents_NewWindowEventHandler(Web_V1_NewWindow);


        private void Web_V1_NewWindow(string URL, int Flags, string TargetFrameName, ref object PostData, string Headers, ref bool Processed)
        {
            Processed = true; //Stop event from being processed

            //Code to open in same window
            //this.webBrowser1.Navigate(URL);

            //Code to open in new window instead of same window
            frmEBeyanname Popup = new frmEBeyanname();
            Popup.webBrowser1.Navigate(URL);
            Popup.Show();
        }

bye

Upvotes: 0

Mikko Rantanen
Mikko Rantanen

Reputation: 8084

The WebBrowser component has NewWindow event with CancelEventArgs. So just add a handler similar to:

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

When the javascript tries to open a popup window the event gets triggered and cancels it.

Upvotes: 6

A quick and dirty solution would be to use WebClient to download the html to a temporary file, replace the ad script with string.Empty and load the file in the control.

Upvotes: 0

Greg
Greg

Reputation: 321638

Can you inject Javascript into the document? You could add this:

window.open = function() { return false; }

Upvotes: 0

Related Questions