B4J Question Call B4J sub from webview javascript

mediait

Member
Licensed User
Longtime User
I've been searching for a couple of hours to no avail.

I have done this before with the B4A webview.

Is there any code or library examples that shows how a sub can be called from javascript?
 

mediait

Member
Licensed User
Longtime User
Actually I dont need to call a sub I just needed to get info from the javascript.

I have found code to capture javascript alerts, which works. The returned info seems to be in args(0) which returns the following:

WebEvent [source = javafx.scene.web.WebEngine@10336ad5, eventType = WEB_ALERT, data = my_alert_message]

How do I parse this to get the data field?

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private wv As WebView
    Private we, wvjo As JavaObject
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
   
    wv.Initialize("wv")
   
    we.InitializeNewInstance("javafx.scene.web.WebEngine",Null)
    wvjo = wv
    we = wvjo.RunMethod("getEngine",Null) 
    Dim e As Object = we.CreateEvent("javafx.event.EventHandler", "wv", False)
    we.RunMethod("setOnAlert", Array(e))
   
    MainForm.rootpane.AddNode(wv, 0, 0, -1,-1)
   
    MainForm.Show
    wv.LoadUrl("some web page with a javascript alert")

End Sub

Sub wv_Event(MethodName As String, Args() As Object)
    Log(Args(0))
   
    'RETURNS:
    'WebEvent [source = javafx.scene.web.WebEngine@10336ad5, eventType = WEB_ALERT, data = my_alert_message]
End Sub
 
Upvote 0

mediait

Member
Licensed User
Longtime User
I'd be happy with the alert capture if I could only extract the message.

I've tried creating a javaobject from args(0) and using getfield but that doesn't work.

I'd appreciate any help with just getting the alert message.
 
Upvote 0

mediait

Member
Licensed User
Longtime User
Thanks for your help Erel but I dont understand where you're going. Args(0) is the only defined item i.e. there are no other args elements, just args(0).

Is the args(0) value:
WebEvent [source = javafx.scene.web.WebEngine@10336ad5, eventType = WEB_ALERT, data = my_alert_message]
a string or a description of an object?
 
Upvote 0

mediait

Member
Licensed User
Longtime User
debug.jpg

It's the data value I need to extract. Anyone know how I can do this?
 
Upvote 0

mediait

Member
Licensed User
Longtime User
Thaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaank you!

for anyone interested this is it:

Sub wv_Event(MethodName As String, Args() As Object)
Dim we1 As JavaObject
we1 = Args(0)
Log(we1.RunMethod("getData",Null))
End Sub

Daestrum is King.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Not King, I just like reading API documentation (How sad am I ? )
 
Upvote 0
Top