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");
    }
}
Ev_Hyper
  • 183
  • 8
  • "Doesn't work correctly" - how exactly? – Evk Oct 09 '17 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. – Marco Oct 09 '17 at 15:19
  • @Evk the ads doesn't load – Ev_Hyper Oct 09 '17 at 16:12
  • @Marco are you sure there will be any difference between loaded page with AngleSharp and WebClient? – Ev_Hyper Oct 09 '17 at 16:13
  • What if wait for a page to load then modify it with javascript (that is - not use NavigateToString but modify directly). – Evk Oct 16 '17 at 11:25
  • @Evk I'll glad to do it but I can't catch when page is loaded – Ev_Hyper Oct 16 '17 at 12:02
  • Do you have demo git project one can debug? – Tarun Lalwani Oct 18 '17 at 07:04
  • @TarunLalwani the mcve too small to create git repo - I'll add some detail to question – Ev_Hyper Oct 19 '17 at 09:06
  • @TarunLalwani thank you for advice - I added the full version – Ev_Hyper Oct 19 '17 at 09:11
  • This is a very strange question. First of all - what do you want to manipulate? Why has JavaScript to be active for this manipulation? Why can't you just inject a JavaScript that performs the required manipulation? Also you are using AngleSharp synchronously, which is very bad. Your UI will be hanging. – Florian Rappl Dec 13 '17 at 21:26
  • @FlorianRappl `First of all - what do you want to manipulate?` I have to remove some part of html page. – Ev_Hyper Dec 23 '17 at 06:21
  • @Florian Rappl `Why has JavaScript to be active for this manipulation? `I have to show ads at the same place where it is on original page. – Ev_Hyper Dec 23 '17 at 06:24
  • @FlorianRappl `Why can't you just inject a JavaScript that performs the required manipulation?` because I don't know JS at all - I even not sure is it possible – Ev_Hyper Dec 23 '17 at 06:34
  • `Also you are using AngleSharp synchronously, which is very bad. Your UI will be hanging` yeah, thanks - I know it. It's only for MCVE (my "real" project primarily written in F# ) – Ev_Hyper Dec 23 '17 at 06:38
  • @Ev_Hyper Thanks for the response. If it is possible with AngleSharp (theoretically) it will also be possible with JS. Also note that the API is pretty much the same (except camelCase instead of PascalCase). JS alone should not be the big hurdle here. So the issue with AngleSharp will potentially be the JS integration. Since you want to load the page anyway in a browser, injecting a JS script sounds like the easiest (and most reliable) path to me. – Florian Rappl Dec 23 '17 at 13:33

2 Answers2

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.

Christoph Bimminger
  • 1,006
  • 7
  • 25
-1

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