【问题标题】:Javascript not working properly in my WebView implementationJavascript 在我的 WebView 实现中无法正常工作
【发布时间】:2015-03-22 16:12:22
【问题描述】:

我正在尝试解决 javascript 和 Disqus 的问题,我必须实现我的 WebView,我所做的只是从 url 下载 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 cmets很奇怪,看起来像这样:

我相信这是某种 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 加载它

有什么提示吗?谢谢!

【问题讨论】:

    标签: javascript android html webview android-webview


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-16
      • 2018-07-02
      • 1970-01-01
      • 2012-12-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多