B4J Question [B4J] jscriptengine JsBridge js-WebViev-to-B4J-to-js [working example]

a6000000

Member
Licensed User
Longtime User
[EDIT] better: https://www.b4x.com/android/forum/t...4j-and-from-b4j-to-webview-javascript.123547/

[solved]


finally found what works:

from WebViev click to B4J








Send data-message from WebView js function with js alert("message") to a B4J Sub.

How can I send the message without use of alert(msg) ?

working example JsBridge:

B4JsBridge.png


B4J JsBridge zip






B4J line 41:
Sub wv_Event(MethodName As String, Args() As Object)
...
line 46:
Dim dat As String = we1.RunMethod("getData",Null) '//here I get the String msg from the js alert(msg) in the B4J String dat

End Sub



B4J line 38:
we.RunMethod("setOnAlert", Array(e))

"setOnAlert" only responds to js alert(msg) in line: 85 in my WebView html copy

I need Alternative to receive a String from alert(String),


where do I read about others we.RunMethod like maybe "setOnMYFUNCTION" ?



all code:
B4X:
''' B4J !!!

#Region  Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 700
#End Region
' jsbridge
' http://www.mediafire.com/file/q3x4mabuxhp8981/8_B4J2jsBRIDGE.zip/file
' see https://www.b4x.com/android/forum/threads/jscriptengine.35781/

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form

    Private wv As WebView
    Private we, wvjo As JavaObject

    'B4J2wv
    Private TextArea1 As TextArea
    Private Button1 As Button
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("B4JsBridge")

    wv.LoadURL("file:///../B4X/B4JsBridge.html")   'c:/B4X/B4JsBridge.html
    MainForm.Show
    Wait For wv_PageFinished (Url As String)

    'vw2B4J
    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)

    'how can we send msg to B4J Sub  without alert() , alternative RunMethod  ?
    we.RunMethod("setOnAlert", Array(e))
End Sub

Sub wv_Event(MethodName As String, Args() As Object)
    Log(Args(0))
    Dim we1 As JavaObject = Args(0)
    Log(" getSource: "    & we1.RunMethod("getSource",Null))
    Log(" getEventType: " & we1.RunMethod("getEventType",Null))
    Dim dat As String =     we1.RunMethod("getData",Null)
    Log(" getData: "      & dat)

    TextArea1.Text = dat
End Sub
'vw2B4J end

'B4J2wv
Sub ExecuteJavaScript (wvwv As WebView, script As String) As Object
    Dim jo As JavaObject = wvwv
    Return jo.RunMethodJO("getEngine", Null).RunMethod("executeScript", Array(script))
End Sub

Sub Button1_Click
    Dim executejs As String = TextArea1.Text
    executejs = "B4J2wv("""  & executejs  & """)"
    Log(executejs)
    ExecuteJavaScript(wv, executejs)
End Sub
'B4J2wv end

#Region  B4JsBridge.html File content
#If htmlcontentWebView

<!doctype html><html lang="en-US">
<head>
<script type="text/javascript">
    // B4J 'ExecuteJavaScript(WebView1, $"document.body.style.backgroundColor = '#fbd7bd'"$)

    // B4J  JSBridge

    // B4JSub2jsWV
    function B4J2wv(message){
        document.getElementById("inputField").value = message;
    }

    // jsWV2B4JSub
    function wv2B4J(){
       var msg = document.getElementById("inputField").value;
       alert(msg);
       // how to get the message without alert
    }
</script>
<style>
    .tit {
        color:#b69221;
        font-size:300%;
        margin-top:-11px;
    }
    body {
       background-color:#fbd7bd;
    }
</style></head>
<body>
<center><h1 class="tit">in WebView </h1></center>
    <input id="inputField"  type="text" value="">
    <input onClick="wv2B4J()"  type="button" value="send to B4J"/>
    <!--input id="inputFieldtest"  type="text" value="test">
    <br><button Type="button" onClick="aaa()">Click alert</button-->
</body></html>

#End If
#End Region

#Region designer B4JsBridge.bjl
#If txttxt

    WebView: wv

     ------

    Label1:"in B4J"
    TextArea1:" ", Button1: "send to WebView"

#End If
#End Region



How can I send the msg to B4J Sub without an alert(msg)?
 
Last edited:

tchart

Well-Known Member
Licensed User
Longtime User
It's just calling a JavaScript function. You can pass whatever parameters you want and just do what you need to do in the JavaScript function.
 
Upvote 0

a6000000

Member
Licensed User
Longtime User
[EDIT] better: https://www.b4x.com/android/forum/t...4j-and-from-b4j-to-webview-javascript.123547/


finally found what works:

from WebViev click to B4J

 
Last edited:
Upvote 0
Top