首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在WebBrowser控件中注入CSS

如何在WebBrowser控件中注入CSS
EN

Stack Overflow用户
提问于 2019-05-13 20:47:50
回答 1查看 557关注 0票数 0
代码语言:javascript
复制
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        webBrowser1.Navigate("https://www.aparat.com/aleffamily/live2");
        webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;

    }

    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {

        HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
        HtmlElement styleEl = webBrowser1.Document.CreateElement("style");
        IHTMLStyleElement element = (IHTMLStyleElement)styleEl.DomElement;
        IHTMLStyleSheetElement styleSheet = element.styleSheet;
        styleSheet.cssText = @"body {background-color:transparent !important; margin: 0px auto; overflow: hidden; }.live__content{display:none;}.chat__footer{display:none;}.chat__header{display:none}#header{display:none}.in-chat-avatar{display:none}.message__username{font-style:italic;font-weight: 800!important;color:ff8f0f !important}.message__text{font-style:italic;font-weight: 400!important;color:ff8f0f !important}.chat__content{ background-color:transparent  !important}.chat{ background-color:transparent  !important}";
        head.AppendChild(styleEl);

    }


}

}

这段代码对我不起作用,我得到了这个错误

错误1找不到类型或命名空间名称“IHTMLStyleSheetElement”(是否缺少using指令或程序集引用?)C:\Users\aleffamily\documents\visual studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 30 13 WindowsFormsApplication1

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-14 03:27:08

您试图在IHTMLStyleElementIHTMLStyleSheetElement中引用什么?它不是.NET框架中的任何地方。

只需修改代码以编写泛型HtmlElement类型的内容,即可使用样式表编写.InnerText.InnerHtml的内容

代码语言:javascript
复制
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        webBrowser1.Navigate("https://www.aparat.com/aleffamily/live2");
        webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
    }

    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {

        HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
        HtmlElement styleEl = webBrowser1.Document.CreateElement("style");
        styleEl.InnerHtml = @"body {background-color:transparent !important; margin: 0px auto; overflow: hidden; }.live__content{display:none;}.chat__footer{display:none;}.chat__header{display:none}#header{display:none}.in-chat-avatar{display:none}.message__username{font-style:italic;font-weight: 800!important;color:ff8f0f !important}.message__text{font-style:italic;font-weight: 400!important;color:ff8f0f !important}.chat__content{ background-color:transparent  !important}.chat{ background-color:transparent  !important}";
        head.AppendChild(styleEl);

    }
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56112708

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档