Projects

Find all our projects in development below.
All source code is GNU General Public License (GPL)

javaSpy

Browsing frmDOMSpy.vb (29.85 KB)

Option Explicit On

Public Class frmDOMSpy

    Private xOffset As Integer = 0
    Private domMap As Hashtable = Nothing

    Private elementCursor As Integer = 0
    Private elementCount As Integer = 0

    Private cursorOn As Bitmap = Nothing
    Private cursorOff As Bitmap = Nothing

    Private browserCapture As Bitmap = Nothing
    Private reloadNode As TreeNode = Nothing

    Public Sub New(Optional ByVal domURL As String = Nothing)

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        If Not (domURL Is Nothing) Then txtAddress.Text = domURL

    End Sub

    Private Function _getResBitmap(ByVal name As String) As Bitmap

        ' return the requested bitmap from resources
        Try
            Return My.Resources.ResourceManager.GetObject(name)
        Catch
            Return Nothing
        End Try

    End Function

    Private Sub _showWaitCursor(ByVal bShow As Boolean)

        ' show/hide the hourglass
        If bShow Then
            Me.Cursor = Cursors.WaitCursor
            Me.UseWaitCursor = True
        Else
            Me.UseWaitCursor = False
            Me.Cursor = Cursors.Arrow
        End If

    End Sub

    Private Overloads Sub _buildDOM(ByVal domdoc As Object, _
        Optional ByVal parentNode As TreeNode = Nothing)

        For Each thisElement As Object In domdoc.All

            Dim thisNode As TreeNode = Nothing
            Dim nodeText As String = "<" + thisElement.tagName + ">"

            If parentNode Is Nothing Then
                thisNode = parentNode
            Else

                If Not (thisElement.parentNode Is Nothing) AndAlso _
                    domMap.Contains(thisElement.parentNode) Then
                    thisNode = domMap(thisElement.parentNode)
                Else
                    thisNode = parentNode
                End If

            End If

            Try

                nodeText = thisElement.outerHTML.Substring(0, thisElement.outerHTML.IndexOf(">") + 1)
                If nodeText.StartsWith(vbCrLf) Then nodeText = nodeText.Substring(2)

            Catch
            End Try

            If lblExpose.BorderStyle = Border3DStyle.SunkenOuter Then

                Try

                    If thisElement.innerText Is Nothing And Not nodeText.StartsWith("<!") Then
                        nodeText = nodeText.Replace(">", " />")
                    ElseIf Not (thisElement.innerText Is Nothing) Then
                        nodeText += thisElement.innerText + "</" + thisElement.tagName + ">"
                    End If

                Catch
                End Try

            End If

            thisNode = thisNode.Nodes.Add(nodeText)

            With thisNode
                .ContextMenuStrip = menuElement
                .Tag = thisElement
            End With

            domMap.Add(thisElement, thisNode)

            elementCursor += 1
            prgStatus.Value = Math.Round((elementCursor / elementCount) * 100)

            If thisElement.tagName = "FRAME" Or _
                thisElement.tagName = "IFRAME" Then

                elementCount += thisElement.contentWindow.document.all.length
                _buildDOM(thisElement.contentWindow.document, thisNode)

            End If

        Next ' thisElement

    End Sub

    Private Overloads Sub _buildDOM(ByVal elementNode As TreeNode)

        For Each thisElement As Object In elementNode.Tag.children

            Dim thisNode As TreeNode = Nothing
            Dim nodeText As String = "<" + thisElement.tagName + ">"

            Try
                nodeText = thisElement.outerHTML.Substring(0, thisElement.outerHTML.IndexOf(">") + 1)
                If nodeText.StartsWith(vbCrLf) Then nodeText = nodeText.Substring(2)
            Catch
            End Try

            If lblExpose.BorderStyle = Border3DStyle.SunkenOuter Then

                Try

                    If thisElement.innerText Is Nothing And Not nodeText.StartsWith("<!") Then
                        nodeText = nodeText.Replace(">", " />")
                    ElseIf Not (thisElement.innerText Is Nothing) Then
                        nodeText += thisElement.innerText + "</" + thisElement.tagName + ">"
                    End If

                Catch
                End Try

            End If

            thisNode = elementNode.Nodes.Add(nodeText)

            With thisNode
                .ContextMenuStrip = menuElement
                .Tag = thisElement
            End With

            If domMap.Contains(thisElement) Then _
                domMap.Remove(thisElement)

            domMap.Add(thisElement, thisNode)

            elementCursor += 1
            prgStatus.Value = Math.Round((elementCursor / elementCount) * 100)

            If thisElement.tagName = "FRAME" Or _
                thisElement.tagName = "IFRAME" Then

                Try

                    elementCount += thisElement.contentWindow.document.all.length
                    _buildDOM(thisElement.contentWindow.document, thisNode)

                Catch
                End Try

            End If

            Try

                If thisElement.children.length > 0 Then

                    elementCount += thisElement.children.length
                    _buildDOM(thisNode)

                End If

            Catch
            End Try

        Next ' thisElement

    End Sub

    Private Function _findNodeText(ByVal nodeText As String, _
        Optional ByVal nodeParent As TreeNode = Nothing) As TreeNode

        Dim nodeRet As TreeNode = Nothing
        Dim nodeList As TreeNodeCollection = Nothing

        If nodeParent Is Nothing Then
            nodeList = tvSpy.Nodes
        Else
            nodeList = nodeParent.Nodes
        End If

        For Each thisNode As TreeNode In nodeList

            If thisNode.Text.Contains(nodeText) Then

                nodeRet = thisNode
                Exit For

            End If

            If thisNode.GetNodeCount(False) > 0 Then

                nodeRet = _findNodeText(nodeText, thisNode)
                If Not (nodeRet Is Nothing) Then Exit For

            End If

        Next ' thisNode

        Return nodeRet

    End Function

    Private Sub frmMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        ' dispose of resources
        If Not (browserCapture Is Nothing) Then _
            browserCapture.Dispose()

        cursorOff.Dispose()
        cursorOn.Dispose()

    End Sub

    Private Sub frmDOMSpy_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim _wndLabel As New clsWindowObject(lblURL.Handle)
        Dim _wndTextBox As New clsWindowObject(txtAddress.Handle)
        Dim _wndButton As New clsWindowObject(btnURL.Handle)
        Dim _wndContainer As New clsWindowObject(tabDOMSpy.Handle)

        ' adjust the parent of the label and address textbox
        _wndLabel.Parent = _wndContainer
        _wndTextBox.Parent = _wndContainer
        _wndButton.Parent = _wndContainer

        xOffset = tabDOMSpy.ItemSize.Width * tabDOMSpy.TabPages.Count

        ' reposition the label
        lblURL.Left = xOffset + 10
        lblURL.Top = 0

        ' reposition the address textbox
        txtAddress.Left = lblURL.Left + lblURL.Width
        txtAddress.Top = 0

        ' reposition the url button
        btnURL.Left = txtAddress.Left + txtAddress.Width
        btnURL.Top = 0

        ' resize the interface
        frmDOMSpy_Resize(sender, e)

        ' insert blank node
        tvSpy.Nodes.Add("No URL entered")

        ' navigate to blank page
        browserMain.DocumentText = GetResourceString("web_blank")

        ' cache bitmap resources
        cursorOn = _getResBitmap("cursor_on")
        cursorOff = _getResBitmap("cursor_off")

        spyContainer.Panel2MinSize = spyContainer.Panel2.Width

    End Sub

    Private Sub frmDOMSpy_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown

        txtAddress.Focus()
        If txtAddress.Text <> "" Then txtAddress_KeyDown(sender, Nothing)

    End Sub

    Private Sub frmDOMSpy_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize

        txtAddress.Width = tabDOMSpy.Width - lblURL.Width - btnURL.Width - xOffset - 12
        btnURL.Left = txtAddress.Left + txtAddress.Width
        ssMain.ShowItemToolTips = Not (Me.WindowState = FormWindowState.Maximized)

    End Sub

    Private Sub lblURL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblURL.Click

        txtAddress.Focus()
        txtAddress.SelectAll()

    End Sub

    Private Sub txtAddress_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtAddress.KeyDown

        If e Is Nothing Or _
            (Not (e Is Nothing) AndAlso e.KeyCode = Keys.Enter) Then _
            btnURL_Click(sender, e)

    End Sub

    Private Sub btnURL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnURL.Click

        If txtAddress.Text.Trim = "" Then

            MessageBox.Show("Please type the URL.", Application.ProductName, _
                MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

            txtAddress.Focus()
            Exit Sub

        End If

        If lblCursor.BorderStyle = Border3DStyle.SunkenOuter Then _
            lblStatus.Text = "Ready"

        browserMain.Navigate(txtAddress.Text)

        tvSpy.Nodes.Clear()

        With tvSpy.Nodes.Add("URL: " + txtAddress.Text)
            .ContextMenuStrip = menuURL
            .Tag = txtAddress.Text
        End With

        lvAttribs.Items.Clear()
        lvStyles.Items.Clear()

        domMap = New Hashtable()

        If tabDOMSpy.SelectedTab.Name = "pageSpy" Then
            tvSpy.Focus()
        ElseIf tabDOMSpy.SelectedTab.Name = "pageView" Then
            browserMain.Focus()
        End If

    End Sub

    Private Sub browserMain_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles browserMain.DocumentCompleted

        If Not e.Url.ToString.Contains("about:") And _
             Not e.Url.ToString.Contains("javascript:") Then

            txtAddress.Text = e.Url.ToString

            If browserMain.ReadyState = WebBrowserReadyState.Complete Then

                lblStatus.Text = "Loading DOM..."

                Application.DoEvents()
                _showWaitCursor(True)

                timerComplete.Stop()
                timerComplete.Start()

                Dim docNode As TreeNode = tvSpy.Nodes(0)

                If Not (reloadNode Is Nothing) Then
                    docNode = reloadNode
                    reloadNode = Nothing
                ElseIf docNode.GetNodeCount(False) > 0 Then
                    docNode = tvSpy.Nodes.Add("URL: " + e.Url.ToString)
                End If

                With docNode
                    .ContextMenuStrip = menuURL
                    .Tag = txtAddress.Text
                End With

                elementCursor = 0
                elementCount = browserMain.Document.DomDocument.all.length

                prgStatus.Value = 0
                prgStatus.Visible = True

                _buildDOM(browserMain.Document.DomDocument, _
                    docNode)

                docNode.EnsureVisible()
                tvSpy.SelectedNode = docNode

            End If

        End If

    End Sub

    Private Sub browserMain_Navigating(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserNavigatingEventArgs) Handles browserMain.Navigating

        If Not e.Url.ToString.Contains("about:") And _
            Not e.Url.ToString.Contains("javascript:") Then _
            lblStatus.Text = "Loading web page..."

    End Sub

    Private Sub spyContainer_SplitterMoved(ByVal sender As Object, ByVal e As System.Windows.Forms.SplitterEventArgs) Handles spyContainer.SplitterMoved

        spyContainer.Panel2.Refresh()

    End Sub

    Private Sub tvSpy_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tvSpy.AfterSelect

        lvAttribs.Items.Clear()
        lvStyles.Items.Clear()

        If Not (tvSpy.SelectedNode Is Nothing) AndAlso _
            Not (tvSpy.SelectedNode.Tag Is Nothing) Then

            Try

                For Each thisAttrib As Object In tvSpy.SelectedNode.Tag.attributes

                    With lvAttribs.Items.Add(thisAttrib.Name)
                        .SubItems.Add(thisAttrib.Value)
                    End With

                Next ' thisAttrib

                If lvAttribs.Items.Count > 0 Then

                    Dim htmlStyles As New clsHTMLStyles(tvSpy.SelectedNode.Tag)

                    For Each thisStyle As String In htmlStyles.Styles

                        With lvStyles.Items.Add(thisStyle)
                            .SubItems.Add(htmlStyles.Style(thisStyle))
                        End With

                    Next ' thisStyle

                End If

            Catch
            End Try

        End If

    End Sub

    Private Sub tvSpy_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tvSpy.DoubleClick

        If Not (tvSpy.SelectedNode Is Nothing) Then

            If tvSpy.SelectedNode.Text = "No URL entered" Then _
                txtAddress.Focus()

        End If

    End Sub

    Private Sub tvSpy_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles tvSpy.KeyUp

        If e.KeyCode = Keys.F3 Then lblFind_Click(sender, Nothing)

    End Sub

    Private Sub tvSpy_NodeMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles tvSpy.NodeMouseClick

        If (e.Button = Windows.Forms.MouseButtons.Left And _
            SystemInformation.MouseButtonsSwapped) Or _
            (e.Button = Windows.Forms.MouseButtons.Right And _
            Not SystemInformation.MouseButtonsSwapped) Then

            tvSpy.SelectedNode = e.Node

        End If

    End Sub

    Private Sub lvAttribs_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lvAttribs.DoubleClick

        If lvAttribs.SelectedItems.Count > 0 Then

            Dim editDlg As New clsEditDialog(Me)

            editDlg.InputText = lvAttribs.SelectedItems(0).SubItems(1).Text
            editDlg.Show("Edit attribute '" + lvAttribs.SelectedItems(0).Text + "'")

            If Not editDlg.Aborted Then

                If Not (tvSpy.SelectedNode Is Nothing) AndAlso _
                    Not (tvSpy.SelectedNode.Tag Is Nothing) Then

                    Try

                        tvSpy.SelectedNode.Tag.attributes(lvAttribs.SelectedItems(0).Text).Value = editDlg.OutputText
                        lvAttribs.SelectedItems(0).SubItems(1).Text = editDlg.OutputText

                    Catch ex As Exception

                        MessageBox.Show("Error: " + ex.Message, _
                            Application.ProductName, MessageBoxButtons.OK, _
                            MessageBoxIcon.Error)

                    End Try

                End If

            End If

        End If

    End Sub

    Private Sub lvAttribs_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles lvAttribs.KeyUp

        If e.KeyCode = Keys.Delete And lvAttribs.SelectedItems.Count > 0 Then

            Try

                tvSpy.SelectedNode.Tag.attributes(lvAttribs.SelectedItems(0).Text).Value = ""
                lvAttribs.SelectedItems(0).SubItems(1).Text = ""

            Catch ex As Exception

                MessageBox.Show("Error: " + ex.Message, _
                    Application.ProductName, MessageBoxButtons.OK, _
                    MessageBoxIcon.Error)

            End Try

        End If

    End Sub

    Private Sub lvAttribs_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles lvAttribs.Resize

        If lvAttribs.Columns.Count > 0 Then

            Dim colWidth As Integer = lvAttribs.Width / lvAttribs.Columns.Count - 10

            lvAttribs.Columns(0).Width = colWidth
            lvAttribs.Columns(1).Width = colWidth

        End If

    End Sub

    Private Sub lvStyles_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lvStyles.DoubleClick

        If lvStyles.SelectedItems.Count > 0 Then

            Dim editDlg As New clsEditDialog(Me)

            editDlg.InputText = lvStyles.SelectedItems(0).SubItems(1).Text
            editDlg.Show("Edit style '" + lvStyles.SelectedItems(0).Text + "'")

            If Not editDlg.Aborted Then

                If Not (tvSpy.SelectedNode Is Nothing) AndAlso _
                    Not (tvSpy.SelectedNode.Tag Is Nothing) Then

                    Try

                        Dim htmlStyles As New clsHTMLStyles(tvSpy.SelectedNode.Tag)

                        htmlStyles.Style(lvStyles.SelectedItems(0).Text) = editDlg.OutputText
                        lvStyles.SelectedItems(0).SubItems(1).Text = editDlg.OutputText

                    Catch ex As Exception

                        MessageBox.Show("Error: " + ex.Message, _
                            Application.ProductName, MessageBoxButtons.OK, _
                            MessageBoxIcon.Error)

                    End Try

                End If

            End If

        End If

    End Sub

    Private Sub lvStyles_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles lvStyles.KeyUp

        If e.KeyCode = Keys.Delete And lvStyles.SelectedItems.Count > 0 Then

            Try

                Dim htmlStyles As New clsHTMLStyles(tvSpy.SelectedNode.Tag)

                htmlStyles.Style(lvStyles.SelectedItems(0).Text) = ""
                lvStyles.SelectedItems(0).SubItems(1).Text = ""

            Catch ex As Exception

                MessageBox.Show("Error: " + ex.Message, _
                    Application.ProductName, MessageBoxButtons.OK, _
                    MessageBoxIcon.Error)

            End Try

        End If

    End Sub

    Private Sub lvStyles_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles lvStyles.Resize

        If lvStyles.Columns.Count > 0 Then

            Dim colWidth As Integer = lvStyles.Width / lvStyles.Columns.Count - 10

            lvStyles.Columns(0).Width = colWidth
            lvStyles.Columns(1).Width = colWidth

        End If

    End Sub

    Private Sub lblCursor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblCursor.Click

        If browserCapture Is Nothing Then

            lblCursor.Image = cursorOn
            lblCursor.BorderStyle = Border3DStyle.SunkenOuter

            ' get browser window position relative to the screen
            Dim browserPoint As Point = pageView.PointToScreen(browserMain.Location)

            ' create the capture bitmap
            Dim g1 As Graphics = Me.CreateGraphics()
            browserCapture = New Bitmap(picMask.Width, picMask.Height, g1)

            ' copy the screen to bitmap
            Dim g2 As Graphics = Graphics.FromImage(browserCapture)
            g2.CopyFromScreen(browserPoint.X, browserPoint.Y, 0, 0, _
                New Size(picMask.Width, picMask.Height))

            ' hide the webbrowser
            browserMain.Visible = False

            ' show the mask
            picMask.Visible = True

            Application.DoEvents()

            ' draw the capture bitmap
            Dim g As Graphics = picMask.CreateGraphics()
            g.DrawImage(browserCapture, New Point(0, 0))

            g.Dispose()
            g2.Dispose()
            g1.Dispose()

        Else

            browserCapture.Dispose()
            browserCapture = Nothing

            lblCursor.Image = cursorOff
            lblCursor.BorderStyle = Border3DStyle.RaisedOuter

            ' hide the mask
            picMask.Visible = False

            ' show the webbrowser
            browserMain.Visible = True

            lblStatus.Text = "Ready"

        End If

    End Sub

    Private Sub lblExpose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblExpose.Click

        If lblExpose.BorderStyle = Border3DStyle.RaisedOuter Then
            lblExpose.BorderStyle = Border3DStyle.SunkenOuter
        Else
            lblExpose.BorderStyle = Border3DStyle.RaisedOuter
        End If

        If domMap Is Nothing Then Exit Sub

        elementCursor = 0
        elementCount = domMap.Count

        prgStatus.Value = 0
        prgStatus.Visible = True

        _showWaitCursor(True)

        For Each thisEntry As DictionaryEntry In domMap

            Try

                Dim thisElement As Object = thisEntry.Key
                Dim thisNode As TreeNode = thisEntry.Value

                Dim nodeText As String = "<" + thisElement.tagName + ">"

                Try

                    nodeText = thisElement.outerHTML.Substring(0, thisElement.outerHTML.IndexOf(">") + 1)
                    If nodeText.StartsWith(vbCrLf) Then nodeText = nodeText.Substring(2)

                Catch
                End Try

                If lblExpose.BorderStyle = Border3DStyle.SunkenOuter Then

                    Try

                        If thisElement.innerText Is Nothing And Not nodeText.StartsWith("<!") Then
                            nodeText = nodeText.Replace(">", " />")
                        ElseIf Not (thisElement.innerText Is Nothing) Then
                            nodeText += thisElement.innerText + "</" + thisElement.tagName + ">"
                        End If

                    Catch
                    End Try

                End If

                thisNode.Text = nodeText

            Catch
            End Try

            elementCursor += 1
            prgStatus.Value = Math.Round((elementCursor / elementCount) * 100)

        Next ' thisElement

        _showWaitCursor(False)
        prgStatus.Visible = False

    End Sub

    Private Sub lblFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblFind.Click

        Static lastFindText As String = ""
        Dim findText As String = ""

        If e Is Nothing And lastFindText <> "" Then
            findText = lastFindText
        Else
            findText = InputBox("Enter the text to find:", Application.ProductName, lastFindText)
        End If

        If findText <> "" Then

            Dim startNode As TreeNode = tvSpy.SelectedNode
            If startNode Is Nothing Then startNode = tvSpy.Nodes(0)

            _showWaitCursor(True)
            Dim findNode As TreeNode = _findNodeText(findText, startNode)
            _showWaitCursor(False)

            If Not (findNode Is Nothing) Then

                findNode.EnsureVisible()
                tvSpy.SelectedNode = findNode

            Else

                MessageBox.Show("Cannot find """ + findText + """", Application.ProductName, _
                    MessageBoxButtons.OK, MessageBoxIcon.Information)

                findText = ""

            End If

        End If

        lastFindText = findText

    End Sub

    Private Sub picMask_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picMask.MouseClick

        Dim capturedElement As HtmlElement = browserMain.Document.GetElementFromPoint(e.Location)

        If Not (capturedElement Is Nothing) AndAlso _
            Not (domMap Is Nothing) AndAlso _
            domMap.Contains(capturedElement.DomElement) Then

            lblCursor.PerformClick()
            tabDOMSpy.SelectedTab = pageSpy

            Dim thisNode As TreeNode = domMap(capturedElement.DomElement)
            thisNode.EnsureVisible()
            tvSpy.SelectedNode = thisNode
            tvSpy.Focus()

        Else
            lblCursor.PerformClick()
        End If

    End Sub

    Private Sub picMask_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picMask.MouseMove

        If Not (browserCapture Is Nothing) Then

            ' draw the capture bitmap
            Dim g As Graphics = picMask.CreateGraphics()
            g.DrawImage(browserCapture, New Point(0, 0))

            ' draw the cross-hairs
            g.DrawLine(Pens.Black, 0, e.Y, picMask.Width, e.Y)
            g.DrawLine(Pens.Black, e.X, 0, e.X, picMask.Height)

            g.Dispose()

            lblStatus.Text = "(" + e.X.ToString + ", " + e.Y.ToString + ")"

        End If

    End Sub

    Private Sub picMask_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles picMask.Paint

        If Not (browserCapture Is Nothing) Then

            ' draw the capture bitmap
            e.Graphics.DrawImage(browserCapture, New Point(0, 0))

        End If

    End Sub

    Private Sub timerComplete_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timerComplete.Tick

        timerComplete.Stop()

        prgStatus.Visible = False

        _showWaitCursor(False)
        lblStatus.Text = "Loading complete"

        If Not (tvSpy.SelectedNode Is Nothing) AndAlso _
            TypeOf tvSpy.SelectedNode.Tag Is String Then _
            tvSpy.SelectedNode.Expand()

    End Sub

    Private Sub tabDOMSpy_Selected(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlEventArgs) Handles tabDOMSpy.Selected

        If e.Action = TabControlAction.Selected Then
            lblFind.Enabled = (e.TabPage.Name = pageSpy.Name)
            lblExpose.Enabled = (e.TabPage.Name = pageSpy.Name)
            lblCursor.Enabled = (e.TabPage.Name = pageView.Name)
        End If

        If lblCursor.BorderStyle = Border3DStyle.SunkenOuter Then _
            lblStatus.Text = "Ready"

    End Sub

    Private Sub mnuReload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuReload.Click

        If TypeOf tvSpy.SelectedNode.Tag Is String Then

            tvSpy.SelectedNode.Nodes.Clear()
            browserMain.Navigate(tvSpy.SelectedNode.Tag)

            reloadNode = tvSpy.SelectedNode

        End If

    End Sub

    Private Sub mnuRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuRemove.Click

        tvSpy.Nodes.Remove(tvSpy.SelectedNode)

        If tvSpy.GetNodeCount(False) = 0 Then

            domMap = Nothing

            ' insert blank node
            tvSpy.Nodes.Add("No URL entered")

            ' navigate to blank page
            browserMain.DocumentText = GetResourceString("web_blank")

            lblStatus.Text = "Ready"

        End If

    End Sub

    Private Sub mnuEditText_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuEditText.Click

        If tvSpy.SelectedNode.Tag Is Nothing Then Exit Sub

        Dim editDlg As New clsEditDialog(Me)

        editDlg.InputText = tvSpy.SelectedNode.Tag.innerText
        editDlg.Show("Edit InnerText")

        Try

            If Not editDlg.Aborted Then

                tvSpy.SelectedNode.Tag.innerText = editDlg.OutputText
                mnuReloadElement.PerformClick()

            End If

        Catch ex As Exception

            MessageBox.Show("Error: " + ex.Message, _
                Application.ProductName, MessageBoxButtons.OK, _
                MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub mnuEditHTML_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuEditHTML.Click

        If tvSpy.SelectedNode.Tag Is Nothing Then Exit Sub

        Dim editDlg As New clsEditDialog(Me)

        editDlg.InputText = tvSpy.SelectedNode.Tag.innerHTML
        editDlg.Show("Edit InnerHTML")

        Try

            If Not editDlg.Aborted Then

                tvSpy.SelectedNode.Tag.innerHTML = editDlg.OutputText
                mnuReloadElement.PerformClick()

            End If

        Catch ex As Exception

            MessageBox.Show("Error: " + ex.Message, _
                Application.ProductName, MessageBoxButtons.OK, _
                MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub mnuReloadElement_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuReloadElement.Click

        If tvSpy.SelectedNode.Tag Is Nothing Then Exit Sub

        Dim thisElement As Object = tvSpy.SelectedNode.Tag
        If TypeOf thisElement Is String Then Exit Sub

        Dim nodeText As String = "<" + thisElement.tagName + ">"

        Try

            nodeText = thisElement.outerHTML.Substring(0, thisElement.outerHTML.IndexOf(">") + 1)
            If nodeText.StartsWith(vbCrLf) Then nodeText = nodeText.Substring(2)

        Catch
        End Try

        If lblExpose.BorderStyle = Border3DStyle.SunkenOuter Then

            Try

                If thisElement.innerText Is Nothing Then
                    nodeText = nodeText.Replace(">", " />")
                ElseIf Not (thisElement.innerText Is Nothing) Then
                    nodeText += thisElement.innerText + "</" + thisElement.tagName + ">"
                End If

            Catch
            End Try

        End If

        tvSpy.SelectedNode.Text = nodeText
        tvSpy.SelectedNode.Nodes.Clear()

        elementCursor = 0
        elementCount = thisElement.children.length

        prgStatus.Value = 0
        prgStatus.Visible = True
        _showWaitCursor(True)

        _buildDOM(tvSpy.SelectedNode)

        _showWaitCursor(False)
        prgStatus.Visible = False

    End Sub

End Class

Download frmDOMSpy.vb

Back to file list


Back to project page