|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionI'm new to programming, and was trying to create a browser (using the web browser control) that connected through a TCP client. I soon realized that I would need to trap the links on the page, so I could redirect them through my application. A little bit of time and coffee later, I realized that no one appears to have ever needed this. Or at the very least, I was unable to find any information on it. However, I eventually gave up and decided to give it a shot myself. I am trying to get better with my programming, and would really like to try and make a career out of it, so please comment on this and let me know what you think or where I can improve. Description of the solutionThe web browser control shipping with the .NET Framework provides many useful functions and properties. As well as this, many of the events are available without too much fiddling around. First off, I monitored the Document CompletedSetting up the event handler is as simple as this: Private Sub webControl_DocumentCompleted(ByVal sender As Object, _
ByVal e As WebBrowserDocumentCompletedEventArgs) _
Handles webControl.DocumentCompleted
Dim link As HtmlElement
Dim links As HtmlElementCollection = webControl.Document.Links
For Each link In links
If btnChange.Checked Then
link.SetAttribute("href", newLink)
End If
link.AttachEventHandler("onclick", AddressOf LinkClicked)
Next
End Sub
This code gets all the links on the page, and then assigns the Link ClickedHere's the Private Sub LinkClicked(ByVal sender As Object, ByVal e As EventArgs)
Dim link As HtmlElement = webControl.Document.ActiveElement
Dim url As String = link.GetAttribute("href")
MsgBox("Link Clicked: " & link.InnerText & _
vbCrLf & "Destination: " & url)
End Sub
Now we know that a link was clicked. So next, I simply grab the document's currently active item (the link) and read in the ' Additional FeaturesCancel NavigationThe Private Sub webControl_Navigating(ByVal sender As Object, _
ByVal e As WebBrowserNavigatingEventArgs) _
Handles webControl.Navigating
If btnCancel.Checked Then
e.Cancel = True
End If
End Sub
ConclusionThe best thing about this solution is it involves only managed code. You get a managed event for any JavaScript event that occurs. There are many other properties and ideas that can be exploited here, but I think that's pretty much self explanatory. I hope this code is useful to someone; if not, at least, I finally got to post something on here.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||