Background
Following my post about Live Score [Cricket] on GIT, I got a request to publish the code for doing the same. And here I am, starting with my first article. I hope that you will find it useful in one or the other way.
Introduction
There is nothing much to be done, but it is found that not many of us know the way - how to do it. I am showing you how you can update "your" post automatically in CodeProject. There can be many other uses of it, but I recently used it for this task.
Using the Code
It consist of 2 steps:
- Get the content to be posted
- Post the content
This code is required to know whether the page is loaded.
Dim proceed As Boolean
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, _
ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) _
Handles WebBrowser1.DocumentCompleted
proceed = True
End Sub
And here is the function that can be used to browse a page, or to post some data to a page.
Public Sub FillForm(ByRef wbrowser As System.Windows.Forms.WebBrowser, _
ByVal url As String, Optional ByVal keys() As String = Nothing, _
Optional ByVal values() As String = Nothing, _
Optional ByVal submit As String = "")
Dim obj As HtmlElement
Dim objcol As System.Windows.Forms.HtmlElementCollection
proceed = False
wbrowser.Navigate(url)
While Not proceed
Application.DoEvents()
End While
proceed = False
If keys Is Nothing Or values Is Nothing Then Exit Sub
If keys.Length = values.Length Then
For i As Integer = 0 To keys.Length - 1
obj = wbrowser.Document.GetElementById(keys(i))
If obj IsNot Nothing Then obj.SetAttribute("value", values(i))
Next
If submit <> "" Then
If submit = "true" Then
objcol = wbrowser.Document.GetElementsByTagName("input")
For Each objx In objcol
If objx.GetAttribute("Type").ToLower = "submit" Then
obj = objx
Exit For
End If
Next
Else
obj = wbrowser.Document.GetElementById(submit)
End If
If obj IsNot Nothing Then
obj.InvokeMember("Click")
End If
End If
End If
proceed = False
End Sub
To browse a page, you can use this function like:
FillForm(WebBrowser1, "www.google.com")
My application uses Yahoo Cricket to get the latest score. So I get the page:
FillForm(WebBrowser1, _
http://cricket.yahoo.com/cricket-live-score-south-africa-vs-india_11992)
When the page is loaded, I have to look for the score. I checked the page source for that and found that tab1
and header-main
contains the main score.
So I use the following to get the score content:
Dim content As String = WebBrowser1.Document.GetElementById_
("header-main").InnerText.Trim & vbCrLf & vbCrLf & _
WebBrowser1.Document.GetElementById("tab1").InnerText
This is how I get the content to be posted.
Now we have to post it to CodeProject. For that, I get the edit link of my post, which is - http://www.codeproject.com/script/Forums/Edit.aspx?fid=1580229&select=3719714&floc=/Forums/1580229/General-Indian-Topics.aspx&action=m.
Now with the above function, it is a piece of cake to get it posted.
FillForm(WebBrowser1, My.Settings.EditURL, New String() _
{"ctl00_MC_Subject", "ctl00_MC_ContentText_MessageText"}, _
New String() {"Live Score [Cricket]", content}, "ctl00_MC_submit")
My.Settings.EditURL
contains the edit URL of my post. Again with page source, I found the IDs of the required controls:
ctl00_MC_Subject
ctl00_MC_ContentText_MessageText
ctl00_MC_submit
As you can see, keys
contains the control IDs which are to be edited and values
contain the values for those controls in the same order. submit
is the ID of the button to post the message. I have a special case for this parameter, if submit="true"
, then it finds the button on the page and clicks it. This can be useful when there is just one button on page having no ID.
It works as follows: It looks for controls in the keys
and assigns them values from values
. After assigning all the values, it looks for the submit button to post it.
Points of Interest
This can be used in many ways. This is just what and how you can do it. Just a wise word - use it for good, don't use it to spam.
I know that this doesn't deal with any message box, or script error coming from page. And there can be many other cases to be considered.
There are a wide scope of modifications, but so far, I am fine with this one function only. It worked whenever I required it. Still, your suggestions are welcome. Please keep me posted if you find a good use of it, or a modification.
History
- No changes have been made to the code at this stage (Jan 2011)
- [201101051220] Included code