隐藏WebView直到JavaScript完成

Hide WebView until JavaScript is done

本文关键字:完成 JavaScript 直到 WebView 隐藏      更新时间:2023-09-26

我有一个webview

WebView wv;
wv = (WebView)findViewById(R.id.webView1);
wv.loadUrl("http://example.com/");

简单地说。在:

onPageFinished

:

wv.loadUrl("javascript:(function() { " + "document.getElementsByClassName('centered leaderboard_container')[0].style.display = 'none'; " + "document.getElementsByClassName('n')[0].style.display = 'none'; " + "document.getElementsByClassName('paginator')[0].style.display = 'none'; " + "document.getElementsByTagName('ul')[0].style.display = 'none'; " + "document.getElementsByTagName('tr')[0].style.display = 'none'; " + "})()");

我把webview的可见性设置为INVISIBLE

如何在JavaScript完成后将可见性设置为可见?

现在你可以看到整个页面,然后JavaScript就完成了。

有人知道吗?

p。这个网站不是我的,它是一个第三方网站

在API 17模拟器上进行了测试,并且工作正常。

你可以把javascript从Java注入到web。

反之亦然,一旦url加载从javascript调用到我们的Java代码中的函数,并执行setVisibility()。为此,您将添加一个JS接口。

代码如下:

private final static String HOST = "stackoverflow.com";
private WebView wb;
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_home);
    wb = (WebView) findViewById(R.id.home_webview);
    //Make the webview invisible
    wb.setVisibility(View.INVISIBLE);
    WebSettings webSettings = wb.getSettings();
    webSettings.setJavaScriptEnabled(true);
    wb.setWebViewClient(new WebViewClient(){
        public void onPageFinished(WebView view, String url){
            //Inject javascript code to the url given
            //Not display the element
            wb.loadUrl("javascript:(function(){"+"document.getElementById('Id').style.display ='none';"+"})()");
            //Call to a function defined on my myJavaScriptInterface 
            wb.loadUrl("javascript: window.CallToAnAndroidFunction.setVisible()");
        }           
    });
    //Add a JavaScriptInterface, so I can make calls from the web to Java methods
    wb.addJavascriptInterface(new myJavaScriptInterface(), "CallToAnAndroidFunction");
    wb.loadUrl("http://"+HOST);
}
 public class myJavaScriptInterface {
     @JavascriptInterface
     public void setVisible(){
         runOnUiThread(new Runnable() {
            @Override
            public void run() {
                wb.setVisibility(View.VISIBLE);                 
            }
        });
     }
 }

此功能将对每个页面执行。一旦在第三方服务器上,您必须管理如何处理每个请求,webClient.shouldOverrideUrlLoading()可以帮助您。

更新回答:

我可以按照你的评论复制它,对于最后一个版本,我们应该这样做:

从Android 4.2开始,你现在必须用@JavascriptInterface显式地注释公共方法,以便从托管的JavaScript访问它们。请注意,这也只有在你将应用程序的minSdkVersion或targetSdkVersion设置为17或更高时才会生效。

我添加了它并导入了android.webkit.JavascriptInterface

参考:WebViews中的JavascriptInterface方法现在必须被注释

我也有同样的问题@GromDroid

也许不是最好的解决方案,但它有效:

public class myJavaScriptInterface {
        @JavascriptInterface
        public void setVisible(){
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            wb.setVisibility(View.VISIBLE);
                        }
                    }, 500);
                }
            });
        }
    }

我在webview可见前添加了半秒的延迟