B4A Library WebViewExtras

Discussion in 'Additional libraries, classes and official updates' started by warwound, Nov 7, 2011.

  1. Derek Johnson

    Derek Johnson Active Member Licensed User

    Croid - There is a minor syntax error in my original post. (The 'if' statement had a capital 'I' ). Please use the post as it is now.
     
  2. Croïd

    Croïd Active Member Licensed User

    Derek I need to put additional ?
     

    Attached Files:

  3. Derek Johnson

    Derek Johnson Active Member Licensed User

    Sorry missing a couple of quote marks in the Javascript. This code works now.

    Code:
    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
     
    laomms, TWCoder and Croïd like this.
  4. air cover

    air cover Member Licensed User

    Would you please post your Timer1 code for reference?
     
  5. marcel

    marcel Active Member Licensed User

    I get with some URL's a exception.

    Code:
    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:

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

    Then I have no problems anymore
     
  6. marcel

    marcel Active Member Licensed User

    Add geolocation function:

    Code:
    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.
     
  7. magi6162

    magi6162 Well-Known Member Licensed User

    B4A code:
    Code:
    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)
    Code:
    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: Aug 23, 2015
  8. warwound

    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:

    Code:
    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'?
     
  9. magi6162

    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: Aug 23, 2015
  10. warwound

    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.
     
  11. magi6162

    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.
     
  12. warwound

    warwound Expert Licensed User

    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.
     
  13. magi6162

    magi6162 Well-Known Member Licensed User

    Ok, all solved.

    thanks, best regards
     
  14. fash866

    fash866 Member Licensed User

    How to get the document.body.scrollTop?
     
  15. warwound

    warwound Expert Licensed User

    What have you tried so far?
    You'll have to wait for the webpage to fully load before you can access that property i think.
     
  16. Siam

    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 :

    Code:
    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

    Code:
    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.

    Code:
    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
     
  17. warwound

    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.
     
  18. fash866

    fash866 Member Licensed User

    How to get the webview scrolling top value? like getScrollY
     
  19. warwound

    warwound Expert Licensed User

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

    Code:
    Dim JavaObject1 As JavaObject=MyWebView
    Dim ScrollY As Int=JavaObject1.RunMethod("getScrollY"Null)
     
    fash866 likes this.
  20. fash866

    fash866 Member Licensed User

    thanks!
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice