简体   繁体   English

在渲染之前从WebBrowser控件中删除标记

[英]Delete tag from WebBrowser Control before rendering

The Problem: 问题:

I'm running a winforms application with an embedded WebBrowser control. 我正在运行带有嵌入式WebBrowser控件的winforms应用程序。 I've used the magic registry setting to switch this Control to IE 8 mode (as answered here Will the IE9 WebBrowser Control Support all of IE9's features, including SVG? ). 我已经使用魔术注册表设置将此控制切换到IE 8模式(如下所述,IE9 WebBrowser控件是否支持所有IE9的功能,包括SVG? )。

But now if I navigate to a website which contains the Meta tag X-UA-Compatible IE=9 (as of http://msdn.microsoft.com/en-us/library/cc288325(v=vs.85).aspx ) my webbrowser control switches to IE9 mode and ignores the registry settings. 但现在如果我导航到一个包含Meta标签的网站X-UA-Compatible IE = 9(截至http://msdn.microsoft.com/en-us/library/cc288325(v=vs.85).aspx )我的webbrowser控件切换到IE9模式并忽略注册表设置。

I would like my control to stay in IE8 mode... 我希望我的控制能够保持在IE8模式......

My solution attempts 我的解决方案尝试

I've tried to remove the meta tag after the control has loaded (Document_complete) using IHTMLDOMNode.removeChild but the control does not re-render the page. 我已经尝试在使用IHTMLDOMNode.removeChild加载控件(Document_complete)后删除元标记,但控件不会重新呈现页面。

I've tried to load the HTML content manually (using WebClient), remove the meta tag and feed this into the the webbrowser control (using Document.Write or DocumentText) but this way the control refuses to load any other content (like images). 我试图手动加载HTML内容(使用WebClient),删除元标记并将其提供到webbrowser控件(使用Document.Write或DocumentText),但这样控件拒绝加载任何其他内容(如图像) 。

Help 救命

Now I'm out of ideas short of writing my own HTTPProxy and modifiying the response on the way (which I would not like to do). 现在我的想法不是编写我自己的HTTPProxy并修改路上的响应(我不想这样做)。

Anyone any ideas? 任何想法?

I'm using .Net 4, I cannot change the website which will be displayed and I need it to render in IE8 mode regardless of the X-UA-Compatible tag... 我正在使用.Net 4,我无法更改将要显示的网站,无论X-UA兼容标签如何,我都需要在IE8模式下进行渲染...

Thanks! 谢谢!

I had problems with DocumentText too - I gave up with it. 我也遇到了DocumentText问题 - 我放弃了它。

My solution was to write an in-process HTTP server and point the WebBrowser at that. 我的解决方案是编写进程内HTTP服务器并将WebBrowser指向该服务器。

I wrote an article about it here: http://SimplyGenius.net/Article/WebBrowserEx 我在这里写了一篇关于它的文章:http: //SimplyGenius.net/Article/WebBrowserEx

In my case, I was getting the content from the file system. 就我而言,我从文件系统中获取内容。
You'd have to change it to make calls to your target website, but it shouldn't be too much work. 您必须更改它以拨打您的目标网站,但它不应该太多工作​​。
Then you can modify the HTML as you like, and links will still work. 然后,您可以根据需要修改HTML,链接仍然可以使用。

Don't know of a way to make the WebBrowser control ignore that tag and not override your registry setting. 不知道如何使WebBrowser控件忽略该标记而不覆盖您的注册表设置。 For a quick (dirty) workaround you could do the following. 对于快速(脏)的解决方法,您可以执行以下操作。

Create a request for the site which you want to show in the WebBrowser control. 创建要在WebBrowser控件中显示的站点的请求。

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

Get the response. 得到回应。

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

Use NuGet to install the HTMLAgilityPack. 使用NuGet安装HTMLAgilityPack。

http://nuget.org/packages/HtmlAgilityPack http://nuget.org/packages/HtmlAgilityPack

Load the HTML you've just retrieved in an HtmlDocument instance. 在HtmlDocument实例中加载刚刚检索到的HTML。

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

Select the tag. 选择标签。 Here I use StackOverflow.com as an example and select its stylesheet node instead. 在这里,我使用StackOverflow.com作为示例,并选择其样式表节点。 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 retrieve the modified HTML and feed it directly to the WebBrowser control. 剩下的就是检索修改后的HTML并将其直接提供给WebBrowser控件。

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

It cannot interprete what's not there. 它无法解释那里没有的东西。

You could do the same to solve your issue. 你也可以这样做来解决你的问题。 Issue a request, get the response, modify the HTML and feed it to the WebBrowser control. 发出请求,获取响应,修改HTML并将其提供给WebBrowser控件。 Tested it, seems to load the rest of the document OK. 测试它,似乎加载文件的其余部分确定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM