function getLinks(){
var anchors=document.getElementsByTagName('a');
for(var i=0, j=anchors.length; i<j; i++){
console.log(anchors[i].innerHTML+' | '+anchors[i].href);
}
}
var anchors=document.getElementsByTagName('a'), string=[];
for(var i=0, j=anchors.length; i<j; i++){
string.push({text:anchors[i].innerHTML, href:anchors[i].href});
}
B4A.CallSub('ProcessLinks', true, JSON.stringify(string));
Sub Process_Globals
Dim MyWebViewExtras As WebViewExtras
End Sub
Sub ProcessLinks(Links As String)
Log(Links)
' parse the JSON encoded String back to text and href values
End Sub
Sub WebView1_PageFinished (Url As String)
Dim Javascript As String
Javascript="var anchors=document.getElementsByTagName('a'), string=[];for(var i=0, j=anchors.length; i<j; i++){string.push({text:anchors[i].innerHTML, href:anchors[i].href});}B4A.CallSub('ProcessLinks', true, JSON.stringify(string));"
MyWebViewExtras.executeJavascript(WebView1, Javascript)
End Sub
That javascript will create an array of objects.
Each object has two properties: 'text' and 'href'.
The object will be JSON encoded to a String and sent to a B4A Sub named 'ProcessLinks'.
Martin.
Sub ProcessLinks(JSONLinks As String)
Log(JSONLinks)
' parse the JSON encoded String back to text and href values
Dim JSON As JSONParser, Lst1 As List
JSON.Initialize(JSONLinks)
Lst1 = JSON.NextArray
Dim a As Map
For i = 0 To Lst1.Size - 1
JSON.Initialize(Lst1.Get(i))
a = JSON.NextObject ' Here i have error: org.json.JSONException: Unterminated object at character 11 of {href=http://www.7ya.ru/click/?rid=109554&bn=21191&u
For j=0 To a.Size - 1
'Log(a.GetValueAt(i) & "------" & a.GetKeyAt(i))
Next
Next
End Sub
{href=http://solnet.ee/contests/quiz.php, text=>>>}
{href=http://www.solnet.ee/ban_counter/down.php?f=1680, text=dettvorchestvo.ru}
{href=http://solnet.ee/gallery/kormushka.html, text=<img src="gallery/pic/kormushka/kormushka.gif" width="65" height="65" border="0" alt="Фотоконкурс" align="left"> <font color="#3366FF">Экологический фотоконкурс<br><b>"Покормите птиц зимой!"</b></font>}
......
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
Parser.Initialize(Json)
JavascriptArray=Parser.NextArray
For i=0 To JavascriptArray.Size-1
JavascriptObject=JavascriptArray.Get(i)
Log(JavascriptObject.Get("text")&", "&JavascriptObject.Get("href"))
Next
End Sub
Sub WebView1_PageFinished (Url As String)
Dim Javascript As String
Javascript="var anchors=document.getElementsByTagName('a'), string=[];for(var i=0, j=anchors.length; i<j; i++){string.push({text:anchors[i].innerHTML, href:anchors[i].href});}B4A.CallSub('ProcessLinks', true, JSON.stringify(string));"
WebViewExtras1.executeJavascript(WebView1, Javascript)
End Sub
Regular href-s i get via "getHitTestResult" - so, the aim is to get JavaScript's links of the images. Later i will parse "<img src=".
Do you mean you just want to get anchor elements that have an image instead of text to click on?
Martin.
Sub OnLongClick(viewtag As Object) As Boolean
DoEvents
Dim a As Reflector, b As String, d As Object, e As Int
d = Obj1.RunMethod("getHitTestResult")
a.Target = d
e = a.RunMethod("getType")
b = a.RunMethod("getExtra")
If e = 1 OR e = 7 Then 'link
Else 'others
b = JavaScriptLink(b)
End If
If b.Contains("://") Then .....
'making the popup menu...
<img src="http://img.files.7ja.ru/img4.0/1x1.gif" border="0" alt="Придумано и сделано в России!">, http://www.7ya.ru/click/?rid=109555&bn=21192&url=
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'){
// this IMG element is the child of an A (anchor) element
string.push({imageSource:img.src, href:parent.href});
}
}
B4A.CallSub('ProcessLinks', true, JSON.stringify(string));
'Activity module
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim WebView1 As WebView
Dim WebViewExtras1 As WebViewExtras
End Sub
Sub Activity_Create(FirstTime As Boolean)
WebView1.Initialize("WebView1")
WebViewExtras1.addJavascriptInterface(WebView1, "B4A")
WebViewExtras1.addWebChromeClient(WebView1)
Activity.AddView(WebView1, 0, 0, 100%x, 100%y)
WebView1.LoadUrl("http://code.martinpearman.co.uk/deleteme/image_anchors.htm")
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
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
Parser.Initialize(Json)
JavascriptArray=Parser.NextArray
For i=0 To JavascriptArray.Size-1
JavascriptObject=JavascriptArray.Get(i)
Log(JavascriptObject.Get("imageSource")&" | "&JavascriptObject.Get("href"))
Next
End Sub
Sub WebView1_PageFinished (Url As String)
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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Images as anchors</title>
</head>
<body>
<a href="http://www.b4x.com/forum/index.php">
<img src="http://upload.wikimedia.org/wikipedia/commons/d/d8/Basic4android-Logo.jpg" width="336" height="270" alt="" />
</a>
<br />
<a href="http://google.co.uk">
<img src="http://blogs.creativepool.co.uk/files/2011/01/google_logo1.jpg" width="256" height="256" alt="" />
</a>
</body>
</html>
If an image has been made into a clickable link by setting it's javascript onclick to a function then things get tricky - maybe impossible.
But if an image element is a child of an anchor element you can get the info you require.
Martin.
public void requestFocusNodeHref (Message hrefMsg)
Since: API Level 1
Request the anchor or image element URL at the last tapped point. If hrefMsg is null, this method returns immediately and does not dispatch hrefMsg to its target. If the tapped point hits an image, an anchor, or an image in an anchor, the message associates strings in named keys in its data. The value paired with the key may be an empty string.
public WebView.HitTestResult getHitTestResult ()
Since: API Level 1
Return a HitTestResult based on the current cursor node. If a HTML::a tag is found and the anchor has a non-JavaScript url, the HitTestResult type is set to SRC_ANCHOR_TYPE and the url is set in the "extra" field. If the anchor does not have a url or if it is a JavaScript url, the type will be UNKNOWN_TYPE and the url has to be retrieved through requestFocusNodeHref(Message) asynchronously. If a HTML::img tag is found, the HitTestResult type is set to IMAGE_TYPE and the url is set in the "extra" field. A type of SRC_IMAGE_ANCHOR_TYPE indicates an anchor with a url that has an image as a child node. If a phone number is found, the HitTestResult type is set to PHONE_TYPE and the phone number is set in the "extra" field of HitTestResult. If a map address is found, the HitTestResult type is set to GEO_TYPE and the address is set in the "extra" field of HitTestResult. If an email address is found, the HitTestResult type is set to EMAIL_TYPE and the email is set in the "extra" field of HitTestResult. Otherwise, HitTestResult type is set to UNKNOWN_TYPE.
Javascript has many powerful functions to do most tasks - i doubt you'll need to resort to manually parsing a String to find the info you require.
Martin.
<a href="parents/index.html" onmouseover="iName='image2';
Ichange('changed2')" onmouseout="Ichange('default2')">
<img src="http://solnet.ee/pic/o22.gif" width="85" height="50" name="image2" border="0" alt="Родителям"></a>
Return a HitTestResult based on the current cursor node. If a HTML::a tag is found and the anchor has a non-JavaScript url, the HitTestResult type is set to SRC_ANCHOR_TYPE and the url is set in the "extra" field. If the anchor does not have a url or if it is a JavaScript url, the type will be UNKNOWN_TYPE and the url has to be retrieved through requestFocusNodeHref(Message) asynchronously. If a HTML::img tag is found, the HitTestResult type is set to IMAGE_TYPE and the url is set in the "extra" field. A type of SRC_IMAGE_ANCHOR_TYPE indicates an anchor with a url that has an image as a child node. If a phone number is found, the HitTestResult type is set to PHONE_TYPE and the phone number is set in the "extra" field of HitTestResult. If a map address is found, the HitTestResult type is set to GEO_TYPE and the address is set in the "extra" field of HitTestResult. If an email address is found, the HitTestResult type is set to EMAIL_TYPE and the email is set in the "extra" field of HitTestResult. Otherwise, HitTestResult type is set to UNKNOWN_TYPE.
A type of SRC_IMAGE_ANCHOR_TYPE indicates an anchor with a url that has an image as a child node.
If a HTML::img tag is found, the HitTestResult type is set to IMAGE_TYPE and the url is set in the "extra" field.