4

I have to change inner html code before showing it in the WebBrowser.

Test page - http://aksmod.ru/skajrim-mod-kukri-ot-aksyonov-v5-0/

I tried to use AngleSharp.Scripting but it doesn't work correctly (the ads doesn't load)

var config = new Configuration().WithDefaultLoader().WithJavaScript();
var document = BrowsingContext.New(config).OpenAsync(address).Result;

//do something 

return document.DocumentElement.OuterHtml;

later I thought about LoadCompleted, but the result was the same

private void Wb_LoadCompleted(object sender, NavigationEventArgs e)
{
    Console.WriteLine("Loaded");
    string url = e.Uri.ToString();
    if (!(url.StartsWith("http://") || url.StartsWith("https://")))
    { }
    if (e.Uri.AbsolutePath != wb.Source.AbsolutePath)
    { }
    else
    {
        Console.WriteLine("Full Loaded");
        HTMLDocument html = (HTMLDocument)wb.Document;
        var value = html.getElementsByTagName("html").item(index: 0);
        //do something
        wb.NavigateToString(value.OuterHtml);
    }
}

the event just doesn't fire (it works fine for some other sites, although).

So, what I am missing to do it?

Update 1

MCVE

XAML

<Grid>
    <WebBrowser Name="wb" />
</Grid>

Code behind

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        wb.Navigated += Wb_Navigated;
        wb.LoadCompleted += Wb_LoadCompleted;
        wb.Navigate("http://aksmod.ru/skajrim-mod-kukri-ot-aksyonov-v5-0/");
    }

private void Wb_LoadCompleted(object sender, NavigationEventArgs e)
{
    Console.WriteLine("Loaded");
    string url = e.Uri.ToString();
    if (!(url.StartsWith("http://") || url.StartsWith("https://")))
    { }
    if (e.Uri.AbsolutePath != wb.Source.AbsolutePath)
    { }
    else
    {
        Console.WriteLine("Full Loaded");
        HTMLDocument html = (HTMLDocument)wb.Document;
        var value = html.getElementsByTagName("html").item(index: 0);
        //do something
        wb.NavigateToString(value.OuterHtml);
    }
}

    private void Wb_Navigated(object sender, NavigationEventArgs e)
    {

        FieldInfo fiComWebBrowser = typeof(WebBrowser)
            .GetField("_axIWebBrowser2",
                      BindingFlags.Instance | BindingFlags.NonPublic);
        if (fiComWebBrowser == null) return;
        object objComWebBrowser = fiComWebBrowser.GetValue(wb);
        if (objComWebBrowser == null) return;
        objComWebBrowser.GetType().InvokeMember(
            "Silent", BindingFlags.SetProperty, null, objComWebBrowser,
            new object[] { true });

        Console.WriteLine("Navigated");
    }
}
15
  • "Doesn't work correctly" - how exactly? Commented Oct 9, 2017 at 15:15
  • 3
    I would use the WebClient to load the complete site async, change the html code and than load the modified string in the WebBrowser. Commented Oct 9, 2017 at 15:19
  • @Evk the ads doesn't load Commented Oct 9, 2017 at 16:12
  • @Marco are you sure there will be any difference between loaded page with AngleSharp and WebClient? Commented Oct 9, 2017 at 16:13
  • What if wait for a page to load then modify it with javascript (that is - not use NavigateToString but modify directly). Commented Oct 16, 2017 at 11:25

2 Answers 2

3

The ads are embedded as iFrame within the page you presented. In my case, the Ad URL loaded in the iFrame is something like https://cdn.254a.com/images/hosted/elv/retargeting/v5/728x90.html?... (check with web browser's inspector tool)

Probably the ad does not allow iframing in your page (Check what the ad returns in X-Frame-Options header field). If this is the issue, it should be possible to implement a proxy for the ad, and let the proxy change the X-Frame-Options header.

In this case, if the ad URL is https (and not just http), you'd need to create a proxy that acts as Man-in-the-Middle. See accepted answer of What's the point of the X-Frame-Options header?. But you could replace the URL by your proxy URL, with the original URL in the ARGS. the proxy acts as HTTPS client, gets the content, proxy is able to modify the header, and returns the content to your page just via HTTP.

Sign up to request clarification or add additional context in comments.

Comments

-1

You can use: http://html-agility-pack.net for manipulate the Html code on C#.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.