简体   繁体   中英

Javascript not working for WebView - Android

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);


}



}

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 *;
    #}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM