Remove tag from WebBrowser Control before displaying

Problem:

I am running a winforms application with a built-in WebBrowser control. I used the registry magic setting to switch this control mode to IE 8 (as indicated here Will IE9 WebBrowser Control support all IE9 features, including SVG? ).

But now, if I go to a site containing a meta tag X-UA-compatible IE = 9 (from http://msdn.microsoft.com/en-us/library/cc288325(v=vs.85).aspx ) my control switches to IE9 mode with a web browser and ignores registry settings.

I want my control to stay in IE8 mode ...

My solution is trying

I tried to remove the meta tag after loading the control (Document_complete) using IHTMLDOMNode.removeChild, but the control does not display the page again.

I tried loading the HTML content manually (using WebClient), deleting the meta tag, and passing it to the webbrowser control (using Document.Write or DocumentText), but this way the control refuses to load any other content (like images).

Help

Now I don’t have enough ideas to write my own HTTPProxy and modify the answer along the way (which I would not want to do).

Any ideas?

I am using .Net 4, I cannot change the website to be displayed, and I need to render it in IE8 mode, regardless of the X-UA-Compatible tag ...

Thanks!

+4
source share
2 answers

I had problems with DocumentText too - I refused it.

My solution was to write a non-working HTTP server and point it to WebBrowser .

I wrote an article about it here: http://SimplyGenius.net/Article/WebBrowserEx

In my case, I was getting content from the file system.

You will have to change it to make calls to your target site, but it should not be too much.

Then you can change the HTML as you like, and the links will still work.

+3
source

I do not know how to force the WebBrowser control to ignore this tag and not override the registry settings. For a quick (dirty) workaround, you can do the following.

Create a query for the site that you want to display in the WebBrowser control.

 var requestUri = new Uri("http://stackoverflow.com/"); var request = (HttpWebRequest) WebRequest.Create(requestUri); 

Get an answer.

 var response = request.GetResponse(); using (var stream = response.GetResponseStream()) using (var reader = new StreamReader(stream)) { var html = reader.ReadToEnd(); //... } 

Use NuGet to install HTMLAgilityPack.

http://nuget.org/packages/HtmlAgilityPack

Download the HTML that you just returned into the HtmlDocument instance.

 var document = new HtmlAgilityPack.HtmlDocument(); document.LoadHtml(html); 

Select a tag. Here I use StackOverflow.com as an example and instead select its node stylesheet. When found, just remove the node.

 var nodes = document.DocumentNode.SelectNodes("//link[@rel=\"stylesheet\"]"); foreach(var node in nodes) { node.ParentNode.RemoveChild(node); } 

All that remains is to get the modified HTML code and directly pass it to the WebBrowser control.

 html = document.DocumentNode.OuterHtml; webBrowser.DocumentText = html; 

He cannot interpret that which does not exist.

You can do the same to solve your problem. Run the request, get the answer, change the HTML and submit it to the WebBrowser control. The tested seems to load the rest of the document in order.

+2
source

Source: https://habr.com/ru/post/1415424/


All Articles