Wish Javascript

inakigarm

Well-Known Member
Licensed User
Longtime User
Thanks Erel; I attached a simple example

Most searched info return that stringByEvaluatingJavaScriptFromString: must called in webViewDidFinishLoad, so it's called in WebView_PageFinished

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public App As Application
    Public NavControl As NavigationController
    Private Page1 As Page
    Private toolbar1 As Page
    Private WebView1 As WebView

End Sub

Private Sub Application_Start (Nav As NavigationController)
    NavControl = Nav
    Page1.Initialize("Page1")
    Page1.Title = "Page 1"
    Page1.RootPanel.LoadLayout("provawebview")
    NavControl.ShowPage(Page1)
    NavControl.ToolBarVisible=True

    Dim htmlpage As String
    htmlpage="<!DOCTYPE html>"
    htmlpage=htmlpage&"<html>"
    htmlpage=htmlpage&"<body>"
    htmlpage=htmlpage&"<script>function myFunction() {document.getElementById('demo').innerHTML = 'Paragraph changed';}</script>"
    htmlpage=htmlpage&"<h1>My First Web Page</h1>"
    htmlpage=htmlpage&"<p id='demo'>A Paragraph.</p>"
    htmlpage=htmlpage&"<Button Type='Button' onclick='myFunction()'>Try it</Button>"
    
    htmlpage=htmlpage&"</body>"
    htmlpage=htmlpage&"</html>"

    Log (htmlpage)

    WebView1.LoadHtml(htmlpage)

    
End Sub


Sub WebView1_PageFinished (Success As Boolean, Url As String)    

    Log ("Page Loaded")

    Dim ww As NativeObject=Webview1
    Dim scripttext As String
    scripttext="myfunction();"
    Log (scripttext)
    ww.RunMethod("stringByEvaluatingJavaScriptFromString:",Array(scripttext))
End Sub
 

GiulioVale

Active Member
Licensed User
Longtime User
Uhm sorry again but with

B4X:
 Dim Javascript As String
       Javascript="B4A.CallSub('ProcessHTML', true, tit('title') )"
       Log("PageFinished: "&Javascript)
       WebViewExtras1.executeJavascript(browser, Javascript)

can call ProcessHTML inside b4A.

For B4i the same code is ?
 

GiulioVale

Active Member
Licensed User
Longtime User
No.
Do you want to get the html?

Try:
B4X:
Dim html As String =  ww.RunMethod("stringByEvaluatingJavaScriptFromString:",Array("document.documentElement.innerHTML"))


No Erel I want back the result of this javascript function:
B4X:
function tit( classe_titoli ){
    var elements = document.getElementsByClassName( classe_titoli );
    var t="";
    for(var i = 0, length = elements.length; i < length; i++) {
       t = t + elements[i].innerText + "|";
    }
    return t;
  }
 

GiulioVale

Active Member
Licensed User
Longtime User
Yes Erel.
Every page have this function inside (with B4A I can call it and have back the index of a document based on some css class).

A different solution is to have back all the innerHTML and parse this but i prefer first solution.
 

ilan

Expert
Licensed User
Longtime User
how can i get the text as a string from the webview after page finished loading??

is there something like Webview1.DocumentText?
 

GiulioVale

Active Member
Licensed User
Longtime User
how can i get the text as a string from the webview after page finished loading??

is there something like Webview1.DocumentText?
Try
Dim html AsString = ww.RunMethod("stringByEvaluatingJavaScriptFromString:",Array("document.documentElement.innerText"))
 

ilan

Expert
Licensed User
Longtime User
thank you very much it worked like this
but i got an error when i try to load this site to my webview

WebView1.LoadUrl("http://query.yahooapis.com/v1/public/yql?q=select * from yahoo.finance.xchange where pair in (" & """" & "USDILS" & """" & ")&env=store://datatables.org/alltableswithkeys")

error:
*** WebKit discarded an uncaught exception in the webView:decidePolicyForNavigationAction:request:frame:decisionListener: delegate: <NSInvalidArgumentException> *** -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]

why i cannot load this page??
 
Top