B4A Library WebViewExtras

Derek Johnson

Active Member
Licensed User
Derek I need to put additional ?
Sorry missing a couple of quote marks in the Javascript. This code works now.

B4X:
Sub WebView1_PageFinished (Url As String)

'find  <span class="title">Latest Versions</span>
    Get1stElementByClass("title")
End Sub


Sub Get1stElementByClass(id As String)
Dim js As String=$"
    var elems = document.getElementsByClassName('${id}');
    if (elems.length>0) {
        B4A.CallSub('j_result', true, elems[0].innerText )
    } else {
        B4A.CallSub('j_result', true, 'No element found' )
    }
"$
    WebViewExtras1.executeJavascript(WebView1,js)
End Sub

Sub j_result(result As String)
   Log("Result was " & result )
    EditText1.text= result
End Sub
 

air cover

Member
Licensed User
ProcessHTML checks if the page is properly displaying one of two webpages, if not, it starts yet another Timer2 to try to reload the page again quickly and maybe fiddle with the wifi. My problem is that eventually _PageFinished is not called so ProcessHTML never gets called, even though Timer3 IS being called and is supposed to be reloading WebView1 every few minutes.

Sub ProcessHTML(FlingableWebView1 As FlingableWebView, Html As String)
If Html.Contains(ACTIVE_STATE_STRING) = True Then
Timer1.Enabled = False
Else If Html.Contains(INACTIVE_STATE_STRING) = True Then
If Timer1.Enabled = False Then
Timer1.Enabled = True 'this timer does a JavaScript Click() on the page in about an hour...
End If
Else
Log("PAGE ERROR")
Timer2.Enabled=True 'this timer tries to refresh the webpage in ~10 seconds and also fiddles with the wifi
End If
End Sub
Would you please post your Timer1 code for reference?
 

marcel

Active Member
Licensed User
I get with some URL's a exception.

B4X:
java.lang.NullPointerException
    at uk.co.martinpearman.b4a.webviewextras.WebViewExtras$1.onGeolocationPermissionsShowPrompt(WebViewExtras.java:168)
    at com.android.webview.chromium.WebViewContentsClientAdapter.onGeolocationPermissionsShowPrompt(WebViewContentsClientAdapter.java:553)
    at com.android.org.chromium.android_webview.AwContents.onGeolocationPermissionsShowPrompt(AwContents.java:1848)
    at com.android.org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
    at com.android.org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:24)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5139)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
    at dalvik.system.NativeStart.main(Native Method)
Fatal signal 6 (SIGABRT) at 0x00000fa0 (code=-6), thread 4000 (.yazula.android)
I am using from webextras:

B4X:
MyWebViewExtras.addJavascriptInterface(WebView1,"B4A")
MyWebViewExtras.addWebChromeClient(WebView1, "")
If I remove the line:
MyWebViewExtras.addWebChromeClient(WebView1, "")

Then I have no problems anymore
 

marcel

Active Member
Licensed User
I get with some URL's a exception.

B4X:
java.lang.NullPointerException
    at uk.co.martinpearman.b4a.webviewextras.WebViewExtras$1.onGeolocationPermissionsShowPrompt(WebViewExtras.java:168)
    at com.android.webview.chromium.WebViewContentsClientAdapter.onGeolocationPermissionsShowPrompt(WebViewContentsClientAdapter.java:553)
    at com.android.org.chromium.android_webview.AwContents.onGeolocationPermissionsShowPrompt(AwContents.java:1848)
    at com.android.org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
    at com.android.org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:24)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5139)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
    at dalvik.system.NativeStart.main(Native Method)
Fatal signal 6 (SIGABRT) at 0x00000fa0 (code=-6), thread 4000 (.yazula.android)
I am using from webextras:

B4X:
MyWebViewExtras.addJavascriptInterface(WebView1,"B4A")
MyWebViewExtras.addWebChromeClient(WebView1, "MyEventName")
If I remove the line:
MyWebViewExtras.addWebChromeClient(WebView1, "MyEventName")

Then I have no problems anymore
Add geolocation function:

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
This fixes the issue.
 

magi6162

Well-Known Member
Licensed User
B4A code:
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Dim MyWebViewExtras As WebViewExtras
    Dim JavascriptInterface1 As DefaultJavascriptInterface
    Dim wvMap As WebView
    Dim bdlg As BetterDialogs

    Dim miapos As String = "ND"
    Dim orig As String = "Interessato"
    Dim ss As String = ""
    Dim sb As StringBuilder
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Dim sf As String
    Dim fs As MLfiles

    Activity.LoadLayout("MappaInteressato")

    sf = File.DirDefaultExternal & "/googlemap.html"
    sb.Initialize
    sb.Append(fs.ReadTxtFile(sf))
    MyWebViewExtras.Initialize(wvMap)
    JavascriptInterface1.Initialize
    MyWebViewExtras.addJavascriptInterface(JavascriptInterface1, "B4A")
    wvMap.LoadHtml(sb.ToString)
End Sub

Sub wvMap_PageFinished (Url As String)
    miapos = Main.LatMia & ":" & Main.LonMia
    If miapos = "0:0" Then
        miapos = NDCode.SALA_Lat & ":" & NDCode.SALA_Lon
    End If
    MyWebViewExtras.executeJavascript("place_marker('" & ss & "','" & miapos & "','" & orig & "')")
End Sub

Sub ChangeCoordPersona(scoord As String)
    Dim newLat As String = scoord.SubString2(0,scoord.IndexOf(":"))
    Dim newLon As String = scoord.SubString(scoord.IndexOf(":"))
    If bdlg.Msgbox("<b><h1>NoteDroid</h1></b>","<b><i>Accetti le nuove coordinate?</i></b>","Sì","","No",LoadBitmap(File.DirAssets,"a_invoice_256.png")) = DialogResponse.POSITIVE Then
        Main.PND.ExecNonQuery("UPDATE Interessati SET Lat='" & newLat & "',Lon='" & newLon & "' WHERE IDI=" & Interessati.mIDI)
    End If
End Sub
JS code: (this function call on dragend of marker)
B4X:
   var startmarker = new google.maps.Marker({
       position : latLng,
       map : map,
       draggable: true,
       icon : icon1,
       title : Nome
     });

  infowindow = new google.maps.InfoWindow({
  content: contentString
  });

  google.maps.event.addListener(startmarker, "click", function() {
       infowindow.open(map,startmarker);
  });

  google.maps.event.addListener(startmarker, "dragend", function() {
       var lat = startmarker.position.lat();
       var lon = startmarker.position.lng();
       var scoord = lat + ":" + lon;
       changeCoordPersona(scoord);
  });


function changeCoordPersona(scoord) {
        B4A.CallSub("ChangeCoordPersona",true,scoord);
    }
but the B4a function (ChangeCoordPersona) don't execute.
I use webview extra 2
help me, please
 
Last edited:

warwound

Expert
Licensed User
You see your 'startmarker' Marker on the map do you and it is draggable?

If you update your changeCoordPersona function to this:

B4X:
function changeCoordPersona(scoord) {
    console.log("changeCoordPersona called");
        B4A.CallSub("ChangeCoordPersona",true,scoord);
}
Now look in the b4a log when you drag your Marker - do you see 'changeCoordPersona'?
 

magi6162

Well-Known Member
Licensed User
In debug mode don't see log, but messagebox yes and change coordinate.
In release mode don't execute function ChangeCoordPersona (b4a)

i see the marker and is draggable.
but coords wrong if in debug mode.
 
Last edited:

warwound

Expert
Licensed User
Are you compiling using obfuscated mode?
If so change the sub name - add an underecore somewhere in it's name.
For example: change_CoordPersona.
That'll prevent the sub name from being obfuscated - if it's obfuscated then the javascript is calling a sub that doesn't exist.
 

magi6162

Well-Known Member
Licensed User
yes i compiling using obfuscated mode.
I'll try it
thanks

PS.
the code [console.log("xxxxxxxxxx")] as it works ? i don't see nothing in log.
 

warwound

Expert
Licensed User
PS.
the code [console.log("xxxxxxxxxx")] as it works ? i don't see nothing in log.
Depends on which version of WebViewExtras you're using.
The newest version doesn't automatically log the console out whereas the older versions did.

There's a code example HERE that shows how to handle console messages in the newer versions of WebViewExtras.
 

Siam

Active Member
Licensed User
Hello,
I dont give up :p:D
by coincidence I found out that the file upload with Android 5.0 works!!! after several hours i have found the solution.

Add in the Manifest.xml :

B4X:
 android:targetSdkVersion="20"

z.b.
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="20"/>
With this simple modification works The FileUpload with The Samsung galaxy S5 with Android 5.0.0

B4X:
Touch event's action is 0x1 (deviceType=0) [pCnt=1, s=] when=420713087824000
Delivering touch to (30257): action: 0x1, toolType: 1
openFileChooser Jelly Bean
AcceptType: */*
Capture:

If The targetsdkVersion is bigger than 20 following Happens without Firering the FileChooser.

B4X:
Input event: value=1
Touch event's action is 0x0 (deviceType=0) [pCnt=1, s=0.19698 ] when=420716498963000
Delivering touch to (30257): action: 0x0, toolType: 1

or:

Input event: value=0
Touch event's action is 0x1 (deviceType=0) [pCnt=1, s=] when=420265132717000
Delivering touch to (30257): action: 0x1, toolType: 1
I hope that this helps a littlebit to get in Future a Library that works with android 5 and bigger. :rolleyes:

For the moment i think is ok :)



Andy
 

warwound

Expert
Licensed User
WebViewExtras updated to version 1.42

The WebChromeClient currently raises the event:
ProgressChanged (NewProgress As Int)

This update adds a new event:
ProgressChanged2 (WebView1 As WebView, NewProgress As Int)

Much like the existing ProcessedChanged event but the event sub is also passed a reference to the WebView.

Version 1.42 of WebViewExtras is attached to the first post of this thread.
 

warwound

Expert
Licensed User
How to get the webview scrolling top value? like getScrollY
The getScrollY method isn't exposed by WebViewExtras.
You should be able to use JavaObject to call this method instead:

B4X:
Dim JavaObject1 As JavaObject=MyWebView
Dim ScrollY As Int=JavaObject1.RunMethod("getScrollY", Null)
 

fash866

Member
Licensed User
The getScrollY method isn't exposed by WebViewExtras.
You should be able to use JavaObject to call this method instead:

B4X:
Dim JavaObject1 As JavaObject=MyWebView
Dim ScrollY As Int=JavaObject1.RunMethod("getScrollY", Null)
thanks!
 
Top