Android Question Links are found backwards??

Mark Read

Well-Known Member
Licensed User
Longtime User
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:

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
 

warwound

Expert
Licensed User
Longtime User
The javascript iterates from I=number of img elements to zero, look at the javascript while loop.

Martin.
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
Thank you for the explanation Martin. I thought the clue might be in the javascript but I am not good in javascript and could not understand what you wrote.

Mark
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
This is a modified version of that javascript that iterates through the document img tags from start to end:

B4X:
var anchor, img, parent, imgs = document.getElementsByTagName('img'), string = [];
for(var i=0, length=imgs.length; i<length; 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));

The same javascript formatted to a single line for use in b4a:

B4X:
var anchor,img,parent,imgs=document.getElementsByTagName('img'),string=[];for(var i=0, length=imgs.length; i<length; 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));

FYI you can use jsbeautifier.org to unformat such code and make it more readable.

Martin.
 
Upvote 0
Top