views:

103

answers:

3

Hello!

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

+1  A: 

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.

Braveyard
Sounds good! Although I've looked everywhere in the documentation, I can't seem to find how to inject my child nodes back to the element!
nelshh
+1  A: 

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

ika
Sorry, used the wrong version, it's fixed now! When using innerText, the child nodes are destroyed!
nelshh
+1  A: 

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)

Markandey Singh