Welcome Guest Search | Active Topics | Sign In | Register

WebBrowser: Get list of URLs Options
ac2015
Posted: Thursday, December 24, 2015 7:04:31 AM
Rank: Member
Groups: Member

Joined: 12/24/2015
Posts: 14
Hello!

I have always used Awesomium, and I am now trying to convert my code to the EOBrowser.

I would like to get a list of all hyperlinks found in a page.

My previous code was this:

Quote:
Private Function pGetURLs() As List(Of clsURL)

Debug.Print("{pGetURLs")

Dim nList As New List(Of clsURL)

Dim html As String = _
"var linksArray = new Array(); " & _
"for (var i = 0; i < document.links.length; i++) {" & _
"linksArray[i] = [String(document.links[i].innerHTML), String(document.links[i].innerText), String(document.links[i].href)];" & _
"} " & _
"return linksArray;"

Try
Dim linksArray As JSValue() = _wv.ExecuteJavascriptWithResult(String.Format("(function(){{ {0} }})()", html))

For Each link As JSValue() In linksArray

Dim sInnerHTML As String = link(0).ToString().Trim()
Dim sInnerText As String = link(1).ToString().Trim()
Dim sHRef As String = link(2).ToString().Trim()

If sHRef <> "undefined" Then

Dim nItem As New clsURL
nItem.HRef = sHRef
nItem.InnerHTML = sInnerHTML
nItem.InnerText = sInnerText

nList.Add(nItem)
End If

Next
Catch ex As Exception

End Try

Debug.Print("pGetURLs " & nList.Count & "}")

Return nList

End Function


Can somebody tell me how to do this with the EOBrowser?

I think there is not ExecuteJavascriptWithResult.

Thank you for the help.
eo_support
Posted: Thursday, December 24, 2015 3:11:53 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,229
Hi,

EvalScript is the equivalent of ExecuteJavaScriptWithResult. The function returns an object, so you will need to cast it to an JSObject first, then call JSObject.ToArray to a JSArray object. The code will be something like this:

Code: C#
//EvalScript returns either simple value (such as int, string) or a JSObject
JSObject o = (JSObject)webView.EvalScript(your_script);

//In case you return a JavaScript array, it will be represented as a JSArray
JSArray urls = o.ToArray();

//Each item in the JSArray is either an JSObject, or simple values. In your
//case since your JavaScript array contains strings, it will be a simple string
//value
foreach (string url in urls)
{
  .....
}

You can find more information here:

http://www.essentialobjects.com/doc/webbrowser/advanced/js.aspx

Hope this helps. Please feel free to let us know if you have any more questions.

Thanks!
ac2015
Posted: Thursday, December 24, 2015 4:46:03 PM
Rank: Member
Groups: Member

Joined: 12/24/2015
Posts: 14
Can you please post a complete, working, tested example for all users?

I have seen that somebody else already asked this question some time ago, and some buggy code (it wouldn't work for the one who posted the question, and it didn't work for me) was posted.

I think many people need to extract the URLs because they do a web spider.

Your approach is not what we need. We need to extract the href, the innerhtml and outerhtml.

Thank you.
eo_support
Posted: Thursday, December 24, 2015 6:13:42 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,229
ac2015 wrote:
Can you please post a complete, working, tested example for all users?


You are asking for something that does not exist. There are millions of real life scenarios and everyone's requirements is different, so it is not possible for us to post "working code" for every one of these exact scenarios. You need to look into the sample code we provide and understand it, then adapt it to fits your need. If you have any questions about any specific code or documentation we provided, please feel free to ask and we will be very happy to assist you further. But if you just tell us this is what you need and ask us to give you the exact working code, then you are basically asking us to do your job. We do not do that.

ac2015
Posted: Friday, December 25, 2015 2:34:11 AM
Rank: Member
Groups: Member

Joined: 12/24/2015
Posts: 14
It would be excellent promotion however.

I think most people come here because something in CefSharp, Awesomium or WebBrowser does not work, and if they get it working in EO.Browser, they will buy your product.

And the fact that somebody else already requested such a sample shows you that it is not a single-user scenario.

eo_support
Posted: Friday, December 25, 2015 10:01:03 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,229
There are various ways to return multiple values per link. You can simply use three slots in the array for each link. For example, if you have 10 links, your result array can be 30 elements. Element 0, 1, 2 contains href, innerHTML, outerHTML for the first link, element 3, 4, 5 contains href, innerHTML, outerHTML for the second link, and so on. You can also return a JavaScript objects in each array element. In that case the object will be a JSObject on the C# side. You would then use JSObject's indexer to get the value of each property on that object.

We will say this very clear and very publicly in our forum: You need to understand our sample code and adapt it. Period. If you have any question understanding the sample code, please feel free to ask and we will help you further. We have been in this business for a long time and we know very well that if you don't want to adapt our sample code and instead declaring it buggy outright because it doesn't do exactly what you need, you will just do it again and again whenever you need something else to be done. We could gave you working code for 10 times and if on the 11th times we tell you you need to do that yourself, you would be 11 times more angry than if we had told you up front that we don't do that for you. In such cases we would rather have you go with someone else. So our position is very clear on this. We do not do that. We give you sample code to show you the idea. You understand it and come up the final working code for your scenario. And if you have any question in the process, feel free to ask. But we don't do your job. If you can not accept that, you should go with other vendors.
ac2015
Posted: Tuesday, December 29, 2015 3:05:19 PM
Rank: Member
Groups: Member

Joined: 12/24/2015
Posts: 14
The only way to make it work was the following code:

Dim nList As New List(Of clsURL)

Dim html As String =
"var linksArray = new Array(); " &
"for (var i = 0; i < document.links.length; i++) {" &
"linksArray[i] = [String(document.links[i].innerHTML), String(document.links[i].innerText), String(document.links[i].href)];" &
"} " &
"return linksArray;"

Try
Dim linksArray As JSArray = _Browser.WebView.EvalScript(String.Format("(function(){{ {0} }})()", html))

For Each obj As Object In linksArray

Dim sInnerHTML As String = obj(0).ToString().Trim()
Dim sInnerText As String = obj(1).ToString().Trim()
Dim sHRef As String = obj(2).ToString().Trim()

Dim nItem As New clsURL
nItem.HRef = sHRef
nItem.InnerHTML = sInnerHTML
nItem.InnerText = sInnerText

nList.Add(nItem)

Next
Catch ex As Exception
MessageBox.Show(ex.Message.ToString)
End Try
eo_support
Posted: Tuesday, December 29, 2015 5:22:59 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,229
Great. I am glad that you see how it works. Please feel free to let us know if you have any other questions.

Thanks!


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.