在 WebViewClient 中启用通用 JavaScript [英] Enabling general JavaScript in WebViewClient

查看:20
本文介绍了在 WebViewClient 中启用通用 JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 google 中寻找答案时,似乎我不是唯一一个遇到似乎无法解决的问题的人.

While searching for an answer in google, it seems that I'm not the only one stuck with a problem that seems impossible to solve.

我已经设法使用自定义 WebViewClient 创建了一个 WebView - 这使我可以拥有一个进程对话框并在无法加载 URL 时显示错误消息.

I've managed to create a WebView with a custom WebViewClient - this makes it possible for me to have a processdialog and show an error message if an URL couldn't be loaded.

但这会给 JavaScript 带来问题.我正在加载的 URL 有一些 JavaScript,它改变了一些 HTML 元素的 CSS 样式(显示或隐藏元素)或重定向到另一个位置 onclick - 或者甚至可能想要显示一个警告框.但是通过使用 WebViewClient,这些都不起作用.

But this creates a problem with JavaScript. The URL I'm loading has some JavaScript which changes some HTML elements CSS styles (showing or hiding element) or redirects to another location onclick - or maybe even want's to show an alert box. But by using the WebViewClient none of those are working.

这是我加载页面的方式:

This is how I load a page:

public void loadUrl(String url)
{
    final ProgressDialog dialog = ProgressDialog.show(myActivity.this, "",  getString(R.string.loading), true);
            final WebView myWebView = (WebView) findViewById(R.id.webview);
            myWebView.setVerticalScrollBarEnabled(false);
            myWebView.setHorizontalScrollBarEnabled(false);
            WebSettings webSettings = myWebView.getSettings();
            webSettings.setJavaScriptEnabled(true);                                         


            myWebView.setWebViewClient(new WebViewClient() 
            {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) 
                {
                    Toast.makeText(myActivity.this, url, Toast.LENGTH_SHORT).show(); //Debugging purposes
                if (url.endsWith(".mp4")) 
                {
                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        intent.setDataAndType(Uri.parse(url), "video/mp4");
                        view.getContext().startActivity(intent);  
                }
                else
                {                           
                        view.loadUrl(url);
                    }

                    return true;
                }

                public void onPageFinished(WebView view, String url) 
                {
                    //Toast.makeText(myActivity.this, "Oh no!", Toast.LENGTH_SHORT).show();
                    dialog.dismiss();
                }

                public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) 
                {
                    Toast.makeText(myActivity.this, description, Toast.LENGTH_SHORT).show();
                    String summary = "<html><body><strong>" + getString(R.string.lost_connection) + "</body></html>";
                    myWebView.loadData(summary, "text/html", "utf-8");   
                }              

             }); //End WebViewClient

            myWebView.loadUrl(url);     
        }

这可能以更聪明的方式完成,但我是 Java 和 Android 开发的新手......

This could probably be done in a smarter way, but I'm new at both Java and Android development...

我是否可以为 WebViewClient 启用 JavaScript?删除 WebViewClient 解决了问题,但是当页面错误或完成加载时我无法捕获事件.

Is it possible for me to enable the JavaScript for the WebViewClient at all? Removing the WebViewClient solves the problem, but then I can't catch events when the page errors or is finished loading.

推荐答案

我不知道您的确切问题是什么,但我可以毫无问题地启用 JavaScript 和自定义 WebViewclient:

I don't know what your exact problem is, but i can enable the JavaScript and a custom WebViewclient without any problem:

WebView vistaWeb = (WebView) findViewById(R.id.webview);
vistaWeb.setWebChromeClient(new MyCustomChromeClient(this));
vistaWeb.setWebViewClient(new MyCustomWebViewClient(this));
vistaWeb.clearCache(true);
vistaWeb.clearHistory();
vistaWeb.getSettings().setJavaScriptEnabled(true);
vistaWeb.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

这篇关于在 WebViewClient 中启用通用 JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆