简体   繁体   中英

How to replace a HTML-tags inner text content using C#!

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

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");
    }
    ...
}

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)

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.

可能你应该使用innerText而不是innerHTML属性,然后你就可以删除这个条件:((IHTMLElementCollection)node.children)。length == 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM