How can I get the content of a webview?

awama

Active Member
Licensed User
Longtime User
When I do a click on buttons for example in webview then another page ist showing. How can I get the content of the new page in the webview to a string or save to a file?

Many thanks for a solution
Walter
 

awama

Active Member
Licensed User
Longtime User
Hi Martin!

Before I post I tried your example, but I don't now how I have to use it.
Assumed I click for example on 3 web-links on the webview and every time it shows a new page in webview and I want to save every new page. What is to modify at your code example? Many thanks.
Walter

'Activity module
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.

End Sub

Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.

Dim myInterface As JSInterface
Dim WebView1 As WebView
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("layoutMain")
WebView1.Width=100%x
WebView1.Height=100%y

' add the B4A javascript interface to the WebView
myInterface.addJSInterface(WebView1, "B4A")

' now load a web page
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 WebView1_PageFinished (Url As String)
' Now that the web page has loaded we can get the page content as a String
Dim jsStatement As String

jsStatement="B4A.CallSub('processHTML', document.documentElement.outerHTML)"

Log("PageFinished: "&jsStatement)
myInterface.execJS(WebView1, jsStatement)
End Sub

Sub processHTML(html As String)
' This is the Sub that we'll get the web page to send it's HTML content to

' Log may truncate a large page so you'll not see all of the HTML in the log but the 'html' String should still contain all of the web page HTML

Log("processHTML: "&html)
End Sub
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Hi.

Try the attached code - i've commented it for you.

Martin.
 

Attachments

  • SaveWebViewContent.zip
    5.8 KB · Views: 693
Upvote 0

awama

Active Member
Licensed User
Longtime User
Hi.

Try the attached code - i've commented it for you.

Martin.

Hi,

I try it with JSInterface 1.2 , on my ZTE-Blade(V 2.1) and Galaxy S2 (V2.3.3) but the Sub's ...

Sub WebView1_PageFinished (Url As String)
Sub WebView1_SaveWebPage(Url As String, PageContent As String)

...are once only in action. I get only the first page. Where could be the problem or my mistake?
 
Upvote 0

awama

Active Member
Licensed User
Longtime User
Hi,

I try it with JSInterface 1.2 , on my ZTE-Blade(V 2.1) and Galaxy S2 (V2.3.3) but the Sub's ...

Sub WebView1_PageFinished (Url As String)
Sub WebView1_SaveWebPage(Url As String, PageContent As String)

...are once only in action. I get only the first page. Where could be the problem or my mistake?

:sign0013: Now it seems that the function is ok, only a msgbox in the Sub's is once only in action, however the string "PageContent" contents the page. Now I make a few tests and write the result. Many Thanks.
Walter
 
Upvote 0

awama

Active Member
Licensed User
Longtime User
Hi Martin,

your example works well if the Url is changed but if a website change the content because of - for example- a web query then the Sub's ...

Sub WebView1_PageFinished (Url As String)
Sub WebView1_SaveWebPage(Url As String, PageContent As String)

... are not called and consequently the new PageContent is not get / saved.

Is there in principle a solution for this problem?

Thanks. Walter
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Hi.

You could add a menu item to your activity:

B4X:
Activity.AddMenuItem("Save webpage", "SaveWebpage")

And then create a Sub to handle a click on the menu item:

B4X:
Sub SaveWebpage_Click
   Dim Javascript As String
   Javascript="B4A.CallSub('WebView1_SaveWebPage', '"&WebView1.Url&"', document.documentElement.outerHTML)"
   WebInterface.execJS(WebView1, Javascript)
End Sub

Now you can manually save a page instead of relying on the WebView1_PageFinished Sub.

Martin.
 
Upvote 0

awama

Active Member
Licensed User
Longtime User
Hi Martin,

for example your Code with 2 Url's which does not possible to save.
Have you the same "effect"?

Walter
 

Attachments

  • WebViewSave1.zip
    6.1 KB · Views: 445
Upvote 0

warwound

Expert
Licensed User
Longtime User
Hi.

Do you mean that Sub WebView1_SaveWebPage is going to save the webpage content with a filename or some id based upon the Sub's Url value?

You'll have a problem saving content when the Url has already been used to name a saved webpage?

Try appending the current time to the filename or id you use to save the webpage content.

Martin.
 
Upvote 0
Top