0

Currently I have this code where I want to retrieve a content of a webview and also at the same time remove display on some part of the webview but my code didn't do anything at all or showing any error so I'm at loss here. Currently, I'm referring to this and this post for my code.

This is the page where my webview display link

This is my code 'WebViewActivity.java`

public class WebViewActivity extends Activity{

TextToSpeech textToSpeech;


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

    final WebView webview = (WebView) findViewById(R.id.webView);
    TextView contentView = (TextView) findViewById(R.id.contentView);


    Intent intent = getIntent();
    String Address = intent.getStringExtra("URL");
    //Log.d("Valuer", value);
    //tt1.setText(value);

    class MyJavaScriptInterface
    {
        private TextView contentView;

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

        @SuppressWarnings("unused")
        @SuppressLint("JavascriptInterface")

        public void processContent(String aContent)
        {
            final String content = aContent;
            contentView.post(new Runnable()
            {
                public void run()
                {
                    contentView.setText(content);
                }
            });
        }
    }

    webview.getSettings().setLoadsImagesAutomatically(true);
    webview.addJavascriptInterface(new MyJavaScriptInterface(contentView), "INTERFACE");
    webview.getSettings().setJavaScriptEnabled(true);
    webview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    webview.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url)
        {
            webview.loadUrl("javascript:window.INTERFACE.processContent(document.getElementsByClassName('western')[0].innerText);");
            webview.loadUrl("javascript:(function() { " +
                    "document.getElementsByTagName('section')[0].style.display=\"none\"; " +
                    "})()");
        }
    });



    webview.loadUrl(Address);
    //textToSpeech.speak("TALK", TextToSpeech.QUEUE_FLUSH, null);


}



}
0

1 Answer 1

2

try this, By adding this line

       " webview.getSettings().setJavaScriptEnabled(true);" 

and Go to your "Proguard.rules" file and make that commented code workable for javascript

    # If your project uses WebView with JS, uncomment the following
    # and specify the fully qualified class name to the JavaScript interface
    # class:
    #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
    #   public *;
    #}
4
  • Thank you for your respond. Just by removing the # right ? I don't need to edit anything else? Can I know why android didn't just enable this in default settings? Is it due to security reasons?
    – azriebakri
    Jul 29, 2016 at 7:03
  • Yes just remove "#". And yes I think JavaScript is very sensitive thing to use, and it might take plenty of burden to manage it, that's why they recommend this. Jul 29, 2016 at 7:18
  • What does it means by "specify the fully qualified class name to the JavaScript interface class"?
    – azriebakri
    Jul 29, 2016 at 7:20
  • Please do google your questions!! The fully-qualified name for a class or interface is the package name followed by the class/interface name, separated by a period (.). For example, "com.marakana.utils.MyClass" Jul 29, 2016 at 7:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.