Pages

Getting the Dynamically (JavaScript) Generated HTML from a WebBrowser Control in .NET

Challenge

Get the dynamically generated random value from the WebBrowser Control in your .NET or Java Application.


Rules
  • The one with shortest code will be the winner.
  • You can do it in .NET or Java
  • Use this JavaScript 
  • <SCRIPT>var x=Math.random();</SCRIPT>
  • I will give you 3 methods. you have to find a new method which is shorter than what i had given.
  • Don't tweak around the variables of the methods which i had given. You have to find a different solution.
  • Send your solutions to  a...@gmail.com

Three Methods

Download the Source: 
Here

METHOD1
=======

Public Class Form1
    Public Property Code() As String
        Get
            If (RichTextBox1.Text IsNot Nothing) Then
                Code = RichTextBox1.Text
            Else
                Code = ""
            End If
        End Get
        Set(ByVal value As String)
            RichTextBox1.Text = value
        End Set
    End Property
    Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
        webBrowser1.DocumentText = "<P ID='x'>.</P><SCRIPT>document.getElementById('x').innerHTML=Math.random();</SCRIPT>"
        Dim elem As HtmlElement
        If (webBrowser1.Document IsNot Nothing) Then
            Dim elems As HtmlElementCollection = webBrowser1.Document.GetElementsByTagName("P")
            If (elems.Count = 1) Then
                elem = elems(0)
                Code = elem.InnerText
            End If
        End If
    End Sub
End Class
Method 2 ========
Public Class Form2
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        WebBrowser1.Refresh()
        RichTextBox1.Text = WebBrowser1.Document.Body.InnerText
    End Sub
    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        WebBrowser1.DocumentText = "<P ID='x'>.</P><SCRIPT>document.getElementById('x').innerHTML=Math.random();</SCRIPT>"
    End Sub
End Class
Method 3 (Hand Craft) ======================
Public Class Form3
    Private Sub WebBrowser1_Navigated(sender As Object, e As WebBrowserNavigatedEventArgs) Handles WebBrowser1.Navigated
        RichTextBox1.Text = e.Url.AbsoluteUri.Replace("about:", "")
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        WebBrowser1.DocumentText = "<p id='x'>.</p><SCRIPT>window.location=Math.random();</SCRIPT>"
    End Sub
End Class

No comments:

Post a Comment