Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android - get Text out of webview

In my application, I am showing epub HTML files in webview using EPUBLIB. My problem is that I want to use bookmark functionality for my epub reader. For that I want to fetch text from webview which is showing page from my epub's HTML file and then use that text in my bookmark activity to show the user what they have bookmarked. How can I achieve this?

like image 584
Rohit Avatar asked Mar 06 '12 07:03

Rohit


People also ask

How do I get messages from WebView?

HTML, CSS and JavaScript for Android WebView Depending on your requirements, you can fetch the contents of the WebView from the web using webView. loadUrl("<url>") method, or you can bind the code directly (e.g. after loading it from assets) using webView. loadDataWithBaseURL("", html, "text/html", "UTF-8", null) .

How to use JavaScript in Android WebView?

Enable JavaScript JavaScript is disabled in a WebView by default. You can enable it through the WebSettings attached to your WebView . You can retrieve WebSettings with getSettings() , then enable JavaScript with setJavaScriptEnabled() . WebView myWebView = (WebView) findViewById(R.

How WebView works?

Android System WebView lets applications display browser windows in an app instead of transporting the user to another browser. Android developers use WebView when they want to display webpages in a Google app or other application.


2 Answers

Getting the plain text content from a webview is rather hard. Basically, the android classes don't offer it, but javascript does, and Android offers a way for javascript to pass the information back to your code.

Before I go into the details, do note that if your html structure is simple, you might be better off just parsing the data manually.

That said, here is what you do:

  1. Enable javascript
  2. Add your own javascript interface class, to allow the javascript to communicate with your Android code
  3. Register your own webviewClient, overriding the onPageFinished to insert a bit of javascript
  4. In the javascript, acquire the element.innerText of the tag, and pass it to your javascript interface.

To clarify, I'll post a working (but very rough) code example below. It displays a webview on the top, and a textview with the text-based contents on the bottom.

package test.android.webview;  import android.app.Activity; import android.os.Bundle; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.TextView;  public class WebviewTest2Activity extends Activity {     /** Called when the activity is first created. */     @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);          WebView webView = (WebView) findViewById(R.id.webView);         TextView contentView = (TextView) findViewById(R.id.contentView);          /* An instance of this class will be registered as a JavaScript interface */          class MyJavaScriptInterface          {              private TextView contentView;              public MyJavaScriptInterface(TextView aContentView)             {                 contentView = aContentView;             }              @SuppressWarnings("unused")               public void processContent(String aContent)              {                  final String content = aContent;                 contentView.post(new Runnable()                  {                         public void run()                      {                                   contentView.setText(content);                             }                      });             }          }           webView.getSettings().setJavaScriptEnabled(true);          webView.addJavascriptInterface(new MyJavaScriptInterface(contentView), "INTERFACE");          webView.setWebViewClient(new WebViewClient() {              @Override              public void onPageFinished(WebView view, String url)              {                  view.loadUrl("javascript:window.INTERFACE.processContent(document.getElementsByTagName('body')[0].innerText);");              }          });           webView.loadUrl("http://shinyhammer.blogspot.com");     } } 

Using the following main.xml:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:orientation="vertical" >      <WebView         android:id="@+id/webView"         android:layout_width="match_parent"         android:layout_height="fill_parent"         android:layout_weight="0.5" />      <TextView         android:id="@+id/contentView"         android:layout_width="match_parent"         android:layout_height="fill_parent"         android:layout_weight="0.5" />   </LinearLayout> 
like image 181
Paul-Jan Avatar answered Sep 23 '22 10:09

Paul-Jan


Java:

    wvbrowser.evaluateJavascript(         "(function() { return ('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>'); })();",          new ValueCallback<String>() {             @Override             public void onReceiveValue(String html) {                 Log.d("HTML", html);                  // code here             }     }); 

Kotlin:

web_browser.evaluateJavascript("(function() { return ('<html>'+document.getElementsByTagName('span')[0].innerText+'</html>'); })();")  { html ->    Toast.makeText(this@Your_activity, html, Toast.LENGTH_SHORT).show()    // code here                 } 
like image 27
Balaji M Avatar answered Sep 24 '22 10:09

Balaji M