German [B4J] JsBridge B4J-Sub-message > WebView-js >B4J-Sub-message [Beispiel geht]

a6000000

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


B4JsBridge.png



line 87: String: msg , in File B4JsBridge.html , in WebView javascript function:


function wv2B4J(){
var msg = document.getElementById("inputField").value;
alert(msg); }


wird msg mit alert(msg) zu B4J Sub gesendet,
und die msg in alert(msg) wird in B4J Sub:
line 48:

Sub wv_Event(MethodName As String, Args() As Object)
... Dim dat As String = we1.RunMethod("getData",Null) 'dat is the msg ...
End Sub

empfangen,
ich suche eine Variante ohne alert(msg)


Frage: wie vermeide ich das js alert(msg) , ich suche eine Alternative um eine message zu B4J Sub zu senden.

Vielleicht brauch ich andere we.RunMethod(), wo steht was? welche gibt es ausser "setOnAlert"?
//am end von Sub AppStart:
we.RunMethod("setOnAlert", Array(e))

und wo steht was ueber
Sub wv_Event(MethodName As String, Args() As Object)


?

Ich wollte jQuery ajax zum Server durch message senden an B4J umschreiben, weil mein html-js Programm im WiFi ist und kein Internet braucht.


code download zip:
http://www.mediafire.com/file/q3x4mabuxhp8981/8_B4J2jsBRIDGE.zip/file




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/
' see https://www.b4x.com/android/forum/threads/making-upcalls-from-javascript-to-javafx.51794/#post-374183


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)    'dat is the msg <<======
    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);      // <<====== receive String msg in B4J
       // how to get the message without alert
       // how can we send msg to B4J Sub  without alert() , alternative RunMethod  ?
    }
</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"/>
</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
 

Attachments

  • B4JsBridge.png
    B4JsBridge.png
    13.5 KB · Views: 184
Last edited:

a6000000

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


endlich was functioniert gefunden /

finally found what works:

from WebViev click to B4J

 
Last edited:
Top