2

I have a situation where a rather clever website updates the latest information on the site via Shockwave Flash through a TCP connection. The data received is then updated onto the page via JavaScript so in order to get the latest data a browser is required. If attempts are made to hit the website with continual requests then a) you get banned and b) you're not actually getting the latest data, only the last updated base framework.

So I need to run a browser with scripts enabled.

My first question is, using the standard WPF WebBrowser in .NET I get the following warnings which I don't get in standard IE, Chrome or Firefox. What is causing this and how do I supress/allow it but still allowing scripts for the site to be run?

enter image description here

My second question relates to is there a better way do to this or are there any better alternatives to the WebBrowser control that will

  • Allow scripts to run

  • can access the DOM or html and scripts returned in at least text format

  • is compatible with WPF

  • can hide the browser as I don't actually want it displayed.

So far I've looked into WebKit.NET which doesn't seem to allow access to the DOM and didn't like WPF windows when I tested and also Awesomium but again didn't appear to allow direct access to the DOM without javascript.

Are there any other options (apart from hacking their scripts)?

Thank you

  • You might want to look into Awesomium. I've never used it, but it might be a better alternative for you. http://awesomium.com/ On their Download & Samples page, they have a WPF example you can download using it. – Gromer Sep 21 '12 at 15:50

3 Answers3

2

set WebBrowser.ScriptErrorsSuppressed = true;

burning_LEGION
  • 13,246
  • 8
  • 40
  • 52
  • 1
    Sorry, I forgot to mention I'm using WPF WebBrowser which doesn't appear to have a .ScriptErrorsSuppressed property. –  Sep 21 '12 at 15:37
2

Ultimately I ended up keeping the WPF control and used this code to inject a JavaScript script to disable JavaScript errors. The Microsoft HTML Object Library needs to be added.

    private const string DisableScriptError =    @"function noError() { return true;} window.onerror = noError;";

    private void webBrowser1_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
    {
        InjectDisableScript();
    }

    private void InjectDisableScript()
    {

        HTMLDocumentClass doc = webBrowser1.Document as HTMLDocumentClass;
        HTMLDocument doc2 = webBrowser1.Document as HTMLDocument;

        IHTMLScriptElement scriptErrorSuppressed = (IHTMLScriptElement)doc2.createElement("SCRIPT");
        scriptErrorSuppressed.type = "text/javascript";
        scriptErrorSuppressed.text = DisableScriptError;
        IHTMLElementCollection nodes = doc.getElementsByTagName("head");

        foreach (IHTMLElement elem in nodes)
        {
            HTMLHeadElementClass head = (HTMLHeadElementClass)elem;
            head.appendChild((IHTMLDOMNode)scriptErrorSuppressed);
        }
    }
  • You can also read answer here: http://stackoverflow.com/questions/6138199/wpf-webbrowser-control-how-to-supress-script-errors/9739066#9739066 – MuiBienCarlota Nov 14 '12 at 13:35
1

WPF WebBrowser does not have this property as the WinForms control.

You'd be better using a WindowsFormsHost in your WPF application and use the WinForms WebBrowser (so that you can use SuppressScriptErrors.) Make sure you run in full trust.

Fernando Espinosa
  • 4,625
  • 1
  • 30
  • 37