Get Webview page title?

Jack Cole

Well-Known Member
Licensed User
Longtime User
I have an app where it is set to be able to share a URL using the following code:

B4X:
Dim i As Intent
i.Initialize(i.ACTION_SEND, "")
i.PutExtra("android.intent.extra.SUBJECT","Interesting post")
i.SetType("text/*")
i.PutExtra("android.intent.extra.TEXT", mainv.Url)
i.WrapAsIntentChooser("Choose")
StartActivity(i)

The code was courtesy of Erel in a previous post except for the part that adds the email subject, which I figured out how to do. What I would like to do is include the page title from the Webview, but don't see a way of doing that. Can that be done?

Thanks,
Jack
 

warwound

Expert
Licensed User
Longtime User
Hi.

There's no easy way to access the page title - the WebView itself has no methods of any use.

You can use my JSInterface library to send some javascript to the web page though.
That javascript can get the document.title property you want and send it back to a B4A Sub:

B4X:
'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 PageTitle As String
   Dim WebInterface As JSInterface
   Dim WebView1 As WebView
End Sub

Sub Activity_Create(FirstTime As Boolean)
   WebView1.Initialize("WebView1")
   
   WebInterface.addJSInterface(WebView1, "B4A")
   
   Activity.AddView(WebView1, 0, 0, -1, -1)
   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)
   Log("PageFinished: "&Url)
   WebInterface.execJS(WebView1, "B4A.CallSub('SavePageTitle', document.title)")
   Log("Page title requested")
End Sub

Sub SavePageTitle(title As String)
   PageTitle=title
   Log("The page title has been saved: "&title)
End Sub

That works in an asynchronous way - the B4A code waits for the page to load then asks the page to send it's title back to B4A.
You'd not be able to simply write code:

B4X:
Dim Title As String
Title=WebView1.DocumentTitle

So watch that the page title has been received in your B4A code before you try to use it.

Martin.
 
Upvote 0

hackhack

Active Member
Licensed User
Longtime User
Hi.

There's no easy way to access the page title - the WebView itself has no methods of any use.

What? I thought erel said the you had access to everything in the webview?

Ie, if they navigate to a sites oauth pages, they could read anything from the webview?? If you can read input fields on a webpage, you'd think we can read the title.
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
What? I thought erel said the you had access to everything in the webview?

Ie, if they navigate to a sites oauth pages, they could read anything from the webview?? If you can read input fields on a webpage, you'd think we can read the title.

Take a look at my post#14 and example code in this thread:

http://www.b4x.com/forum/basic4andr.../9400-save-webview-html-file-2.html#post56406

You can execute arbitary javascript in any web page - secure page or not - the javascript will always be able to capture any parts of the web page that you want.

Martin.
 
Upvote 0

Neojoy

Member
Licensed User
Longtime User
Hi.

There's no easy way to access the page title - the WebView itself has no methods of any use.

You can use my JSInterface library to send some javascript to the web page though.
That javascript can get the document.title property you want and send it back to a B4A Sub:

B4X:
'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 PageTitle As String
   Dim WebInterface As JSInterface
   Dim WebView1 As WebView
End Sub

Sub Activity_Create(FirstTime As Boolean)
   WebView1.Initialize("WebView1")
   
   WebInterface.addJSInterface(WebView1, "B4A")
   
   Activity.AddView(WebView1, 0, 0, -1, -1)
   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)
   Log("PageFinished: "&Url)
   WebInterface.execJS(WebView1, "B4A.CallSub('SavePageTitle', document.title)")
   Log("Page title requested")
End Sub

Sub SavePageTitle(title As String)
   PageTitle=title
   Log("The page title has been saved: "&title)
End Sub

That works in an asynchronous way - the B4A code waits for the page to load then asks the page to send it's title back to B4A.
You'd not be able to simply write code:

B4X:
Dim Title As String
Title=WebView1.DocumentTitle

So watch that the page title has been received in your B4A code before you try to use it.

Martin.

Please, could you help me, I can´t find "JSInterface" library, I already downloded webviewextras library, but is not there
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
JSInterface is an old library now replaced with WebViewExtras.

Here's the example modified to use WebViewExtras:

B4X:
Sub Process_Globals

End Sub

Sub Globals
   Dim LastLoadedUrl As String
   Dim WebView1 As WebView
   Dim WebViewExtras1 As WebViewExtras
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
   WebView1.Initialize("WebView1")
   
   WebViewExtras1.addJavascriptInterface(WebView1, "B4A")
   
   Activity.AddView(WebView1, 0, 0, 100%x, 100%y)
   
   WebView1.LoadUrl("http://www.b4x.com/forum/basic4android-updates-questions/10670-get-webview-page-title.html#post180445")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub JavascriptCallback(String1 As String)
   Log("JavascriptCallback: "&LastLoadedUrl&" : "&String1)
End Sub

Sub WebView1_PageFinished (Url As String)
    Log("PageFinished: "&Url)
   LastLoadedUrl=Url
    WebViewExtras1.executeJavascript(WebView1, "B4A.CallSub('JavascriptCallback', true, document.title)")
    Log("Page title requested")
End Sub

Martin.
 

Attachments

  • GetWebViewTitle.zip
    6 KB · Views: 317
Upvote 0
Top