Implementing the Tool

The listing that follows illustrates the basic behavior of the tool. However, what's important—the core of the tool—is still to come.

Option Explicit

Private Sub cmdGo_Click()
  WebBrowser1.Navigate2 Text1.Text
End Sub

Private Sub cmdOpen_Click()
On Error GoTo ErrExit
  CommonDialog1.ShowOpen
  Text1.Text = CommonDialog1.filename
  cmdGo_Click
ErrExit:
End Sub

Private Sub cmdWalk_Click()
  List1.Clear
  DoWalkElements WebBrowser1.Document, List1
End Sub

Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
  cmdWalk.Enabled = True
End Sub

Private Sub DoWalkElements(ByVal doc As HTMLDocument, ByVal list As ListBox)
  EnumerateImages doc, list
  EnumerateObjects doc, list
  EnumerateLinks doc, list
End Sub

Everything that the DHTML X-Ray utility does is concentrated in the DoWalkElements subroutine. As shown above, it ends up calling three enumeration functions, like EnumerateImages, EnumerateObjects, and EnumerateLinks. The task of examining the HTML elements can be carried out only once the document has finished loading. This state of things is notified through the WebBrowser's DocumentComplete event. After receiving this message, we enable the Walk button.

cmdWalk.Enabled = True

As you can imagine, all the enumerative functions have the same structure and the same prototype. They all take two arguments. The first is a reference to the document object to consider, while the second is a listbox object to output to.

Having an HTMLDocument argument allows you to transparently handle two or more WebBrowser controls. In practice, we use the main (and visible) WebBrowser to perform a first scanning of the document. For each HTML file we find, we need a recursive call. That's why we need to have a second (and possibly invisible) WebBrowser control to load the inner HTML documents and return the document object for further processing. Using an argument like this saves us from duplicating the code of the DoWalkElements's subroutines.

© 1997 by Wrox Press. All rights reserved.