现在我正在开发一个 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 方法:
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);
}
}
这有效,但仅适用于没有任何子节点的节点。
谁能帮我解决这个问题,我将不胜感激!
问候
//亨里克