Android Question How to use JavaScript to show only all texts in a webpage

GERSON PINTO

Member
Licensed User
Hello
I found in stackOverflow the following JS code ( I don't know if it works!!!!)

B4X:
<script>
  $(document).ready(function(){
   // This give return only texts inside the body tags
   console.log($("body").text());
    })
  </script>

My goal is to show only the text of a webpage in the webview
I am using webviewextras2

how can I implement this in javascript?

B4X:
WebView1.ExecuteJavascript(" ?????")

Can you help?
 

rraswisak

Active Member
Licensed User
Longtime User
you can set the script to document.body.textContent
B4X:
'sample to show the content
Dim js As String = "alert(document.body.textContent);"
Dim w as WebViewExtras

w.executeJavascript(WebView1, js)

Sorry, I don't know how to grab the content and use for further process in android side

Edited: see post #9
 
Last edited:
Upvote 0

GERSON PINTO

Member
Licensed User
you can set the script to document.body.textContent
B4X:
'sample to show the content
Dim js As String = "alert(document.body.textContent);"
Dim w as WebViewExtras

w.executeJavascript(WebView1, js)

Sorry, I don't know how to grab the content and use for further process in android side

nothing happens! maybe stringbuilder???
I attached the simple project in this post
 

Attachments

  • w2t.zip
    9.6 KB · Views: 215
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
you can change the script with this:
B4X:
Dim js As String = "var c = document.body.textContent; document.body.innerHTML = c;"

the result was unformatted

Is it possible to get the JavaScript variable c into a B4A string variable?
I tried with B4A.RunSub etc. but couldn't get that to work at all, despite many different attempts.

RBS
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Sure, i create small project in Code-Snipped section, please take a look

I hope it would be useful for everyone who interest.[/QUOTE]

Have tried your demo and it works, but my situation is quite different.
My WebView doesn't load a URL from file, but instead a Web URL.
I set some values on this Webpage and then need to get a value from
an element of this webpage. As I couldn't figure out how to just get this
value only I thought it might be easier to get the whole Html and then parse
that value out.
Do I understand right that I need to add your function doCallBack somehow to
the Html and then run that callback. If that is the case how do I had that function?

This is the website I am dealing with:
https://qrisk.org/three/

Thanks for the assistance.

RBS
 
Upvote 0

rraswisak

Active Member
Licensed User
Longtime User
@RBS since you don't specify what value you want to read from html element, let me assume that you want to grab the final score.

upload_2019-11-7_3-58-38.png


Getting value from element was easier when the element has "id" attribute, in your case the url above does not have any id's or name for element i want to read. so my approach is reading html structure where the score page is display.

The score result represented with table tag. Reading from page source code, the table-tag containing result exist at index 22. So try to change the project (base on my code-snipped) with this one:

B4X:
Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("1")
   we.addJavascriptInterface(WebView1,"B4A")
   'WebView1.LoadUrl(WebViewAssetFile("index.html"))
   WebView1.LoadUrl("https://qrisk.org/three/")
End Sub

Sub Button1_Click
   'we.executeJavascript(WebView1,"javascript: doCallBack();")
   we.addWebChromeClient(WebView1,"")
   Dim js As String = "B4A.CallSub('ShowResult',True,document.getElementsByTagName('table')[22].rows[0].cells[0].innerHTML)"
   we.executeJavascript(WebView1,js)
End Sub

'new sub
Sub ShowResult(result As String)
   Msgbox(result,"Result")
End Sub
 
Last edited:
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
@RBS since you don't specify what value you want to read from html element, let me assume that you want to grab the final score.

View attachment 85301

Getting value from element was easier when the element has "id" attribute, in your case the url above does not have any id's or name for element i want to read. so my approach is reading html structure where the score page is display.

The score result represented with table tag. Reading from page source code, the table-tag containing result exist at index 22. So try to change the project (base on my code-snipped) with this one:

B4X:
Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("1")
   we.addJavascriptInterface(WebView1,"B4A")
   'WebView1.LoadUrl(WebViewAssetFile("index.html"))
   WebView1.LoadUrl("https://qrisk.org/three/")
End Sub

Sub Button1_Click
   'we.executeJavascript(WebView1,"javascript: doCallBack();")
   we.addWebChromeClient(WebView1,"")
   Dim js As String = "B4A.CallSub('ShowResult',True,document.getElementsByTagName('table')[22].rows[0].cells[0].innerHTML)"
   we.executeJavascript(WebView1,js)
End Sub

'new sub
Sub ShowResult(result As String)
   Msgbox(result,"Result")
End Sub

Indeed, I am trying to get that final score, 12.3% in your image.

My code is like this:

In Activity_Create:

B4X:
 WebView1.JavaScriptEnabled = True 'I know not needed really as set in the .bal file
 WebView1.LoadUrl("https://qrisk.org/three/")
 WVExtras1.addJavascriptInterface(WebView1, "B4A")

In Main:

B4X:
Sub GetScore
WVExtras1.addWebChromeClient(WebView1,"") 'needed to error feedback from B4A.CallSub
' >>> tried with True instead of false and also without the semi-colon at the end
Dim js As String = "B4A.CallSub('ShowResult',false,document.getElementsByTagName('table')[22].rows[0].cells[0].innerHTML);"
WVExtras1.executeJavascript(WebView1, js)
End Sub

'new sub
Sub ShowResult(result As String)
Msgbox(result, "Result")
End Sub

GetScore I run from my menu when then Webview has the calculator page loaded, with all the parameters filled in and the sore calculated.

I get:
Uncaught ReferenceError: B4A is not defined in https://qrisk.org/three/index.php (Line: 1)

I also tried True for the second argument in B4A.CallSub and also tried without the semi-colon at the end of the js.

Any suggestions?

RBS
 
Upvote 0

rraswisak

Active Member
Licensed User
Longtime User
Works fine here, maybe error from somewhere else...
wve.gif


i modify the code so it will do click calculate button first and show the score
B4X:
Sub Button1_Click
   'we.executeJavascript(WebView1,"javascript: doCallBack();")
   we.executeJavascript(WebView1,$"document.getElementsByName("calculate")[0].click()"$)
   Wait For WebView1_PageFinished (Url As String)
   we.addWebChromeClient(WebView1,"")
   Dim js As String = "B4A.CallSub('ShowResult',true,document.getElementsByTagName('table')[22].rows[0].cells[0].innerHTML)"
   we.executeJavascript(WebView1,js)
End Sub
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Works fine here, maybe error from somewhere else...
View attachment 85304

i modify the code so it will do click calculate button first and show the score
B4X:
Sub Button1_Click
   'we.executeJavascript(WebView1,"javascript: doCallBack();")
   we.executeJavascript(WebView1,$"document.getElementsByName("calculate")[0].click()"$)
   Wait For WebView1_PageFinished (Url As String)
   we.addWebChromeClient(WebView1,"")
   Dim js As String = "B4A.CallSub('ShowResult',true,document.getElementsByTagName('table')[22].rows[0].cells[0].innerHTML)"
   we.executeJavascript(WebView1,js)
End Sub

Fixed this now.
Problem was that although WebView1 was set in the .bal file, it was initialized in Main.
This wasn't causing any problem before (but warning did show), but now it did.
So, all working perfectly fine and thanks for that!

RBS
 
Upvote 0
Top