0

I am making an application which uses Android WebView. This application displays page title and page description of the URL that is loaded. As a sample I am using following url

http://in.news.yahoo.com/ca-chief-slams-racist-comments-over-fawad-ahmeds-071749766.html?.tsrc=yahoo

Getting page title is easy

public void onPageFinished (WebView view, String url) {
    String title = view.getTitle();
}

If you do a view source of the above url in Chrome, this title is fetched from

<title>CA chief slams `racist` comments over Fawad Ahmed's `beer-branded kit` refusal - Yahoo! News India</title>

Now, I need to get meta name "description", so as to show page description, which is as given below

<meta name="description" lang="en-IN" content="'CA chief slams `racist` comments over Fawad Ahmed's `beer-branded kit` refusal' on Yahoo! News India. Islamabad, Sept 5 (ANI): Cricket Australia (CA) chief James Sutherland has slammed 'racist comments' aimed at Pakistan-born Australian leg-spinner Fawad Ahmed following his refusal to wear a kit displaying the logo of beer brand VB due to 'religious reasons'."/>

Android web view doesn't have an API to get description from meta tag "description".

How is it possible to get meta tags from document element?

Faheem Kalsekar
  • 1,420
  • 3
  • 25
  • 31

4 Answers4

1

You can solve the problem by this way:

private class JsInterface {
    @JavascriptInterface
    @SuppressWarnings("unused")
    public void processHTML(String content) {
        //handle content
    }
}


mWebView.addJavascriptInterface(new JsInterface(), "CC_FUND");

mWebView.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            mWebView.loadUrl("javascript:window.CC_FUND.processHTML( (function (){var metas = document.getElementsByTagName('meta'); \n" +
                    "\n" +
                    "   for (var i=0; i<metas.length; i++) { \n" +
                    "      if (metas[i].getAttribute(\"name\") == \"description\") { \n" +
                    "         return metas[i].getAttribute(\"content\"); \n" +
                    "      } \n" +
                    "   } \n" +
                    "\n" +
                    "    return \"\";})() );");
            super.onPageFinished(view, url);
        }
}
wang shi
  • 39
  • 1
0

I think what you are looking for is String.contains("meta name=\"description\"").

That would be the easiest method anyway. You could probably piece together a result from the code in how to get html content from a webview?

Another way would be to use the WebView.addJavascriptInterface() to insert the code from murala's answer, but I'm not sure how you would retrieve the result. Use my first idea.

Community
  • 1
  • 1
James Fenn
  • 109
  • 1
  • 14
0

Kotlin code: if you use evaluateJavascript then not need to define javascriptInterface.

call evaluateJavascript then use ValueCallback<String> resultCallback callback value.

                webview.evaluateJavascript(
                        """
                            function getOGPImageUrl(){
                                var metas = document.getElementsByTagName('meta');
                                for (var i=0; i<metas.length; i++) { 
                                    if (metas[i].getAttribute("property") == "og:image") {
                                        return metas[i].getAttribute("content");
                                    }
                                }
                                return "";
                            };
                            getOGPImageUrl();
                        """.trimIndent()) {
                    // this is ValueCallback block
                    Logger.d("getOGPImageUrl: $it")
                }
changhao cui
  • 469
  • 4
  • 7
-1

document.getElementsByName("title"); returns a set of elements not a single element so within a cycle you could use element.tagName to get the tag

basicly document.getElementsByName("title")[0].tagName should work

(or) Just use something like :

var author = $('meta[name=author]').attr("content");
murala
  • 39
  • 5