C# .NET - How to set 'innerText' property of an HTMLElement ?

Asked By svt gdwl on 28-May-09 01:13 AM
I had a small application of BHO which retreives each and every element of a web page opened in IE.
I want to change the text that is visible on web page, as the changed text should be directly visible

to user instead of original text.
How can I do this in c#?If I try to change/assign the innerText of an HTMLELement in the following

ways, the original text is visible for a fraction of seconds initially, and then it is replaced by the

"New text".


1.  htmlElement.innerText = htmlElement.innerText.Replace(htmlElement.innerText, "New Text");
           
2.           htmlElement.innerText = "New Text";





If I try to set the innertext property in the following way

             object obj = "New text";
            htmlElement.setAttribute("innerText", obj,0);

it doesn't show any effect,
I think I was missing something. How can I do this?    
Santhosh N replied to svt gdwl on 28-May-09 01:27 AM

In which event you are doing all this...

I see its not a problem with innerText but where you are doing this...

And also, its better you hide the IE for a while if you can so that there won't be any updatations...

How to set 'innerText' property of an HTMLElement

svt gdwl replied to Santhosh N on 28-May-09 01:34 AM
I was doing all these in ProgressChangeEvent.
I tried to do the same in DownloadBeginEvent, but I was unable to get the text of web page.
If I do this in DownloadCompleteEvent, the result is same as delay in displaying replaced text.

How can I hide IE for a while?
Santhosh N replied to svt gdwl on 28-May-09 01:57 AM

I am not particularly sure and not aware of the code you are having but otherwise, this should give you some idea which is talking boaut hiding toolbars, creating new ones, etc..which might give you an idea as you are really working on this...

cheers

How to set 'innerText' property of an HTMLElement
svt gdwl replied to Santhosh N on 28-May-09 02:03 AM
http://www.codeproject.com/KB/cs/Attach_BHO_with_C_.aspx?fid=447248&df=90&mpp=25&noise=3&sort=Position&view=Quick&fr=51&select=2421069

My BHO is built using above link
Re
Ravenet Rasaiyah replied to svt gdwl on 28-May-09 02:24 AM
Hi

I think you are method is wrong . you need write like this way

private void IWriteAEmlement()
{
if (webBrowser1.Document != null)
{
HtmlDocument doc = webBrowser1.Document;
HtmlElement elem = doc.CreateElement("input");
elem.SetAttribute("id", "test1");
elem.SetAttribute("type", "text");
object obj = "New text";
elem.SetAttribute("value", obj.ToString());
doc.Body.AppendChild(elem);
}
}


thank you
As far as I know
Santhosh N replied to Ravenet Rasaiyah on 28-May-09 02:29 AM

seeing this code, this should add a new element to the HTML DOM rather than replacing the text of the existing element in the HTML Body...

DO let me know if its not....

cheers

Re
Ravenet Rasaiyah replied to Santhosh N on 28-May-09 02:49 AM
Yes,

we need to iterate through every elements. and make in with in the document complete event


void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
PrintDomBegin();
}
private void PrintDomBegin()
{

if (webBrowser1.Document != null)
{
HtmlElementCollection elemColl = null;
HtmlDocument doc = webBrowser1.Document;
if (doc != null)
{
elemColl = doc.GetElementsByTagName("HTML");
String str = PrintDom(elemColl, new System.Text.StringBuilder(), 0);
webBrowser1.DocumentText = str;
}
}
}

private string PrintDom(HtmlElementCollection elemColl, System.Text.StringBuilder returnStr, Int32 depth)
{
System.Text.StringBuilder str = new System.Text.StringBuilder();
foreach (HtmlElement elem in elemColl)
{

elem.InnerText.Replace("New Text", "0");
if (elem.CanHaveChildren)
{
PrintDom(elem.Children, returnStr, depth + 1);
}

}
return (returnStr.ToString());
}

thank you
yeah...
Santhosh N replied to Ravenet Rasaiyah on 28-May-09 03:07 AM

But the problem is not what you are talking about, but with the rendering time...

He is able to do this from his end but wanted to know how to avoid the flickering of the browser during this transition from original text to the updated one

I hope its clear now

cheers