Javascript在我的WebView实现中无法正常工作

时间:2015-03-22 16:12:22

标签: javascript android html webview android-webview

我正在尝试使用javascript和Disqus解决问题,我必须实现我的WebView,而我所做的只是从网址下载html以及一些字符串替换

html = html.replaceFirst("<div", "<div id=\"headerApp\"></div><div");
html = html.replace("<head>", "<head> <style>"+cssContent +"</style>");
html = html.replace("class='hidden-phone'"," ");
html = html.replace("class=\"btn-mobile-pager visible-phone\"","class=\"btn-mobile-pager hidden-phone\"");

然后致电

loadDataWithBaseURL("blarg://ignored", html, "text/html", "utf-8", "");

但是disqus评论很奇怪,看起来像这样:

enter image description here

我认为这是某种javascript问题,这就是我的工作:

WebSettings settings = myWebView.getSettings();
settings.setSupportZoom(false);
settings.setBuiltInZoomControls(false);
settings.setLoadsImagesAutomatically(true);

myWebView.setWebChromeClient(new WebChromeClient());
myWebView.setWebViewClient(new WebViewClient() {

        public boolean shouldOverrideUrlLoading(WebView viewx, String urlx) {

            MyWebView myView = null;

            if(viewx instanceof MyWebView) {
                myView = (MyWebView) viewx;


                if(urlx.contains("https://twitter") || urlx.contains("action=em_fb_edit"))
                {
                    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(urlx));
                    startActivity(browserIntent);
                    myView.stopLoading();
                    return true;
                }
                else if (!urlx.startsWith("http://www.efficacemente")) {
                    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(urlx));
                    startActivity(browserIntent);
                    return false;
                } else if (urlx.contains("@facebook")) {
                    //Fai l'intent a facebook se puoi
                    if (isAppInstalled("com.facebook.katana")) {
                        String uri = "fb://page/101189567644";
                        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
                        startActivity(intent);
                        return false;
                    } else {
                        String url = "http://www.facebook.com/101189567644";
                        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                        startActivity(browserIntent);
                        return false;
                    }
                } else if (urlx.contains("@twitter")) {
                    if (isAppInstalled("com.twitter.android")) {
                        String uri = "twitter://user?screen_name=EfficaceMente";
                        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
                        startActivity(intent);
                        return false;
                    } else {
                        String url = "http://www.twitter.com/Efficacemente";
                        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                        startActivity(browserIntent);
                        return false;
                    }

                } else if (urlx.contains("@bookclub")) {
                    if (isAppInstalled("com.facebook.katana")) {
                        String uri = "fb://page/256384041207653";
                        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
                        startActivity(intent);
                        return false;
                    } else {
                        String url = "http://www.facebook.com/256384041207653";
                        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                        startActivity(browserIntent);
                        return false;
                    }
                } else {
                    myView.myLoad(urlx);
                    return true;
                }
            }
            else
                return false;

        }
    });
myWebView.clearCache(true);
myWebView.clearHistory();
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

myWebView是我创建的MyWebView类的实例,我只需将html下载到html并使用loadDataWithBaseURL加载

任何提示?谢谢!

1 个答案:

答案 0 :(得分:0)

而不是blarg://ignored,在转换之前使用网页的实际网址。您的方法意味着无法加载通过相对URL(图像,JavaScript,CSS等)引用的任何内容,因为WebView不知道要从哪里加载这些内容。