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,506
  • v2.20.zip
    41.1 KB · Views: 826
Last edited by a moderator:

Intiwhiz

Member
Licensed User
@warwound , I have web application, and for access via android use webextra library and webview, but if I use filedialog from webview, there's no choice from camera phone.

If my web application, open from browser on android, there's choice fileupload from camera.

Have solution for this case ?

thanks
 

ekologi

New Member
Licensed User
Longtime User
Dear All, masters,
I was been enabled using camera input in a html page using browser BUT not success using webview, requirement was to enable user to take picture and upload. Using WebViewExtras only give me access to upload file from gallery. Is there any way to accessing camera within webview?
Thanks in Advanced
 

Intiwhiz

Member
Licensed User
yes, i have same problem, the choice from camera didn't shown. If I use browser from android it can be shown
 

Attachments

  • WhatsApp Image 2016-10-15 at 12.15.31 PM.jpeg
    WhatsApp Image 2016-10-15 at 12.15.31 PM.jpeg
    66.7 KB · Views: 328

Mashiane

Expert
Licensed User
Longtime User
There appears to be something in the Javascript that the webview doesn't like on that site, if you add the lines:

B4X:
    Dim WVJO As JavaObject = WebView1
    WVJO.RunMethod("setWebContentsDebuggingEnabled",Array(True))

You can then use the chrome debugging tools on a USB connected Android device at chrome://inspect/#devices in a Google Chrome Browser on your PC to access a huge amount of debugging information.

More info here: https://developers.google.com/web/t...debug/remote-debugging/remote-debugging?hl=en
This is so powerful thanks a million...
 

Leni Berry

Active Member
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.

Dear warwound,
where is the library WebViewExtras2 version 2.41? i cant find the link...
 

warwound

Expert
Licensed User
Longtime User
Dear warwound,
where is the library WebViewExtras2 version 2.41? i cant find the link...

WebViewExtras (original version) was updated to version 2.41 and can be downloaded from first post in this thread.

WebViewExtras2 latest version is 2.20 and available from here.

There is no WebViewExtras2 version 2.41.
 

eps

Expert
Licensed User
Longtime User
I've been using the excellent WebViewExtras2 library to great effect, thanks for providing this.

I've come across an issue - although to be honest it might not be solveable or be worth spending a lot of time on..

I have been using a current Android device (HTC 10) to work with babylonjs which uses webgl to display 3D graphics in HTML5..

This works as expected.

My current device has developed a problem - it's going to HTC to be fixed. I've resurrected an old device of mine and wanted to continue developing - or rather confirming that the HTML5 'app' works as expected on a smartphone.. e.g. an HTC Evo 3D (running... Android 4.0.3). Now this does seem to have some webgl capabilities, which I've confirmed using html5test.com.. but if I go to that website using a ChromeClient via WebViewExtras2 it doesn't have webgl enabled or permitted - which is really frustrating! I was just wondering if there was any way of attempting to expand the webgl capabilities of WebViewExtras2 or if anyone had an inkling as to why it works on the HTC 10 but not on the Evo 3D. Of course I could just be pushing the realms too far - but when I used a CustomChromeTab this had webgl enabled on the Evo 3D - so something in the library seems to be setting webgl to disabled... admittedly 4.0.3 is pushing it. But then in the CustomChromeTab I can't load a local html file... which is what I've been doing in WebViewExtras2.

Anyone able to shed any light on this please? or shall I just wait until the newer phone is returned?
 

An Schi

Well-Known Member
Licensed User
I don't know if that helps or is connected to your problem...
In one of the android 4.x versions (4.1 or 4.4?) the webview was improoved and features were added. Take a look at the android documentation for more detailed infos.
 

smallqiang

New Member
Licensed User
WebViewExtras (original version) was updated to version 2.41 and can be downloaded from first post in this thread.

WebViewExtras2 latest version is 2.20 and available from here.

There is no WebViewExtras2 version 2.41.

Hello warwound, thank you very much for writing webviewextras2, the best b4a WebView extension. In using of this lib, I found below the SDK 23 , onReceivedError is contain a bug, there is no way to obtain 404 error. In order to solve this problem, Google added a onReceivedHttpError event to solve the problem in SDK23. Can you update the trouble of libraries, making it more perfect? Thank you for sharing
 

An Schi

Well-Known Member
Licensed User
I wanted to use the _PageStarted Sub from the DefaultWebViewClient. But the app allways crashed, when i clicked on a link with the error:
java.lang.Exception: Sub def_pagestarted signature does not match expected signature.

Erel suggested to replace

B4X:
Sub Def_PageStarted(Url As String, FavIcon As Bitmap)
    Log("PageStarted URL = " & Url)
End Sub

with

B4X:
Sub Def_PageStarted(Url As String, FavIconO As Object)
    Dim FacIcon As Bitmap = FavIconO
End Sub

Now the app doesn't crash anymore.

Here you can see the whole thread: https://b4x.com/android/forum/threads/solved-pagestarted-signature-error.83126/#post-526484
 

Shay

Well-Known Member
Licensed User
Longtime User
I am trying to autoplay this radio station
on windows it will autoplay, but in webview it is not
I am trying:

B4X:
station="http://glz.co.il/%D7%92%D7%9C%D7%92%D7%9C%D7%A6"
MyWebViewExtras.JavaScriptEnabled = True
MyWebViewExtras.LoadUrl(station)
... after page is loaded
MyWebViewExtras.executeJavascript("javascript:void(0);")

it is not helping
(using WebViewExtra2 2.2)

also tried without luck:
MyWebViewExtras.executeJavascript("void(0);")
 
Last edited:

Shay

Well-Known Member
Licensed User
Longtime User
On the web page, the button to start the live streaming is only sending this void command
this is the button element:
upload_2017-9-2_18-27-46.png
 

Attachments

  • upload_2017-9-2_18-27-7.png
    upload_2017-9-2_18-27-7.png
    297.5 KB · Views: 237

Leni Berry

Active Member
Licensed User
Longtime User
How to Get the source code of a webpage with WebView and WebViewExtras2 not WebViewExtras?

Please Help
 
Top