使用mshtml HTMLDocument修改DOM并在IE浏览器扩展/ BHO中显示
我正在使用 C#.NET:mshtml.HTMLDocument、mshtml.HTMLImg 等。
我想做的是修改 DOM 并反映浏览器中的更改。例如
HTMLDocument doc = WebBrowser.Document as HTMLDocument;
HTMLImg img;
for (int i = 0; i < doc.images.length; i++)
{
img = doc.images.item(i) as HTMLImg;
ProcessImg(ref img);
}
,不幸的是,处理/修改的图像不会显示在浏览器中。我想我需要在本地保存图像并更改 IMG 标签的 src 属性。但除了没有成功(请帮忙!)之外,我担心它也不会显示。我如何告诉浏览器更新(如果有必要的话)
干杯,伙计们! 伯特 你会采取什么方法?
I'm working with C#.NET: mshtml.HTMLDocument, mshtml.HTMLImg etc.
What I'm trying to do is modify the DOM and reflect the change in the browser. e.g.
HTMLDocument doc = WebBrowser.Document as HTMLDocument;
HTMLImg img;
for (int i = 0; i < doc.images.length; i++)
{
img = doc.images.item(i) as HTMLImg;
ProcessImg(ref img);
}
Unfortunately the processed/modified image is not displayed in the browser. I suppose Ineed to I save the image locally and change the src attrib of the IMG tag. But apart from not successding (help please!) to do so, I fear that it won't be displayed as well. How do I tell the browser to update (if necessary at all)
Cheers, folks!
Bert
What approach would you take?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正确,您无法在不更改 SRC 属性的情况下更改现有图像。您应该分享您的不起作用的代码以供人们查看。
请记住,出于性能和可靠性原因,强烈建议不要在 .NET 中编写 IE 的浏览器扩展。
Correct, you can't change an existing image without changing the SRC attribute. You should share your not-working code for that for folks to see.
Please keep in mind that writing browser extensions for IE in .NET is strongly discouraged for performance and reliability reasons.
检查这个链接,绝对可以修改显示的页面,因为我正在使用我正在开发的插件来完成此操作。
基本上,该类实现了 IObjectWithSite 和 IOleCommandTarget,前者允许您的代码订阅浏览操作,后者连接 UI 操作。示例链接中的代码设置/清除 HTMLDocument,它是您要修改的实际文档。
编辑:
我认为尽管 HTMLDocument 看起来并不在 mshtml 命名空间中,但实际上在 System.Windows.Forms 中。 R# 的说法并非如此,但整个 mshtml 命名空间感觉就像是一种间接误导的练习,其中还加入了重定向和消化不良。
Check this link out, its definitely possible to modify the displayed page, as I am doing it with a plugin I am working on.
Basically the class implements IObjectWithSite and IOleCommandTarget, the former allowing your code to subscribe to browsing action and the latter hooking up the UI action. Code in the example link sets/clears an HTMLDocument that is the actual document you want to modify.
edit:
I think that despite appearances HTMLDocument is not in the mshtml namespace, but actually in System.Windows.Forms. R# says otherwise, but the whole mshtml namespace feels like an exercise in misdirection by indirection with redirection and indigestion thrown in for good measure.