1

I want to get all text in body tag of a url but it doesn't work. I have searched many but i could not find anything. I have also added android.permission.INTERNET also.

so what is the problem?

This is my code:

public class Activity_Main  extends Activity {

@SuppressLint("JavascriptInterface")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final WebView webView = (WebView) findViewById(R.id.webView);

    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setDomStorageEnabled(true);

    final TextView contentView = (TextView) findViewById(R.id.contentView);

    class MyJavaScriptInterface
    {
        private TextView contentView;

        public MyJavaScriptInterface(TextView aContentView)
        {
            contentView = aContentView;
        }

        @SuppressWarnings("unused")

        public void processContent(String aContent)
        {
            final String content = aContent;
            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)
        {
            webView.loadUrl("javascript:window.INTERFACE.processContent(document.getElementsByTagName('body')[0].innerText);");
        }
    });

    webView.loadUrl("https://stackoverflow.com");
  }
} 
Ali Sheikhpour
  • 10,475
  • 5
  • 41
  • 82

2 Answers2

0

update @JavascriptInterface for your interface function and check

class MyJavaScriptInterface
    {
        private TextView contentView;

        public MyJavaScriptInterface(TextView aContentView)
        {
            contentView = aContentView;
        }

        @JavascriptInterface
        public void processContent(String aContent)
        {
            final String content = aContent;
            contentView.setText(content);
        }
    }

and there is another way to do this is this for above KITKAT

 webView.evaluateJavascript("alert('pass here some ...')", new ValueCallback<String>() {
       @Override
       public void onReceiveValue(String s) {

        }
    });

this is update solution for executing js in android

full code is here

final WebView webView = (WebView) findViewById(R.id.webView);

    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setDomStorageEnabled(true);

    final TextView contentView = (TextView) findViewById(R.id.contentView);

    class MyJavaScriptInterface
    {
        private TextView contentView;

        public MyJavaScriptInterface(TextView aContentView)
        {
            contentView = aContentView;
        }

        @JavascriptInterface
        public void processContent(String aContent)
        {
            final String content = aContent;
            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)
        {
            super.onPageFinished(view,url);


            if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.KITKAT) {
                webView.evaluateJavascript("document.getElementsByTagName('body')[0].innerText", new ValueCallback<String>() {
                    @Override
                    public void onReceiveValue(String s) {
                        contentView.setText(s);
                    }
                });
            }
            else {
                webView.loadUrl("javascript:window.INTERFACE.processContent(document.getElementsByTagName('body')[0].innerText)");

            }


                       }
    });

    webView.loadUrl("https://stackoverflow.com");
Damodhar
  • 1,219
  • 8
  • 17
0

To call a JavaScript function from Android you don't need to add interface prefix:

webView.loadUrl("javascript:processContent(document.getElementsByTagName('body')[0].innerText);");

Note: You must implement the called javascript function in your HTML page. So If you are calling a third party website in your webview, you have only access to the current existing functions in that page.

e.g https://stackoverflow.com has no javascript function named INTERFACE.processContent!

On the contrary when you want to call and Android method from javascript then you need that prefix:

<script>INTERFACE.myAndroidMethod()</script>

Finally if you want to proccess the HTML content of a thirdparty webpage you can not extract the content using a Webview and JavaScript function (that not exists in that page) but you need Android codes to load the webpage content without the WebView mediation.

Ali Sheikhpour
  • 10,475
  • 5
  • 41
  • 82