Android Question How to inject text to webview

laomms

Member
Licensed User
Longtime User
I'm use WebViewExtras lib, there is two webpage, the original page is online website, I download a piece of source as local page for debug.
Just like attachment picture, after click next on the webpage1, I want auto insert edittext content when button is clicked on the webpage2, and copy the string from the webpage2.
B4X:
Sub Process_Globals
End Sub

Sub Globals
    Dim WebViewExtras1 As WebViewExtras
    Dim WebView1 As WebView
    Private Button1 As Button
    Private Button2 As Button
    Private EditText1 As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("main.bal")
    'WebView1.SetLayout(0,0,100%x,80%y)

    '    add the JavascriptInterface to WebView1
    WebViewExtras1.addJavascriptInterface(WebView1, "B4A")
    WebViewExtras1.addWebChromeClient(WebView1,"Chrome")
    EditText1.text="1111111 2222222 3333333"
    WebView1.LoadUrl("file:///android_asset/test2.html")
End Sub


Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
 
    ........
End Sub

Sub Button2_Click

    ..........
End Sub

Sub WebView1_PageFinished (Url As String)
    Dim Javascript As String
    Javascript="B4A.CallSub('ProcessHTML', false, document.documentElement.outerHTML)"
 
    Log("PageFinished: "&Javascript)
    'If Url="url_of_the_login_page" Then
    Dim Javascript As String
    Dim MyWebViewExtras As WebViewExtras   '   you could make this a global variable if you use it elsewhere in your code
 
 
    Javascript ="B4A.CallSub('ProcessHTML', true, document.getElementById('field1').getAttribute('value'))"
    MyWebViewExtras.executeJavascript(WebView1, Javascript)
    Log("Javascript")
 
        '    alternatively you could execute the javascript without requiring WebViewExtras by using the javascript protocol:
        '    WebView1.LoadUrl("javascript:"&Javascript.ToString)
        Log("PageFinished: "&Javascript)

End Sub

Sub ProcessHTML(Html As String)
    '    This is the Sub that we'll get the web page to send it's HTML content to
 
    '    Log may truncate a large page so you'll not see all of the HTML in the log but the 'html' String should still contain all of the web page HTML
 
    Log("ProcessHTML: "&Html)
End Sub

I don't know how to catch this html element:

B4X:
<div class="align-center input">   
    <div class="ui-input-text ui-body-inherit ui-corner-all ui-shadow-inset">
        <input onkeypress="return event.charCode >= 48 &amp;&amp; event.charCode <= 57" id="field1" type="tel" value="" pattern="[0-9]*">
    </div>
</div>




aaaa.png bbbb.png
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

laomms

Member
Licensed User
Longtime User
ok, I made it. It's simple:
B4X:
Sub Button1_Click
    WebViewExtras1.executeJavascript(WebView1,"B4A.CallSub('Process_HTML', true, document.documentElement.outerHTML)")
End Sub

private Sub Process_HTML(Html As String)
    Dim Javascript As StringBuilder
    Javascript.Initialize
    Javascript.Append("field1.value='1111111';")
    Javascript.Append("field2.value='2222222';")
    Javascript.Append("field3.value='3333333';")
    WebViewExtras1.ExecuteJavascript(WebView1,Javascript.ToString)

End Sub

but why can not use this:

B4X:
Sub Button1_Click
    str=Regex.Split(" ",EditText1.Text)
    For k=3 To str.Length-1 Step 3
        str(k)=str(k)&Chr(10)                           
    Next
    WebViewExtras1.executeJavascript(WebView1,"B4A.CallSub('Process_HTML', true, document.documentElement.outerHTML)")

End Sub


private Sub Process_HTML(Html As String)
    Dim Javascript As StringBuilder
    Javascript.Initialize
    Dim nStr As String=

    For i=1 To str.Length
        nStr="field" & i & ".value=" & "'" & str(i-1) & "'" & ";"
        Javascript.Append(nStr)       'nothing append
    Next
    WebViewExtras1.ExecuteJavascript(WebView1,Javascript.ToString)
End Sub
 
Last edited:
Upvote 0
Top