Android Question Detect which element is pressed on webview?

JohnC

Expert
Licensed User
Longtime User
You don't need webviewextras....if the element is a clickable hyperlink, the below event will be triggered when it is clicked on:

Sub wv_OverrideUrl (URL As String) As Boolean

(just make sure you initialized the webview event with the event name "wv" or change it and the event sub name to your choice)
 
Upvote 0

Rams007

Member
You don't need webviewextras....if the element is a clickable hyperlink, the below event will be triggered when it is clicked on:

Sub wv_OverrideUrl (URL As String) As Boolean

(just make sure you initialized the webview event with the event name "wv" or change it and the event sub name to your choice)

It does happen to be a hyperlink but what if it isn't?
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
if the webpage is yours, then yes. set javascript enable (general webview setting), then use webviewextras to bind javascript to the webview.

in your webpage, presumably you are already doing something when a particular element is tapped, yes? as part of that listener function, just add a call back to b4a. that triggers an event in b4a. you consume that event and have happen whatever you wanted to happen in the app. i don't see a problem. for every element you want so followed, just add the b4a call in the listener function. you can easily id them so the app knows which element was touched. i use it all the time. very simple to set up a little proof of concept. then you can roll out to your webpage.

if you don't own the webpage, it is conceptually possible (again through inserting javascript) as an exercise, but there would be no way to do it in real time. (you could insert a javascript snippet every 1 second, but only if you study the page's source and can figure out if something in the page changes when the element is tapped. if some variable is changed as a result, you could bang away at it every second, and when you saw it had changed, you'd know it was tapped. kind of pathetic unless somebody is paying you to do it. better if the webpage is yours :))
 
Upvote 0

Rams007

Member
Thanks for all that.

It's not my website, I'm just trying to add a few features and add it to an app.

My next problem is that the button links I want to capture are written in the form /threads/community/dothis=21313 missing out the https://www, they do link to a full URL underneath but in the TAG they are written in shortened form.

This means that they are not triggered in

B4X:
Sub wv_OverrideUrl (URL As String) As Boolean

Only full URLs are triggered without this sub https://www.xxx.com etc

is there anyway around this?
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
even though the webpage uses a relative link, the actual call is fully formed and is intercepted by shouldoverrideurlloading().
your sub wv_OverrideUrl() should fire (if you've set it up correctly). i've tested with a relative link on a webpage, and wv_OverrideUrl() fires and shows me the fully qualified url (with the missing "http://www.xxx" etc) i don't see what your problem is.
 
Upvote 0

jkhazraji

Active Member
Licensed User
Longtime User
Use CallSub from inside the javascript blocks of the HTML code to catch the name of any element e.g., a button by defining its id
 
Upvote 0

Biswajit

Active Member
Licensed User
Longtime User
B4X:
'**Declare**'
Sub Globals
    Dim wve As WebViewExtras
    Dim jsi As DefaultJavascriptInterface
End Sub

'**Initialize**'
wve.Initialize(webview)
jsi.Initialize
wve.AddJavascriptInterface(jsi,"B4A")

'**Add click event listener**'
Sub webview_PageFinished (Url As String)
    wve.ExecuteJavascript("document.body.addEventListener('click', event => {B4A.CallSub('DOMClicked',true, event.target)})")
End Sub

'**On clicking on any element in the webview you will get the element code here**''
Sub DOMClicked(element As String)
    log(element)
End Sub
 
Last edited:
Upvote 0

Rams007

Member
B4X:
'**Declare**'
Sub Globals
    Dim wve As WebViewExtras
    Dim jsi As DefaultJavascriptInterface
End Sub

'**Initialize**'
wve.Initialize(webview)
jsi.Initialize
wve.AddJavascriptInterface(jsi,"B4A")

'**Add click event listener**'
Sub webview_PageFinished (Url As String)
    wve.ExecuteJavascript("document.body.addEventListener('click', event => {B4A.CallSub('DOMClicked',true, event.target)})")
End Sub

'**On clicking on any element in the webview you will get the element code here**''
Sub DOMClicked(element As String)
    log(element)
End Sub

Thanks great thanks!
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
before moving on to the next problem - or before continuing to beat this dead horse -
it needs to be understood that the javascript snippet has several potential issues:

1) it does not identify the element which has been clicked on. it is only possible to identify
the element (or elements) if they have an id. so even if the elements have an id, the snippet
needs to ask for it (use event.target.id instead of event.target).

2) if you ask for the id of an element that has no id, you get an emptry string, which tells you
even less than what event.target tells you. (of course, you can use a ternary operator to tell
you which to use: the target or the target.id)

3) a click anywhere on the page is a click. it triggers your sub in b4a. (conceivably you could
word your snippet to do nothing if the target.type was not a button, let's say. but elements that
you are interested in monitoring may not be buttons.)
 
Last edited:
Upvote 0

Rams007

Member
above only tells you what kind of object was clicked, but it doesn't tell you which one. even if you add event.target.id (which would be expected), you get nothing unless the target actually has an id. furthermore, there is the issue of target vs. currenttarget; it's difficult to know where the object is located within the event chain (are we bubbling up or capturing?) nice effort, though.

Well i'm adding the element so I can set it any ID I want.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
so the webpage is yours after saying it wasn't. enough.
 
Upvote 0
Top