简体   繁体   English

如何将内联样式添加到WPF WebBrowser中加载的文档中?

[英]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.

相关问题 如何在WPF中打开默认的Web浏览器? - How can I open my default webbrowser in WPF? 如何检查Webbrowser文档是否已完成加载? - how to check the Webbrowser document is completed loaded? 如何获取由我的wpf应用程序打开的文件路径文档? - How do i get the file path document that is being open by my wpf application? 如何在WPF WebBrowser中运行Prettify来应用语法突出显示? - How can I run Prettify in a WPF WebBrowser to apply syntax highlighting? 如何从WPF WebBrowser获取HtmlElementCollection - How can I get an HtmlElementCollection from a WPF WebBrowser 如何在WPF WebBrowser中清除特定站点的cookie? - How can I clear cookies in the WPF WebBrowser for a specific site? 如何在c#中的wpf webbrowser控件中注入js文件 - How to inject js file in wpf webbrowser control in c# 如何在不修改webbrowser控件中的文档的情况下注入并执行javascript函数? - How to inject and execute a javascript function without modifiting document in webbrowser control? 如何检测网页是否在WebBrowser控件中呈现? - How can I detect if the webpage is being rendered in WebBrowser Control? 我可以在WPF中使WebBrowser半透明和无边界吗? - Can I make WebBrowser translucent and borderless in WPF?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM