how can i block loading remote content on dotnetbrowser?

Finally I found the solution. I dropped out all of my tries what I posted above, and tried another way. The DotNetBrowser has a resource handler and you can set what kind of resources you’d like to allow to load and what not. Here is my code:

var network = wpfBrowserView.Browser.Context.NetworkService;
var resourceHandler = new MyResourceHandler();
resourceHandler.Load += args => {
    ResourceType resource = args.Parameters.ResourceType;

    switch (resource) {
        case ResourceType.PREFETCH:
        case ResourceType.IMAGE:
        case ResourceType.MEDIA:
        case ResourceType.OBJECT:
        case ResourceType.STYLESHEET:
        case ResourceType.FONT_RESOURCE:
        case ResourceType.SUB_RESOURCE:
            return false;
        default:
            // allow to load for the others
            return true;
    }
};

network.ResourceHandler = resourceHandler;

And the custom resource handler:

public class ResourceLoadEventArgs {
    public ResourceParams Parameters { get; set; }
}

public delegate bool ResourceLoadHandler(ResourceLoadEventArgs args);

public class MyResourceHandler : ResourceHandler {
    public event ResourceLoadHandler Load;

    public bool CanLoadResource(ResourceParams parameters) {
        return Load?.Invoke(new ResourceLoadEventArgs { Parameters = parameters }) ?? true;
    }
}

So, adding this piece of code before loading the html into the browser view, it causes to pass every test in the email privacy tester. Then you can put a button for the user to load the remote content, and when the user clicks on it, you can allow every resource.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top