B4A Library WebViewExtras

Hi all.

WebViewExtras is my latest library.
It's a much updated version of JSInterface.

WebViewExtras exposes more of the available native Android WebView methods to your B4A application:

addJavascriptInterface(webView1 As WebView, interfaceName As String)

Add a javascript interface to webView1, methods of the interface can be accessed using javascript with the interfaceName as the javascript namespace.

The interface contains just a single overloaded method CallSub().
The CallSub method signatures are:

CallSub(subName As String, callUIThread As boolean)
CallSub(subName As String, callUIThread As boolean, parameter1 As String)
CallSub(subName As String, callUIThread As boolean, parameter1 As String, parameter2 As String)
CallSub(subName As String, callUIThread As boolean, parameter1 As String, parameter2 As String, parameter3 As String)


So if you have added the interface to your webView with the interfaceName of "B4A" then you can write javascript such as:

B4X:
B4A.CallSub('MySubName', true)

The callUIThread parameter is an important update - it's not available with JSInterface.

Does the Sub called by your javascript modify your activity UI?
If the answer is yes then you need to pass boolean true as callUIThread otherwise you can pass false.
If you pass false and then the Sub tries to modify your activity UI you will get an exception.

Does your javascript require a return value from your Sub?
If the answer is yes then the Sub MUST NOT modify the activity UI.
If CallSub is excuted with callUIThread set to true then no values will be returned from your Sub to the javascript.

You will need to structure your B4A code so that Subs that return values to javascript do not modify the activity UI.

addWebChromeClient(webView1 As WebView, EventName As String)

Add a WebChromeClient to webView1.

The default B4A WebView has no WebChromeClient.
A WebChromeClient handles many things, the WebChromeClient that WebViewExtras adds to your WebView enables:

Version 1.30 of WebViewExtras requires that an additional EventName parameter is passed to the addWebChromeClient method, see this post: http://www.b4x.com/forum/additional-libraries-official-updates/12453-webviewextras-2.html#post102448

clearCache(webView1 As WebView, includeDiskFiles As boolean)

Clear the WebView cache.
Note that the cache is per-application, so this will clear the cache for all WebViews used in an application.

boolean includeDiskFiles - If false, only the RAM cache is cleared.

executeJavascript(webView1 As WebView, javascriptStatement As String)

Executes a string of one or more javascript statements in webView1.
javascriptStatement - A string of one or more (semi-colon seperated) javascript statements.

flingScroll(webView1 As WebView, vx As Int, vy As Int)

flingScroll is a poorly documented method of the WebView.
It's included in WebViewExtras as it may be useful but i can find no documentation for it or it's parameters.

vx and vy do not seem to be pixel values - i suspect they are velocity values for the kinetic/fling scroll.

pageDown(webView1 As WebView, scrollToBottom As boolean)

Scroll the contents of webView1 down by half the page size.

scrollToBottom - If true then webView1 will be scrolled to the bottom of the page.

Returns a Boolean value to indicate the success or failure of the scroll.

pageUp(webView1 As WebView, scrollToTop As boolean)

Scroll the contents of webView1 up by half the page size.

scrollToTop - If true then webView1 will be scrolled to the top of the page.

Returns a Boolean value to indicate the success or failure of the scroll.

zoomIn(webView1 As WebView)

Perform zoom in on webView1.

Returns a Boolean value to indicate the success or failure of the zoom.

zoomOut(webView1 As WebView)

Perform zoom out on webView1.

Returns a Boolean value to indicate the success or failure of the zoom.

Up to date documentation/reference for this library can be found here: http://www.b4x.com/forum/additional-libraries-official-updates/12453-webviewextras-3.html#post106486.

Library and demo code is attached to this post.

The demo is a bit brief - sorry but i don't have time to write demo code for all the new methods.
The demo displays two WebViews - the top WebView has a JavascriptInterface and WebChromeClient whereas the lower WebView has neither - it is the default B4A WebView.

Martin.

Edit by Erel:
- There is a security issue related to AddJavascriptInterface in older versions of Android. See this link: https://www.b4x.com/android/forum/t...ascriptinterface-vulnerability.85032/#content
- v2.20 is attached. This is the latest version.
 

Attachments

  • WebViewExtras_v1_42.zip
    7.8 KB · Views: 9,522
  • v2.20.zip
    41.1 KB · Views: 846
Last edited by a moderator:

ArminKH

Well-Known Member
@arminkh

  • If you use WebViewExtras AddWebChromeClient then you are adding support for javascript dialogs to the WebView.
  • Don't add the WebChromeClient to the WebView and i'm sure you will get no javascript dialogs.

Look at the documentation for the WebChromeClient: http://developer.android.com/reference/android/webkit/WebChromeClient.html
Look at it's onJsAlert() and onJsPrompt() callback methods.

You need a custom WebChromeClient that implements it's own callbacks for the javascript dialogs and from there you can display your own message - or take whatever action you desire.

A custom WebChromeClient would be created as a java class and th

HTH.
I cant remove webchromclient because i want extract the source of site and webchromclient is necessary for this
I am not familar with java syntax
 

warwound

Expert
Licensed User
Longtime User
Which version of WebViewExtras are you using?
Is it the version (one) available to download in post #1 of this thread?
Or are you using WebViewExtras2 that's only available to download from my server at b4a.martinpearman.co.uk?
 

ArminKH

Well-Known Member
I use version 1.4
Can u create an option in addwebchromeclient as boolean for enable or disable javascript alerts?
 

ArminKH

Well-Known Member
Are you sure about that?
I'd have guessed that you only need a JavascriptInterface?
Yes
i put this in global
B4X:
Dim Tojava,SCJAVA As WebViewExtras
and put this in activity_create
B4X:
 SCJAVA.addJavascriptInterface(WebView1, "B4A")
          SCJAVA.addWebChromeClient(WebView1, "")
then login to my site by this

B4X:
Tojava.executeJavascript(WebView1,"document.getElementById('txtUserName').value='"&txtusername.Text&"';")
          Tojava.executeJavascript(WebView1,"document.getElementById('txtPassword').value='"&txtpassword.Text&"';")
          Tojava.executeJavascript(WebView1,"document.getElementById('TXT_CAPT').value='"&captchacode.Text&"';")
          Tojava.executeJavascript(WebView1,"document.getElementById('LoginButton').click();")
then extract source of site by this

B4X:
Sub Process_HTML(Html As String)
                 source=Html
          End Sub

BUT When user and pass is wrong webview show an alert and i want to disable that
 
Last edited:

warwound

Expert
Licensed User
Longtime User
Try the attached modified version of WebViewExtras 1.4.
You need WebViewExtras as well as this modified WebViewExtrasArminkh library checked in the b4a IDE library list.
Now find and replace all occurences of WebViewExtras with WebViewExtrasArminkh.
For example change:
B4X:
Dim WebView1 As WebView
to:
B4X:
Dim WebView1 As WebViewArminkh

The WebChromeClient no longer displays javascript dialogs (untested!).

Martin.
 

Attachments

  • WebViewExtrasArminkh.zip
    7.7 KB · Views: 419

ArminKH

Well-Known Member
i will test this tonight
tnx for your quick response martin;)
 

oceanwanderlust

Member
Licensed User
Longtime User
Earlier in this thread there is some discussion of disabling the clipboard by handling the long click, but I am unsure of the current status. Using AJWebkit, I can receive the LongPress in my DefaultWebViewClient, but even when I return True, the clipboard still appears. How do I intercept this event so that the clipboard does not appear?
 

warwound

Expert
Licensed User
Longtime User
WebViewExtras updated to version 2.41

This update makes the executeJavascript method compatible with android API level 19+.
More info can be found here: https://www.b4x.com/android/forum/threads/webview-webviewextras-javascript-and-sdk21.48968/.
WebViewExtras v 2.41 is attached to the first post i this thread.

I have also applied the same update to the (never got officially released) WebViewExtras2 library.
WebViewExtras2 can be downloaded from: http://b4a.martinpearman.co.uk/webviewextras/WebViewExtras2-v2.10.zip.
 

ArminKH

Well-Known Member
Tnx 4 new update
In this update you combine modified web view extra arminkh in above post(you create this modified version for me before)with default version? And if not can you please create new modified version of new update for me?
 

warwound

Expert
Licensed User
Longtime User
Tnx 4 new update
In this update you combine modified web view extra arminkh in above post(you create this modified version for me before)with default version? And if not can you please create new modified version of new update for me?

I've attached the modified version for you.
 

ArminKH

Well-Known Member
Can i use default version and modified version together?(in an activity i want show modal dialog and in other activity i dont want show that)
I think if i use this two lib together maybe app will be crashed
Is this wrong?
Tnx again for your quick response and excuse me for my english
 

warwound

Expert
Licensed User
Longtime User
Can i use default version and modified version together?(in an activity i want show modal dialog and in other activity i dont want show that)
I think if i use this two lib together maybe app will be crashed
Is this wrong?
Tnx again for your quick response and excuse me for my english

I've recompiled your update so that it's java packagename is different to the normal version of WebViewExtras.
You should now be able to use the normal version and your version in the same application.
 

Attachments

  • WebViewExtrasArminkh-v2.41.zip
    8.1 KB · Views: 396

BobsYourUncle

Member
Licensed User
Longtime User
Hi Martin,
A big thank you for your WebViewExtras library! Saved my bacon today!
Works a treat on KitKat as well...
Cheers,
Bob.
 

Siam

Active Member
Licensed User
Longtime User
Hello,

i hope that i be right here?

since Android 4.4.x it was not possible to upload files with webview because google has removed this function. Now Android 5 is Out and it should work with it.
But here is my Problem i must say to the webview that it is allowed to open a File Selector here is what i have found about it:

public boolean onShowFileChooser (WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams)
Added in API level 21
Tell the client to show a file chooser. This is called to handle HTML forms with 'file' input type, in response to the user pressing the "Select File" button. To cancel the request, call filePathCallback.onReceiveValue(null) and return true.

Parameters
webView The WebView instance that is initiating the request.
filePathCallback Invoke this callback to supply the list of paths to files to upload, or NULL to cancel. Must only be called if the showFileChooser implementations returns true.
fileChooserParams Describes the mode of file chooser to be opened, and options to be used with it.
Returns
  • true if filePathCallback will be invoked, false to use default handling.
from url:
https://developer.android.com/refer...oid.webkit.WebChromeClient.FileChooserParams)

My question is now. is it possible to add this function to webviewextras ?

lg

Andy
 

bluedude

Well-Known Member
Licensed User
Longtime User
Hi, is this working on API 21?

I'm trying to use it after a long time and it does not seem so work anymore on Android 4.4.4.

Which one is the latest version, 1.41?
 

warwound

Expert
Licensed User
Longtime User
Version 1.41 is the latest version of the original WebViewExtras.
WebViewExtras2 is available for download from my server (let me know if you want a link).

Both libraries call the same android API methods but WebViewExtras2 is more complete - wrapping more of the API.

Kitkat broke a number of WebView related functions.
For example file uploads do not work in Kitkat - don't work in java and don't work in b4a.
Lollipop has restored some functions but it's unclear exactly what works and what doesn't - unless you have many hours to research the subject.

What exactly are you trying to do?
 

bluedude

Well-Known Member
Licensed User
Longtime User
Thanks warwound, got it working with Bootstrap, jQuery and your library. Pretty cool and responsive and hybrid.
 
Top