隐藏 WebView 直到 JavaScript 完成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16342100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 04:15:50  来源:igfitidea点击:

Hide WebView until JavaScript is done

javascriptandroidwebview

提问by GromDroid

I have a webview

我有一个网络视图

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

Simply said.

简单地说。

at:

在:

onPageFinished

I have:

我有:

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'; " + "})()");

I've set webview visibility to INVISIBLE

我已将 webview 可见性设置为 INVISIBLE

How can I set visibility to VISIBLE after the JavaScript is done?

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

Now you get to see the whole page for a second and than the JavaScript is done..

现在你可以看到整个页面一秒钟,然后 JavaScript 就完成了..

Anyone?

任何人?

ps. The website is not mine, its a 3rd party website

附:该网站不是我的,它是第 3 方网站

回答by AlexBcn

Tested on API 17 emulator and it works.

在 API 17 模拟器上测试过,它可以工作。

You can inject javascript from Java to the web.

您可以将 Java 中的 javascript 注入网络。

And do vice-versa, once the url is loaded call from javascript to a function on our Java code, and execute the setVisibility(). For that purpose you are going to add a JS interface.

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

Here the code:

这里的代码:

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

 }

This functionality is going to be executed for every page. Once on the 3rd party server you have to manage what to do with every request, webClient.shouldOverrideUrlLoading()can help you.

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

Updated answer:

更新的答案:

I could reproduce it as you commented, for the last version we should do:

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

Beginning in Android 4.2, you will now have to explicitly annotate public methods with @JavascriptInterface in order to make them accessible from hosted JavaScript. Note that this also only takes effect only if you have set your app's minSdkVersion or targetSdkVersion to 17 or higher.

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

I added it and imported android.webkit.JavascriptInterface

我添加并导入 android.webkit.JavascriptInterface

Reference: JavascriptInterface methods in WebViews must now be annotated

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

回答by Spettacolo83

I had the same problem of @GromDroid.

我遇到了@GromDroid 同样的问题。

Maybe not the best solution but it works:

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

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

I've added a delay of half second before make the webview visible.

在使 webview 可见之前,我添加了半秒的延迟。