C#:最好的方式注入到CSS MSHTML实例? [英] C#: Best way to inject CSS into MSHTML instance?

查看:202
本文介绍了C#:最好的方式注入到CSS MSHTML实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图注入一些CSS伴随一些其他HTML到C#托管WebBrowser控件。我试图通过底层MSHTML(的DomDocument属性)控制要做到这一点,因为这code是作为一个完整的IE8 BHO的原型不爽。

I'm trying to inject some CSS that accompanies some other HTML into a C# managed WebBrowser control. I am trying to do this via the underlying MSHTML (DomDocument property) control, as this code is serving as a prototype of sorts for a full IE8 BHO.

问题是,虽然我可以注入HTML(通过mydomdocument.body.insertAdjacentHTML)和Javascript(通过mydomdocument.parentWindow.execScript),它是平了拒绝我的CSS code。

The problem is, while I can inject HTML (via mydomdocument.body.insertAdjacentHTML) and Javascript (via mydomdocument.parentWindow.execScript), it is flat-out rejecting my CSS code.

如果我比较包含我想与注射后目标网页的源文件中插入HTML中的字符串时,MSHTML的源将字面上包含一切的除了的为<风格> 元素及其底层源。

If I compare the string containing the HTML I want to insert with the destination page source after injection, the MSHTML's source will literally contain everything except for the <style> element and its underlying source.

CSS的通行证CSS 2.1 W3C验证。它不会做任何事太棘手,除了一些背景图像属性具有图像直接嵌入到CSS(如背景图像:网址(数据:图像/ PNG; BASE64 ...),并注释掉这些行不会改变结果。

The CSS passes W3C validation for CSS 2.1. It doesn't do anything too tricky, with the exception that some background-image properties have the image directly embedded into the CSS (e.g. background-image: url("data:image/png;base64 ...), and commenting out those lines doesn't change the result.

更多奇怪的(而且我不知道这是否是相关的),是,我是有这个上周没有问题。这周我回来给它,开关周围的一些code,它实际注射前处理待注入HTML后,它不再奏效。当然,我认为我的一个变化,都可能是问题,但评论出所有的逻辑和喂养它直串的HTML仍然出现未格式化。在

More strangely (and I am not sure if this is relevant), was that I was having no problems with this last week. I came back to it this week and, after switching around some of the code that handles the to-be-injected HTML before actual injection, it no longer worked. Naturally I thought that one of my changes might somehow be the problem, but after commenting all that logic out and feeding it a straight string the HTML is still appearing unformatted.

在我注入&LT的那一刻,身体GT; 标签,虽然我已经试图注入&LT; HEAD&GT; 而这会见了类似的结果。

At the moment I'm injecting into the <body> tag, though I've attempted to inject into <head> and that's met with similar results.

在此先感谢您的帮助!

汤姆

推荐答案

弄成解决自己:

mshtml.HTMLDocument test = (mshtml.HTMLDocument)webBrowser1.Document.DomDocument;

//inject CSS
if (test.styleSheets.length < 31) { // createStyleSheet throws "Invalid Argument if >31 stylesheets on page

    mshtml.IHTMLStyleSheet css = (mshtml.IHTMLStyleSheet)test.createStyleSheet("", 0);
    css.cssText = myDataClass.returnInjectionCSS(); // String containing CSS to inject into the page
        // CSS should now affect page

} else {
    System.Console.WriteLine("Could not inject CSS due to styleSheets.length > 31");
    return;
}

我没想到的是,createStyleSheet创建的指针仍然是在文档的DOM'活'......因此,你不需要你创建的样式表追加回到其父。我最终通过研究动态CSS code为JavaScript作为实现是pretty很多相同搞清楚了这一点。

What I didn't realize is that createStyleSheet creates a pointer that is still 'live' in the document's DOM... therefore you don't need to append your created stylesheet back to its parent. I ended up figuring this out by studying dynamic CSS code for Javascript as the implementations are pretty much identical.

这篇关于C#:最好的方式注入到CSS MSHTML实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆