iOS Question How to replace B4A.CallSub in B4i?

watesoft

Active Member
Licensed User
Longtime User
The following code works in B4A, but does not work in B4I,I searched the forum and found that there is no such feature in B4I. I found a method in https://www.b4x.com/android/forum/threads/b4i-addjavascriptinterface.72837/ , but it seems to be different from my needs,I don’t know how to start. Who can help me? thank you very much.

B4X:
Private Webview1 As WKWebView
Sub Webview1_OverrideUrl (Url As String) As Boolean
    Dim C_UTF8 As StringUtils
    Url=C_UTF8.DecodeUrl(Url,"UTF8")
    If Url.Length >= 8  And  Txt_Verse.Text<>"" Then
        Dim javascript As String
        javascript=$"var bb=document.getElementsByName('B${Txt_Verse.Text}')[0];
                            var kk=bb.style.color;
                            if (kk=="blue")
                            {
                                bb.style.color="red";
                            }
                            else
                            {
                                bb.style.color="blue";
                            }
                           
                            function join(){
                            var bb=document.getElementsByTagName('a');
                            var mm=new Array();
                            for(var i=0;i<bb.length;i++){
                                var kk=document.getElementsByName("B"+(i+1))[0];
                                if(kk.style.color=="red")
                                {
                                    mm.push(i+1);
                                }
                            }
                                return mm.join("/");
                        }
                   B4A.CallSub('GetVarFromWebview_1', true, join());"$
        Webview1.EvaluateJavaScript("Webview1",javascript)
    End If
    Return True
End Sub

Sub GetVarFromWebview_1(WebVar As String)
    Dim Verse() As String
    If WebVar<>Null And WebVar<>""  Then
        Verse=Regex.Split("/",WebVar)
        '.............
        '.............
    End If
End Sub
 
Last edited:

watesoft

Active Member
Licensed User
Longtime User
I don't think that B4A.CallSub is needed here. You can run a javascript that will return the string. Check this project: https://www.b4x.com/android/forum/threads/b4x-webview-ckeditor-rich-text-editor.133260/#content
Erel,Thanks for your help.
I copy the sub of RunJavaScript to my project, Line of "Dim sf As Object = WebView1.EvaluateJavaScript(js)" shows error: "Cannot assign void value".
The reason for the error is that I am using WKwebview instead of native webview. Due to the needs of other functions, I cannot use webview. How to solve this problem?
 
Upvote 0

watesoft

Active Member
Licensed User
Longtime User
The problem is solved.Remove "B4A.CallSub('GetVarFromWebview_1', true, join())" from javascript string,and put GetVarFromWebview_1 into Sub WV_Main_JSComplete.
B4X:
Sub WV_Main_JSComplete (Success As Boolean, Tag As Object, Result As String)
    If Success Then
        GetVarFromWebview_1(Result)
    End If
End Sub
 
Upvote 0
Top