Hello,
I am using a modified version of Warwound's code from this post (Many thanks!):
http://www.b4x.com/android/forum/threads/get-links-from-webpage.13317/
What I have is this:
In the sub ProcessLinks I have to reverse the loop with "i", otherwise the links are stored in reverse order, the last one first!
I have 300+ Images in this webpage and spent ages wondering why the first ones were not being shown.
Am I missing something in the javascript?
Thanks
Mark
I am using a modified version of Warwound's code from this post (Many thanks!):
http://www.b4x.com/android/forum/threads/get-links-from-webpage.13317/
What I have is this:
B4X:
Sub btnOK_click
Activity.LoadLayout("Layout1") 'just a webview
Initialise 'sub to set constants and screen
ToastMessageShow ("Loading URL .... Please wait.", True)
WebViewExtras1.addJavascriptInterface(WebView1, "B4A")
WebViewExtras1.addWebChromeClient(WebView1,"")
WebView1.LoadUrl(MainUrl)
End Sub
Sub WebView1_PageFinished (Url As String)
Log("webpage loaded")
Dim Javascript As String
Javascript="var anchor,img,parent,imgs=document.getElementsByTagName('img'),i=imgs.length,string=[];while(i--){img=imgs[i];parent=img.parentNode;if(parent.tagName.toLowerCase()==='a'){string.push({imageSource:img.src,href:parent.href})}}B4A.CallSub('ProcessLinks',true,JSON.stringify(string));"
WebViewExtras1.executeJavascript(WebView1, Javascript)
End Sub
Sub ProcessLinks(Json As String)
' Json represents an array of javascript objects
' Log(Json)
Dim JavascriptArray As List
Dim JavascriptObject As Map
Dim i As Int
Dim Parser As JSONParser
Dim ImageString1, ImageString2 As String
ToastMessageShow ("processing links",False)
Parser.Initialize(Json)
JavascriptArray=Parser.NextArray
For i=JavascriptArray.Size-1 To 0 Step -1
JavascriptObject=JavascriptArray.Get(i)
Log(JavascriptObject.Get("imageSource")&" | "&JavascriptObject.Get("href"))
ImageString1=JavascriptObject.Get("imageSource")
ImageString2=JavascriptObject.Get("href")
If ImageString1.Contains(".jpg") Then
thumbs.Add(ImageString1)
images.Add(ImageString2)
ImageTotal = ImageTotal + 1
End If
Next
ToastMessageShow("Images found: "&thumbs.Size, True)
.... etc
End Sub
In the sub ProcessLinks I have to reverse the loop with "i", otherwise the links are stored in reverse order, the last one first!
I have 300+ Images in this webpage and spent ages wondering why the first ones were not being shown.
Am I missing something in the javascript?
Thanks
Mark