【问题标题】:How to inject CSS in WebBrowser control?如何在 WebBrowser 控件中注入 CSS?
【发布时间】:2011-07-26 16:13:28
【问题描述】:

据我所知,有一种方法可以将 javascript 注入 DOM。下面是使用 webbrowser 控件注入 javascript 的示例代码:

HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
HtmlElement scriptEl = webBrowser1.Document.CreateElement("script");
IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
element.text = "function sayHello() { alert('hello') }";
head.AppendChild(scriptEl);
webBrowser1.Document.InvokeScript("sayHello");

有没有更简单的方法将 css 注入到 DOM 中?

【问题讨论】:

    标签: c# .net winforms webbrowser-control


    【解决方案1】:

    我自己没有尝试过,但由于 CSS 样式规则可以使用 <style> 标记包含在文档中,如下所示:

    <html>
    <head>
    <style type="text/css">
        h1 {color:red}
        p {color:blue}
    </style>
    </head>
    

    你可以尝试给予:

    HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
    HtmlElement styleEl = webBrowser1.Document.CreateElement("style");
    IHTMLStyleElement element = (IHTMLStyleElement)styleEl.DomElement;
    IHTMLStyleSheetElement styleSheet = element.styleSheet;
    styleSheet.cssText = @"h1 { color: red }";
    head.AppendChild(styleEl);
    

    去吧。您可以在 IHTMLStyleElement here 上找到更多信息。

    编辑

    看来答案比我原先想的要简单得多:

      using mshtml;
    
      IHTMLDocument2 doc = (webBrowser1.Document.DomDocument) as IHTMLDocument2;
      // The first parameter is the url, the second is the index of the added style sheet.
      IHTMLStyleSheet ss = doc.createStyleSheet("", 0);
    
      // Now that you have the style sheet you have a few options:
      // 1. You can just set the content as text.
      ss.cssText = @"h1 { color: blue; }";
      // 2. You can add/remove style rules.
      int index = ss.addRule("h1", "color: red;");
      ss.removeRule(index);
      // You can even walk over the rules using "ss.rules" and modify them.
    

    我编写了一个小型测试项目来验证它是否有效。我通过在 MSDN 上搜索 IHTMLStyleSheet 得出了这个最终结果,我遇到了 this pagethis pagethis one

    【讨论】:

    • paracycle,元素的智能感知,没有.text ..这就是我现在挠头的原因...... :(
    • 是的,再看一下参考文档,似乎中间还有一层。我更改了代码 sn-p 以符合该要求。我现在没有测试代码的环境,所以请评论你的结果。
    • 没有 IHTMLStyleSheetElement 但有 IHTMLStyleSheet,我尝试过使用 IHTMLStyleSheet。它抛出 NullReferenceException 错误,styleSheet 未设置为对象的实例。元素的样式表为空。 :(
    • 我将设置一个测试环境并相应地更新我的答案。创建一个新的 HTMLStyleSheet 元素可能很简单。
    • 两件事。首先:请提及引用的程序集和 using 语句。否则很难使用你的代码。第二:在第一行中,最后必须是IHTMLDocument2,而不是HTMLDocument2。要添加的程序集是 MSHTML(Microsoft HTML 对象库)。然后添加using mshtml;
    【解决方案2】:

    对我来说,这似乎就像在 DocumentText. 中首先设置我的样式一样简单,显然不是最佳实践,但它适用于简单的 CSS。

    webBrowser1.DocumentText = "<style> " +
                                     "body { " +
                                        "font-family: Algerian; " +
                                      "} " +
                                "</style> "+
                                "<a href='https://www.google.ca'>Test</a>";
    

    【讨论】:

      猜你喜欢
      • 2019-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-19
      相关资源
      最近更新 更多