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.basic4ppc.com/forum/addi...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.basic4ppc.com/forum/addi...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
 

Attachments

  • WebViewExtras_v1_42.zip
    7.8 KB · Views: 6,331
Last edited by a moderator:

eps

Expert
Licensed 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
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:

DonManfred

Expert
Licensed User
Both javascript commands are not doing anything...
What doyou expect to happen?
 

Shay

Well-Known Member
Licensed 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: 67

Leni Berry

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

Please Help
 

JohnC

Expert
Licensed User
OK, I found a bug, don't know if its a WVE bug or it's another example of why you can't use MsgBox2 in an event sub.

(This bug seems like it might be related to another bug I found: https://www.b4x.com/android/forum/threads/weird-webview-and-msgbox2-conflict.83602/#post-529656)

Basically,

If I use this code on the first page of this thread:

B4X:
Sub MyEventName_GeolocationPermissionsRequest As Int
   Dim Response As Int
   Response=Msgbox2("Allow the webpage to use device geolocation features?", "Permission required:", "Allow", "", "Disallow", Null)
   If Response=DialogResponse.POSITIVE Then
      ToastMessageShow("Permission granted", True)
      Return MyWebViewExtras.GEOLOCATION_PERMISSION_ALLOW
   Else
      ToastMessageShow("Permission denied", True)
      Return MyWebViewExtras.GEOLOCATION_PERMISSION_DISALLOW
   End If
End Sub

After I click "Allow", the webpage seems like it can NOT run javascript reliably on it anymore.

However, if I simply have:
B4X:
Return wve.GEOLOCATION_PERMISSION_ALLOW
In the above sub, then the webpage acts correctly after I click Allow.
 

John Woodsmall

Active Member
Licensed User
i have simple html page (that works in desktop crome). It saves the input varibles
and then later wants to retrieve them.

does not seem to work under /files ???

var equ = document.getElementById('Equip_Mod').value ;
window.localStorage.setItem("Equip_Mod",equ);
--
var wag = document.getElementById('Cust_name') ;
wag.value= window.localStorage.getItem("Cust_name");
 

magarcan

Active Member
Licensed User
Hi!!

I'm working from your example here.
The fact is WebView1_PageFinished Event is triggered before the site has been fully loaded. I've set un a Delay before executing the Javascript and it works, but don't think this is the best way.

Any idea?

Thanks!!!
 

warwound

Expert
Licensed User
The fact is WebView1_PageFinished Event is triggered before the site has been fully loaded. I've set un a Delay before executing the Javascript and it works, but don't think this is the best way.

Any idea?

PageFinished means that all resources on the webpage have been loaded from the server, it does not mean that those resources have been fully rendered within the WebView.

Do you have control over the webpage?
If so then you can add a javascript event listener that will execute when the webpage has been fully rendered and that javascript can call a b4a sub via a JavascriptInterface.
Look here for more info: https://www.w3schools.com/jsref/event_onload.asp

If you have no control over the webpage then the only thing i can think of is to inject some javascript into the webpage and that injected javascript creates an onload event listener and calls a b4a sub via a JavascriptInterface.
 

Derek Johnson

Active Member
Licensed User
hi all

The following URL open without any issue in browser but not rendering with webview

http://ambiotinfotech.com/pos

login credentials:

demo / demo

tried with webviewextras but failed

need some support, Thanks in advance.

You should probably post this as a separate question, however I changed the URL in the WebView Extras demo code and got this on my Nexus 5X. What is the issue? (It was slow to load however).
 

Attachments

  • Pos.png
    Pos.png
    19.6 KB · Views: 111

Derek Johnson

Active Member
Licensed User
Srikanth,

I did spot this error after I logged in and it did take 10-20 seconds to finish loading after the page finished event in WebView.

** Activity (main) Create, isFirst = true **
After 00 sec Loading URL1
** Activity (main) Resume **
After 00.009 sec Resume
After 00.026 sec Removed cookies
After 00.049 sec OverideURL http://ambiotinfotech.com/pos/
After 00.79 sec OverideURL http://ambiotinfotech.com/pos/login
After 01.219 sec Page Finished http://ambiotinfotech.com/pos/login
After 33.112 sec OverideURL http://ambiotinfotech.com/pos/pos
Uncaught TypeError: Cannot read property 'getItem' of null in http://ambiotinfotech.com/pos/themes/default/assets/dist/js/scripts.min.js (Line: 1)
After 01.476 sec Page Finished http://ambiotinfotech.com/pos/pos
 
Top