user2146441
user2146441

Reputation: 228

Prevent webbrowser control loading javascript from certain domain

I'm loading a html page into a C# webbrowser object. There are links to some remote javascript files between script tags in the html page. One of them has a number of errors which I can't remedy as I can't change the javascript. As it's not needed, I'd rather disable it.

I would like to stop the webbrowser loading any javascript from a particular domain or just have the browser be returned a '404' for that javascript file. This can be done in Fiddler with the Autoresponder.

Is there any way to do this in C#? Like a mini-proxy forwarding all requests except those to *.domainwitherrors.com? Failing that, is there there a way to use Fiddler directly in C#?

Trapping the 'Navigating' event in the webbrowser doesn't catch the javascript before it's loaded into the DOM.

   webBrowser1.Navigate("http://www.somedomain.com");
   webBrowser1.Navigating += webBrowser1_Navigating;

    void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
    {
        if (e.Url.ToString().Contains("domainwitherrors"))
        {
            return;
        }

}

Upvotes: 0

Views: 866

Answers (1)

EricLaw
EricLaw

Reputation: 57115

No, the WebBrowser control doesn't offer any request filtering mechanisms that would allow you to prevent subdownloads from only one domain. You could add the domain to your restricted sites zone (which will block its ability to run script in every IE-based browser on your system) using IE's Tools > Internet Options > Security tab.

FiddlerCore is a .NET class library that can be embedded into applications to provide exactly the sort of functionality you're describing. You can even use the URLMonInterop.SetProxyInProcess API it exposes so that only your application (and not the system as a whole) routes its traffic through the proxy.

Upvotes: 2

Related Questions