Android Question WebviewExtras JS problem

Andrei E

Member
Licensed User
Longtime User
Hello dear developers
Can anyone advice me about properly usage of WebviewExtras?
I'm trying to replace webview elements and emulate submit button but got fail

Activity
B4X:
Sub Globals
    Dim Label1, Label2, Label3 As Label
    Dim Edittext1, Edittext2, Edittext3 As EditText
    Dim Webview1 As WebView
    Dim WebviewExtras1 As WebViewExtras
    Dim Button1 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Label1.Initialize("Label1")
        Label1.TextSize = 14
        Label1.Text = "Host (URL):"
    Label2.Initialize("Label2")
        Label2.TextSize = 14
        Label2.Text = "Username:"
    Label3.Initialize("Label3")
        Label3.TextSize = 14
        Label3.Text = "Password:"
    Edittext1.Initialize("Edittext1")
        Edittext1.TextSize = 14
        Edittext1.SingleLine = True
        Edittext1.Text = "http://localhost:8080/index.html"
    Edittext2.Initialize("Edittext2")
        Edittext2.TextSize = 14
        Edittext2.SingleLine = True
        Edittext2.Text = "admin"
    Edittext3.Initialize("Edittext3")
        Edittext3.TextSize = 14
        Edittext3.SingleLine = True
        Edittext3.Text = "1234567890"
        Edittext3.PasswordMode = True
    Button1.Initialize("Button1")
        Button1.TextSize = 14
        Button1.Text = "Login"
    Webview1.Initialize("Webview1")
        Webview1.JavaScriptEnabled = True
        Webview1.Visible = False
    WebviewExtras1.Initialize(Webview1)
 
    Activity.AddView(Webview1, 0, 0, 100%x, 100%y)
    Activity.AddView(Label1, 10dip, 10dip, 100%x - 20dip, 20dip)
    Activity.AddView(Edittext1, 10dip, 30dip, 100%x - 20dip, 30dip)
    Activity.AddView(Label2, 10dip, 60dip, 100%x - 20dip, 20dip)
    Activity.AddView(Edittext2, 10dip, 80dip, 100%x - 20dip, 30dip)
    Activity.AddView(Label3, 10dip, 110dip, 100%x - 20dip, 20dip)
    Activity.AddView(Edittext3, 10dip, 130dip, 100%x - 20dip, 30dip)
    Activity.AddView(Button1, 10dip, 160dip, 100%x - 20dip, 45dip)
 
    Activity.AddMenuItem("Go Back", "Menuitem1")
End Sub

Sub Menuitem1_Click()
    Label1.Visible = True
        Label2.Visible = True
        Label3.Visible = True
    Edittext1.Visible = True
        Edittext2.Visible = True
        Edittext3.Visible = True
    Button1.Visible = True
    Webview1.Visible = False
End Sub

Sub Button1_Click()
    Label1.Visible = False
        Label2.Visible = False
        Label3.Visible = False
    Edittext1.Visible = False
        Edittext2.Visible = False
        Edittext3.Visible = False
    Button1.Visible = False
    Webview1.Visible = True
    Webview1.LoadUrl(Edittext1.Text)
End Sub

Sub Webview1_PageFinished (Url As String)
    Dim Javascript As String
    Javascript = "document.forms['logon'].getElementById('username').value='" & Edittext2.Text & "';" & _
                "document.forms['logon'].getElementById('password').value='" & Edittext3.Text & "';" & _
                "document.forms['logon'].submit();"
    WebviewExtras1.executeJavascript(Javascript)
    Activity.Title = WebviewExtras1.GetTitle
End Sub
Html
B4X:
<html>
<head>
<title>Login</title>
</head>
    <form action="./login.php" method="post">
        <div id="logon">
            <span>
                Username: <input type="text" name="username" />
                <br>
                Password: <input type="password" name="password" />
                <br>
                <input type="submit" name="login" value="Login" />
            </span>
            <input type="hidden" name="token" value="abcdef1234567890">
        </div>
    </form>
</body>
</html>
 

Attachments

  • project_source.zip
    254.1 KB · Views: 213
  • activity.jpg
    activity.jpg
    18.3 KB · Views: 214
  • result.jpg
    result.jpg
    11.1 KB · Views: 196

Cableguy

Expert
Licensed User
Longtime User
Search the forum...
You're missing a 'Submit()' action somewhere on your form's html
 
Upvote 0

Andrei E

Member
Licensed User
Longtime User
login.php
B4X:
<?php
    echo "You're logged as <b>" . $_POST['username'] . "@" . $_POST['password'];
?>
No, it's fine for manual use
There is simething wrong B4a code but I can't fix it properly

Maybe I should to set additional permission in project? bcoz a simple alert JS is also not working((

B4X:
Javascript = "alert(" & Chr(34) & "I am an alert box!" & Chr(34) & ");"
WebviewExtras1.executeJavascript(Javascript)
 

Attachments

  • test_login1.jpg
    test_login1.jpg
    42.9 KB · Views: 206
  • test_login2.jpg
    test_login2.jpg
    45 KB · Views: 188
Last edited:
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
I didn't notice the JavaScript.submit() line...

Well, from your code you seem to be placing views on top of the original form, why? Why not simply let the user interact with the webpage normally? Are you aiming to do some auto-submit form or something else?
Jason and webextras allow you to interact with the web elements of virtually any page and even to add extra events to web actions so that you can take additional action on your code.
So, without knowing exactly what you are trying to do ( I thought you were trying to auto submit a form), we cannot offer more insight than this.
 
Upvote 0

Andrei E

Member
Licensed User
Longtime User
I'm trying to autologin in some forum but the original login page contains hiiden values (token, session etc.) and I can't pass it by direct POST via HttpUtils2 bcoz everytime when I'm loading index page, all hidden values also changes. My task is load index page, read hidden values, autocomplete all field (like as username and password) and send to server
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
I have done something similar... Same situation.
Assuming you KNOW the form fields names, you recreate your login view and with JS you pass the values to the proper web form field, after that, just submit().
You DON'T need to read the hidden values, just have your views values passed into the correct form field and submit. Each token has an alive duration, so even if you submit after a few minutes, it will be recognised as valid.
 
Upvote 0

Andrei E

Member
Licensed User
Longtime User
That is my problem - I can't pass fields with my data (you can see it at 1st and 2nd screenshots)
This is little VB.NET example for my task:
B4X:
WebBrowser1.Document.GetElementById("username").InnerText = "admin"
WebBrowser1.Document.GetElementById("password").InnerText = "123456789"
and it's working perfactly, but B4a code is not

Cableguy, thanks for mall your suggestions, but can you show me any working example
Best Regards
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
I'm away from my laptop for the afternoon, so I will only be able to help you later on the day... I don't know if I still have that on hand, but shortly I will have to reproduce that exact thing, so helping you will eventually help me
 
Upvote 0

Andrei E

Member
Licensed User
Longtime User
Thanks
I'll wait your code

Best Regards
 
Upvote 0

Andrei E

Member
Licensed User
Longtime User
Well, it's done and working
At 1st time I got server notifycation about coockies, but with CookieManager my code working correctly and local and at real server

Cableguy, thanks man, you did my day)
:)

Working code:
B4X:
Sub Globals
    Dim isReady As Boolean
    Dim Webview1 As WebView, WVExtras1 As WebViewExtras
    Dim jsi As JSInterface
    Dim CookieManager1 As CookieManager
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Webview1.Initialize("Webview1")
    WVExtras1.addWebChromeClient(Webview1, "Webview1")
   
    jsi.addConsoleInterface(Webview1)
    CookieManager1.SetAcceptCookies(True)
   
    Activity.AddView(Webview1, 0, 0, 100%x, 100%y)
    Webview1.LoadUrl("http://localhost:8080/index.html")
End Sub

Sub Webview1_PageFinished (Url As String)
    If isReady = False Then
        Dim sJavascript As String
        sJavascript= "document.forms.logon.username.value='admin';" & _
                    "document.forms.logon.password.value='12345';" & _
                    "document.forms.logon.submit();"
        jsi.execJS(Webview1, sJavascript)
        isReady = True
    End If
    If CookieManager1.HasCookies Then Log("Cookies: " & CookieManager1.GetCookie(Url))
    Log(Url)
End Sub
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Glad to help you with that... As I said, I ended up helping myself as I am going to need it shortly.
 
Upvote 0
Top