如何使用 C# 替换 HTML 标签内部文本内容!

发布于 2024-09-24 16:30:01 字数 1054 浏览 1 评论 0原文

现在我正在开发一个 Internet Explorer 插件,它应该扫描 HTML 文档中的纯文本 URL,然后“链接”它们。

我可以访问网站 DOM,并且想遍历所有 DOM 节点并使用 RegEx 搜索“链接”,以用 HTML 代码替换这些文本,但是,当更改 IHTMLElement 对象的“InnerText”属性时,它的所有子节点都丢失了,这严重破坏了网站。

这是一些代码:

//This method is called when IE has finished loading a page
void _webBrowser2Events_DocumentComplete(object pDisp, ref object URL)
{
    if (pDisp == _webBrowser2)
    {
        HTMLDocument pageContent = _webBrowser2.Document;
        IHTMLElement bodyHtmlElmnt = pageContent.body;
        fixElement(bodyHtmlElmnt);
    }   
}

这是 fixElement-method:

void fixElement(IHTMLElement node)
{
    if (node.innerText!=null && ((IHTMLElementCollection)node.children).length==0)
    {
        node.innerText= node.innerText.Replace("testString", "replaceWithThis");
    }

    foreach (IHTMLElement child in (node.children as mshtml.IHTMLElementCollection))
    {
        fixElement(child);
    }
}

这有效,但仅适用于没有任何子节点的节点。

谁能帮我解决这个问题,我将不胜感激!

问候

//亨里克

Right now I'm working on a Internet Explorer add on which is supposed to scan a HTML-document for URL's in plain text, and then "linkify" them.

I have access to the websites DOM, and had an idea to traverse all of the DOM nodes and search for "links" using RegEx, to replace these text with HTML-code, however, when changing the "InnerText" property of the IHTMLElement object, all of it's child nodes are lost, which seriously f*cks up the website.

Here's some code:

//This method is called when IE has finished loading a page
void _webBrowser2Events_DocumentComplete(object pDisp, ref object URL)
{
    if (pDisp == _webBrowser2)
    {
        HTMLDocument pageContent = _webBrowser2.Document;
        IHTMLElement bodyHtmlElmnt = pageContent.body;
        fixElement(bodyHtmlElmnt);
    }   
}

And here's the fixElement-method:

void fixElement(IHTMLElement node)
{
    if (node.innerText!=null && ((IHTMLElementCollection)node.children).length==0)
    {
        node.innerText= node.innerText.Replace("testString", "replaceWithThis");
    }

    foreach (IHTMLElement child in (node.children as mshtml.IHTMLElementCollection))
    {
        fixElement(child);
    }
}

This works, but only for nodes that doesn't have any children.

Can anyone please help me with this problem, I would be very grateful!

Regards

//Henrik

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

谁与争疯 2024-10-01 16:30:01

为什么你不想像这样使用 javscript
http://userscripts.org/scripts/review/1352
然后只需使用您的 C# 代码执行此 javascript。
这样做的好处

webBrowser1.Navigate(new Uri("javascript:<YOURSCRIPT>"));

是你可以做很多事情,甚至不需要重新发明它们,url链接化很早以前就由javascript人发明了,所以只需使用该代码..

如果有任何脚本(比如这个很大),那么你可以从*.js 文件使用此脚本)

javascript:(function(){document.body.appendChild(document.createElement('script')).src='<YOUR SCRIPT URL>';})();

替换为托管在互联网或本地的 JavaScript(如果本地使用 file:// url 格式)

Why you dont want to use javscript like this
http://userscripts.org/scripts/review/1352
Then just execute this javascript using your c# code.
just

webBrowser1.Navigate(new Uri("javascript:<YOURSCRIPT>"));

Good thing about this is you can do many things without even re-inventing them , url linkification is long back invented by javascript people, so just use that code..

If any script (like this one is big , then you can insert from *.js file using this script)

javascript:(function(){document.body.appendChild(document.createElement('script')).src='<YOUR SCRIPT URL>';})();

replace with your javascript hosted on internet OR localy (if local use file:// url format)

不可一世的女人 2024-10-01 16:30:01

好吧,对我来说很明显(但我没有测试它),您应该

删除((IHTMLElementCollection)node.children).length==0

从方法 fixElement 的第一行

void fixElement(IHTMLElement node)
{
    if (node.innerText!=null) // && ((IHTMLElementCollection)node.children).length==0)
    {
         node.innerText= node.innerText.Replace("testString", "replaceWithThis");
    }
    ...
}

Well, it seems obvious to me (But I didn't tested it), that you should remove

((IHTMLElementCollection)node.children).length==0

from the first line of method fixElement:

void fixElement(IHTMLElement node)
{
    if (node.innerText!=null) // && ((IHTMLElementCollection)node.children).length==0)
    {
         node.innerText= node.innerText.Replace("testString", "replaceWithThis");
    }
    ...
}
埖埖迣鎅 2024-10-01 16:30:01

您可以做的是将子节点存储在临时 IHTMLElement 中并更改所需的元素,然后您可以将节点再次注入到更改的元素中。

我希望它有帮助。

What you can do is to store the child nodes in temp IHTMLElement and change the desired element and then you can inject the nodes back again into the changed element.

I hope it helps.

心不设防 2024-10-01 16:30:01

也许您应该使用innerText而不是innerHTML属性,然后您将能够删除此条件:((IHTMLElementCollection)node.children).length==0

Probably you should use innerText instead of innerHTML property, and then you'll be able to remove this condition: ((IHTMLElementCollection)node.children).length==0

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文