Is this possible?

Cableguy

Expert
Licensed User
Longtime User
In my quest to achieve some degree of automation on a Web based game, I am strugling with the Http methods...So I remembered that in B4Ppc I made a version of Andrew's Browser control, wich among other methods, could retrieve a web page "field" name, ID or value, or set it providing you knew the ID or the name of the field, and even trigger it's click event (i.e. execute the click of a button on a form)...

Is this possible In Android?
 

lagore

Active Member
Licensed User
Longtime User
Hi Cableguy,
We must be following each other along the same lines as I have just done this because of a problem I have using HttpUtils due to a circular redirection.
I use the JSInterface and it works really well, the app that I am using it with has a webview but the webview is only inititilised but never loaded into the activity as I do not need to see the site just get data from the HTML.
have a look at this post from Martin who wrote the JSInterface

The code that I use has 2 variations ,
B4X:
javascript="var logonForm=document.forms.logonForm;logonForm.username.value='yourname';logonForm.password.value='yourpassword';logonForm.submit();"
myInterface.execJS(webview1, javascript)

B4X:
javascript="document.all.Id.value='yourname';document.all.Pass.value='yourpassword';dothejob();"
myInterface.execJS(webview1, javascript)
the difference between the two is the way the two sites are coded, the first one is,
HTML:
              <tbody>
                  <tr>
                    <td class="nowrap"><label for="username">Username:</label></td>
                    <td class="txtpad">
                        <input class="txt" id="username" name="username" type="text" />
                    </td>
                  </tr>
                  <tr>

                    <td class="nowrap"><label for="password">Password:</label></td>
                    <td class="txtpad">
                        <input class="txt" id="password" onfocus="g_fFcs=0" type="password" name="password" />
                    </td>
                  </tr>
                  <tr>
                    <td class="nowrap">&nbsp;</td>
                    <td class="txtpad" colspan="2">

                        <input class="btn" onmousedown="this.className='btnOnMseDwn'" id="SubmitCreds" onmouseover="this.className='btnOnMseOvr'" onclick="clkLgn()" onmouseout="this.className='btn'" type="submit" value="Log On" name="SubmitCreds" /> 
                    </td>
                  </tr>
              </tbody>
you can see where I got the field name from and also the button has a 'submit' type, the form also had a title of
HTML:
<form action="/CookieAuth.dll?Logon" method="post" id="logonForm" autocomplete="off">
and that is where I got the ID of "logonForm"
The second one did not have a form ID and had two buttons with the same type.
HTML:
<input id="Id" type="text" onkeypress="Enter_Web(event)" onkeydown="KeyIsDown(true)" maxlength="10" size="10">
<br>
<b>
<input id="Pass" type="password" onkeypress="Enter_Web(event)" onkeydown="KeyIsDown(false)" maxlength="8" size="10">
<br>
<br>
<input id="B1" class="login" type="button" name="B1" value="Login" onclick="dothejob()" style="color: blue;">
<input id="CLS" class="login" type="button" name="CLS" value="Clear" onclick="clearform()" style="color: blue;">
so with a small bit of trial and error "document.all." worked to reference the form and I activated the on click javascript directly with "dothejob()".
When the web page is loaded the "Sub web_PageFinished (Url As String)" will run and this is where you put your code to handle the data.
The only complication is getting the data from the webview after it has loaded, this is where HttpUtils wins (if you can get it to work) as once the 'post' has returned you then do a 'get' to load the HTML of the site. WebView has no function to get its loaded HTML (not sure if this is a limitation of Android or a lacking facility of B4A "Put it on the wishlist Erel" We can CaptureBitmap but cannot get the underlying HTML this is where JSInterface comes to the rescue again. This makes the javascript work in the other direction and sends the HTML to your B4A app
B4X:
myInterface.addJSInterface(WebView1, "B4A")

Dim jsStatement As String
      
   jsStatement="B4A.CallSub('processHTML', document.documentElement.outerHTML)"
      
   Log("PageFinished: "&jsStatement)
   myInterface.execJS(WebView1, jsStatement)

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
   PageContent = html
   Log(PageContent)
End Sub
The only complication is you might get a thread execption as the processHTML is generated on a different thread then the main app (I think) so needs to be handled carefully. As I said earlier if you can get HttpUtils to work then this is by far the better and much quicker way to go.
Edward
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Thanks for the pointer...

Well, There are two fields to be filled (username and password) and then a hidden field to be "traped", and then force the submit of the form...

I know this can be done by adding some Jscript to the original html code, so I will go that way...

Most of my to be app work is done by retrieving data from html code, not neede to actually load the page..
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
The only complication is you might get a thread execption as the processHTML is generated on a different thread then the main app (I think) so needs to be handled carefully.
Edward

Hi.

I have actually created a new library which i intend to replace JSInterface.

It's got the same methods as JSInterface but can also handle calls from javascript to the Activity UI thread.
The new library is complete (been complete for a few weeks) but i've been too busy to document and upload it.

Leave it to me and i'll get it documented and uploaded over the weekend - hopefully tomorrow.

Martin.
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Great to hear that...

So it will be possible to "simulate" user actions over a web page (?)
 
Upvote 0
Top