Edit HTML In WebBrowser

Edit page source and display in webbrowser

Page 1 of 1

4 Replies - 14423 Views - Last Post: 11 September 2011 - 10:50 PM Rate Topic: -----

#1 matto  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 18-April 09

Edit HTML In WebBrowser

Post icon  Posted 18 July 2009 - 05:01 AM

Hi,

I want to be able to edit the HTML page source from a webbrowser control. E.g. the webbrowser navigates to http://www.google.com and I want to be able to edit the text between "<title>" and "</title>"

I have managed to grab the HTML and display it in a textbox:

Private Function GetDocument() As mshtml.HTMLDocument
Try
Dim htm As mshtml.HTMLDocument = WebBrowser1.document.DomDocument
GetDocument = htm
Catch
Throw (New Exception("Cannot retrieve the document from the
WebBrowser" + _
"Control: " + Err.GetException().Message))
End Try
End Function
textbox1.Text = htm


Any ideas? Thanks,

matto

Is This A Good Question/Topic? 0
  • +

Replies To: Edit HTML In WebBrowser

#2 LoveIsNull  Icon User is offline

  • Recovering D.I.C Addict
  • member icon

Reputation: 52
  • View blog
  • Posts: 646
  • Joined: 10-March 09

Re: Edit HTML In WebBrowser

Posted 18 July 2009 - 11:36 AM

I don't work with the WebBrowser control very much at all, but here is a quick example that should find and replace virtually whatever value in whatever tag you specify.
It uses IndexOf, Substring and Replace methods of the String class. You might want to read about the System.String class and the other methods it has to offer for these purposes.
	Private strNewTitle As String = "New Title" 'Whatever value is going between the tags, a title in this case
	Private strTag As String = "title"		  'The tag to look for

	Private Sub webOriginal_Navigated(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserNavigatedEventArgs) Handles webOriginal.Navigated
		Dim strSource As String = webOriginal.DocumentText.ToLower  'Get the source
		Dim iPos As Integer = 0 'Position counter (if replacing all tag occurrences)
		Dim strStartTag As String = "<" & strTag & ">"  'Form the start and end tags
		Dim strEndTag As String = "</" & strTag & ">"
		'Use a loop if we want to replace all occurrences
		Do 
			'Figure out where to begin
			Dim iBeg As Integer = strSource.IndexOf(strStartTag)
			'Figure out where our </tag> starts, adjust to get the correct end position
			Dim iEnd As Integer = strSource.IndexOf(strEndTag) + strTag.Length + 3
			iPos = iEnd 'This is where we'll pick up next time if looking for other occurrences
			'Get the <tag> we're looking for and whatever is in between
			Dim strTitleFull As String = strSource.Substring(iBeg, iEnd - iBeg)
			'Just give ourselves a not of what was found
			Debug.WriteLine("Found: " & strTitleFull) '"<" & strTag & ">" & strNewTitle & "</" & strTag & ">"
			'Replace it with what we've specified
			strSource = strSource.Replace(strTitleFull, strStartTag & strNewTitle & strEndTag)
			'Take a look at it in its entirety
			txtSource.Text = strSource
		Loop Until iPos >= strSource.LastIndexOf(strEndTag)

	End Sub

Was This Post Helpful? 0
  • +
  • -

#3 matto  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 18-April 09

Re: Edit HTML In WebBrowser

Posted 18 July 2009 - 12:18 PM

Thanks tonnes, just what I was looking for :)

I would like to be able to display the edited HTML from the textbox in the browser now. Is this possible? One way I know is saving the contents of the textbox to a file and then displaying it, but is there an easier method?

Also, I want to search between these two tags: <li class="list"> and </li>

However, in I don't know how to use this in VB. Should I change "list" to 'list'? Would that still work?

Thanks again :)
Was This Post Helpful? 0
  • +
  • -

#4 Guest_Bryce Gough*


Reputation:

Re: Edit HTML In WebBrowser

Posted 19 February 2011 - 05:05 AM

Try this to show it in the browser:

Private Sub WebHTMLChange(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Textbox1.TextChanged
Webbrowser1.DocumentText = Textbox1.Text
End Sub



Hope I helped, Any questions: admin@limestory75.com
Was This Post Helpful? 0

#5 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Edit HTML In WebBrowser

Posted 11 September 2011 - 10:50 PM

Then just make sure that you type:
WebBrowser1.document.title = TextBox1.text
WebBrowser1.Refresh

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1