Android Question B4a Webview javascript variable or function return

Yvon Steinthal

Active Member
Licensed User
Longtime User
Hello,

I have looked through the WebViewExtras library and i still cant make my code work...
What im trying to do is call a function or variable from B4A that would return me some value from the webview's javascript.

Here's a modified version of the code i have:

B4X:
pnlDemoVideo.Initialize("")
    pnlDemoVideo.Color=  Colors.White
    Activity.AddView(pnlDemoVideo,0,0,Activity.Width,Activity.Height)
 
'Initializing webview and setting permission for camera usage
    wv_Tele.Initialize("wvTele")

'Verifying permission and setting the WebViewExtra object.
rp.CheckAndRequest(rp.PERMISSION_CAMERA)
 
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result Then
        Dim client As JavaObject
        client.InitializeNewInstance(Application.PackageName & ".main$MyChromeClient", Null)
        Dim jo As JavaObject = wv_Tele
        jo.RunMethod("setWebChromeClient", Array(client))
        
       CamViewExtras.Initialize(wv_Tele)
CamViewExtras.LoadUrl(TeleUrl)
CamViewExtras.JavaScriptEnabled = True
    CamViewExtras.AddJavascriptInterface(wv_Tele ,"B4A")
    CamViewExtras.ExecuteJavascript("chicken()")

End If

I know this code is missing something regarding getting the value back, and i think thats where i am clueless. Any help would be greatly appreciated...


Y.
 

DonManfred

Expert
Licensed User
Longtime User
You are not providing enough informations. what is the content of your javascript-code?

Upload a small project which shows the issue.
 
Upvote 0

Yvon Steinthal

Active Member
Licensed User
Longtime User
Im sorry, I am trying to do something like this:

B4X:
  Dim Javascript As String="B4A.CallSub('GetVarFromWebview', true, chicken())"
  
    WebViewExtras1.executeJavascript(WebView1, Javascript)

Sub GetVarFromWebview(WebVar As String)
   
    Log(WebVar)
    'This is where i would do something different according to the returned variable from the function chicken().

End Sub

I just want to call a javascript function that would return a variable to my B4A app so that i can (for example) close the webview depending on the returning value.
In truth its a bit more complicated as i am doing a WebRTC video conference and the user on the other end can potentially control my app (is what im trying to do).


PS: The above code does not work for me, it never triggers my function GetVarFromWebView.

I hope this makes things clearer?

Y.
 
Upvote 0

Yvon Steinthal

Active Member
Licensed User
Longtime User
but you are not calling this script. You are calling
chicken()

Yes No, i asked my question incorrectly, my first post was more of a general question as to what needed to be done for me to get a return value. My second post was deeper into what i needed to happen. Overall my second post doesnt work... and i cant trigger my B4A function.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
can you upload a html file with the chicken function included?

B4X:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
    <title>UDXLog Datenschutz, Haftungsausschluss u.s.w.</title>
  </head>
<script type="text/javascript">
function chicken(){
    return Math.PI;
};
</script>
<body>

<form method="post" id="TSForm">
<div id="q3" class="q required">
<label class="question top_question" for="RESULT_TextField-3">First Name&nbsp;<b class="icon_required" style="color:#FF0000">*</b></label>
<input type="text" name="RESULT_TextField-3" class="text_field" id="RESULT_TextField-3"  size="25" value="" />
</div>
<div id="q4" class="q required">
<label class="question top_question" for="RESULT_TextField-4">Last Name&nbsp;<b class="icon_required" style="color:#FF0000">*</b></label>
<input type="text" name="RESULT_TextField-4" class="text_field" id="RESULT_TextField-4"  size="25" value="" />
</div>
</form>

</body>

</html>

B4X:
Sub WWW_PageFinished (Url As String)
    Log($"WWW_PageFinished(${Url})"$)
    Dim Javascript As String
    Javascript=$"B4A.CallSub('GetVarFromWebview', true, chicken());"$
    'Log(Javascript)
    WebViewExtras1.executeJavascript(WebView1, Javascript)
End Sub
Sub GetVarFromWebview(WebVar As String)
    Log($"GetVarFromWebview(${WebVar})"$)
End Sub

B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
DefaultWebViewDatabasePath:
NewWebViewDatabasePath:
** Activity (main) Resume **
WWW_PageFinished(file:///android_asset/my_webpage.htm)
GetVarFromWebview(3.14159)
 

Attachments

  • wbjs.zip
    7.9 KB · Views: 393
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
maybe the $ at the beginning and at the end of the javascript call?
No. I just used the smart string literal to put a complete Script into the variable

i tested out a bit.

B4X:
    Dim Javascript As String
    Javascript=$"function chicken(){
    return Math.PI;
}
    var value = chicken();
    B4A.CallSub('GetVarFromWebview', true, value);"$
Using the smart string literal i was able to write a "multiline" script.

In this case
B4X:
Dim Javascript As String
    Javascript=$"B4A.CallSub('GetVarFromWebview', true, chicken());"$
the smart string literal is not really used.

So you could also write
B4X:
Dim Javascript As String
    Javascript="B4A.CallSub('GetVarFromWebview', true, chicken());"
 
Upvote 0
Top