B4J Question Call B4J sub from webview javascript

Discussion in 'B4J Questions' started by mediait, May 6, 2015.

  1. mediait

    mediait Member Licensed 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?
     
  2. mediait

    mediait Member Licensed 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?

    Code:
    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, 
    00, -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
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    Have you tried to handle the LocationChanged event with a "dummy" link?
     
  4. mediait

    mediait Member Licensed 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.
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    Use the debugger to find the fields. Put a breakpoint in this event and examine args.
     
  6. mediait

    mediait Member Licensed 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?
     
  7. mediait

    mediait Member Licensed User

    [​IMG]
    It's the data value I need to extract. Anyone know how I can do this?
     
    Erel likes this.
  8. Daestrum

    Daestrum Well-Known Member Licensed User

    There's a getData() method for web events

    Maybe try Arg(0) > JavaObject then jo.RunMethod("getData",null) on it?

    Not tried so this may be wrong.
     
    Erel, inakigarm and mediait like this.
  9. mediait

    mediait Member Licensed 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.
     
    Mashiane and Daestrum like this.
  10. Daestrum

    Daestrum Well-Known Member Licensed User

    Not King, I just like reading API documentation (How sad am I ? )
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice