Android Example Retrieve HTML content from WebView using WebViewExtras library

Hello,
The code example below retrieves HTML content from WebView via three different techniques: 1.) MenuItem 2.) LongClick 3.) PageFinished. The code calls addJavascriptInterface() & executeJavascript() contained in WebViewExtras library V1.40 to accomplish this task.

For more background see http://www.b4x.com/android/forum/threads/save-webview-html-file.9400 and http://www.b4x.com/android/forum/threads/how-can-i-get-the-content-of-a-webview.11015/

'HUGE HATS OFF' to both Martin (Warwound) and Eriel for making WebViewExtras and WebView respectively available to the rest of us. Both of you have my gratitude.

Thanks guys,
Gregg

B4X:
'Activity module
Sub Process_Globals
End Sub
Sub Globals
    Dim myInterface As WebViewExtras 'Requires WebViewExtras library
    Dim WebView1 As WebView
End Sub
Sub Activity_Create(FirstTime As Boolean)
    Activity.AddMenuItem("Save webpage", "MenuItem")
    Activity.LoadLayout("layoutMain") 'This layout contains a single WebView view
    myInterface.addJavascriptInterface(WebView1, "B4A")
    WebView1.LoadUrl("http://www.b4x.com/android/help/jsinterface.html")
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub MenuItem_Click
  Dim jsStatement As String = Javascript="B4A.CallSub('processHTML', true, document.documentElement.outerHTML)"
  myInterface.executeJavascript(WebView1, jsStatement)
End Sub
Sub WebView1_LongClick
    Dim jsStatement As String = "B4A.CallSub('processHTML', true, document.documentElement.outerHTML)"
    myInterface.executeJavascript(WebView1, jsStatement)
End Sub
Sub WebView1_PageFinished (Url As String)
    Dim jsStatement As String = "B4A.CallSub('processHTML', true, document.documentElement.outerHTML)"
    myInterface.executeJavascript(WebView1, jsStatement)
End Sub
Sub processHTML(html As String)
    Log(html)
End Sub
 

Gregg Homan

Member
Licensed User
Longtime User
Sorry ... I made a typo above ... please replace above Sub MenuItem_Click with this code ... Gregg
B4X:
Sub MenuItem_Click
  Dim jsStatement As String ="B4A.CallSub('processHTML', true, document.documentElement.outerHTML)"
  myInterface.executeJavascript(WebView1, jsStatement)
End Sub
 

forestd

Member
Licensed User
Longtime User
How are you
Copy the code above, after the operation, without any response.No HTML in the log.
May be this
Dim jsStatement As String ="B4A.CallSub('processHTML', true, document.documentElement.outerHTML

I hope to guide
thank you
 
Top