开发者

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

开发者 https://www.devze.com 2023-01-18 03:48 出处:网络
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.

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", "re开发者_运维技巧placeWithThis");
    }

    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


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)


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


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消