c# - 如何在 WebBrowser 控件中注入(inject) CSS

标签 c# css winforms

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);

    }


}

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

Error 1 The type or namespace name 'IHTMLStyleSheetElement' could not be found (are you missing a using directive or an assembly reference?) C:\Users\aleffamily\documents\visual studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 30 13 WindowsFormsApplication1

最佳答案

你想用 IHTMLStyleElementIHTMLStyleSheetElement 引用什么?它不在 .NET 框架中。

只需修改您的代码以编写通用 HtmlElement 类型的内容,而不是编写 .InnerText.InnerHtml 的内容你的样式表

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);

    }
}

关于c# - 如何在 WebBrowser 控件中注入(inject) CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56112708/

相关文章:

c# - 在运行时创建多个文本框并从数据库中分配值文本框..我想显示数据库中的所有数据

javascript - 如何更改文本区域中闪烁的光标/插入符号

html - sIFR 3 - 选择器

C# 从后台线程更新 Windows 窗体中的 UI

c# - 面板背景漆不起作用

winforms - 如何更改 BarButtonItem 的背景颜色?

c# - 逆变值类型

c# - LINQ 和可为 null 的参数

C# 在 foreach 中排序

javascript - React - 处理动态内联样式的理想方式