[英]How can I inject inline styling into a Document being loaded in my WPF WebBrowser?
I've been trying to use mshtml to modify a third party web api. 我一直在尝试使用mshtml修改第三方Web API。 Right now I am trying to change the display property on two elements to none so they will be invisible.
现在,我试图将两个元素的显示属性更改为无,以便它们不可见。
I know their id's. 我知道他们的身份证。
The first one is an img
and the id is zendbox_close
. 第一个是
img
,id是zendbox_close
。 The second is a div
and the id is zenbox_scrim
. 第二个是
div
,id是zenbox_scrim
。
The html looks like this HTML看起来像这样
<div class="zenbox_header">
<img id="zenbox_close"></img>
</div>
...
<div id="zenbox_scrim...></div>
All I want to do is add in some inline styling so it looks like this 我要做的就是添加一些内联样式,因此看起来像这样
<div class="zenbox_header">
<img id="zenbox_close" style="display:none;"></img>
</div>
...
<div id="zenbox_scrim style="display:none;"...></div>
In my code behind for the WPF WebBrowser
that is opening up the webpage, I have gotten this far: 在我打开网页的WPF
WebBrowser
代码中,我已经做到了这一点:
IHTMLDocument3 doc = (IHTMLDocument3)this._browser.Document;
IHTMLImgElement element = (IHTMLImgElement)doc.getElementById("zenbox_close");
I saw in another post someone was talking about injecting scripts and they said you can use 我在另一篇文章中看到有人在谈论注入脚本,他们说您可以使用
IHTMLElement scriptEl = doc.CreateElement("script");
I am not sure what the HTML element analog to this would be though. 我不确定与此类似的HTML元素是什么。 Also I had to use
IHTMLDocument3
to use the method getElementById
, but that class doesn't appear to contain anything similar to CreateElement()
. 另外,我还必须使用
IHTMLDocument3
来使用方法getElementById
,但是该类似乎不包含任何类似于CreateElement()
。
My question is how can I inject inline styling into a Document
being loaded in my WPF
WebBrowser
? 我的问题是如何将内联样式注入正在
WPF
WebBrowser
加载的Document
中?
Yes you can manipulate styles inline. 是的,您可以内联操作样式。
A good way to do this is to work with the IHTMLStyle or IHTMLCurrentStyle interfaces when working with an IHTMLElement. 一种好的方法是在使用IHTMLElement时使用IHTMLStyle或IHTMLCurrentStyle接口。 There are some differences with the values reflected by those two and they are not always synch'd.
这两个值所反映的值存在一些差异,并且它们并不总是同步的。 A better explanation of why that is:
对此的更好解释是:
IHTMLStyle vs IHTMLCurrentStyle IHTMLStyle与IHTMLCurrentStyle
Code sample would look like: 代码示例如下所示:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
wb.LoadCompleted += wb_LoadCompleted;
wb.Source = new Uri("http://www.google.com");
}
void wb_LoadCompleted(object sender, NavigationEventArgs e)
{
var doc = wb.Document as HTMLDocument;
var collection = doc.getElementsByTagName("input");
foreach (IHTMLElement input in collection)
{
dynamic currentStyle = (input as IHTMLElement2).currentStyle.getAttribute("backgroundColor");
input.style.setAttribute("backgroundColor", "red");
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.