Persisting Changes Made in WebBrowser back to source file

C

c j anderson, mcp

I recently had a need to be able to highlight selected text in a web
browser control (working code is posted below). Attempts to access the
modified html source through the webbrowser or the mshtml COM interface
(see below), however, have failed and I resorted to inserting the
changes into the file by the means shown... This method is not 100%
successful because the innerHTML reported by the COM object is
*different* from what is actually in the source (spaces are
added/removed; elements & attributes are sometime capitalized; etc.) It
seems like there should be a reliable way to do this via either the
WebBrowser Windows Form Control or the MSHTML component -- does anyone
know of a better way?

Private Sub HighlightToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
HighlightToolStripMenuItem.Click
'we have to resort to COM to play with the underlying HTML in the
browser

'get the IHTMLDocument2 object
Dim htmldoc2 As mshtml.IHTMLDocument2
htmldoc2 = CType(WebBrowser1.Document.DomDocument,
mshtml.IHTMLDocument2)

'get the IHTMLTextRange object
Dim textrange As mshtml.IHTMLTxtRange =
CType(htmldoc2.selection.createRange, mshtml.IHTMLTxtRange)

If (Not String.IsNullOrEmpty(textrange.htmlText)) Then

Dim strhtmlbrowser As String = textrange.htmlText
Dim strhtmlbrowserlc As String = strhtmlbrowser.ToLowerInvariant()

'keywords file needs to be loaded to get list of allowed colors
Dim frm As New frmColors(arstrAllowedColors)
frm.ShowDialog()

textrange.pasteHTML("<span style=""background-color: " &
frm.SelectedColor & """>" & strhtmlbrowser & "</span>")
frm.Close()

textrange = CType(htmldoc2.selection.createRange,
mshtml.IHTMLTxtRange)

Dim strhtmlui As String = txtHTML.Text
Dim strhtmluilc As String = strhtmlui.ToLowerInvariant()
If (strhtmluilc.IndexOf(strhtmlbrowserlc) <> -1) Then
'work from back to keep index valid
strhtmlui = strhtmlui.Insert(strhtmluilc.IndexOf(strhtmlbrowserlc) +
strhtmlbrowser.Length, "</span>")
strhtmlui = strhtmlui.Insert(strhtmluilc.IndexOf(strhtmlbrowserlc),
"<span style=""background-color: yellow"">")

txtHTML.Text = strhtmlui

End If
End If

End Sub

Cheers.
 
C

c j anderson, mcp

I still haven't found a better solution... any ideas on how to make
changes in the browser show in the source view? the modified HTML
source code must exist somewhere...

Cheers.
 
C

Charles Law

It wasn't completely clear to me what your actual requirement and/or
subsequent problem was from your post.

It sounds like you want to highlight some selected text (presumably in a
manner other than normal highlighting), and that you resorted to a technique
that itself has problems. Is that correct?

If I have summarised correctly then I would not ordinarily highlight text by
modifying the underlying html. Instead I would use one of two methods.

To highlight an element, I would use a runtime style. This available on the
IHTMLElement2 interface. Use the runtimeStyle property to set
backgroundColor and color.

To highlight individual bits of text, I would use rendering services,
available on the IHTMLRenderingServices interface. This technique is a lot
more complicated, but may be more what you are looking for. Essentially, you
create markup pointers to define the start and end of your selection, and
place a display pointer at each location. You then create a render style in
which you define the background and foreground colours, and then call
AddSegment on the IHTMLRenderingServices interface. MSDN has a sketchy
outline of how to do this. Feel free to post back if this is the way you
want to go.

HTH

Charles
 
C

c j anderson, mcp

thanks for the reply.

my need is to change what is displayed in the browser and then save it
back to file. the problem is not with the pasteHtml (per se), but
rather with being able to see the changes... if I select "View Source"
from the context menu, I only see the original document, not the
changes...

having looked at the source that comes from the serviced component, I
doubt that I will be able to use it (it's changing the underlying source
in ways that makes it invalid XHTML Strict)

I will look into the methods that you suggested though to see if they
simplify the process from what I am currently employing.

Cheers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top