为什么JS在API 28中而不在Webview中的23中执行?

时间:2019-07-19 09:22:44

标签: javascript java android webview

我有一个api代码可以在api 28中工作,但不能在api 23中使用,该js代码是删除css文件的所有超链接,

enter image description here

代码:

  WebSettings webSettings = siteView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setAllowUniversalAccessFromFileURLs(true);
    webSettings.setDomStorageEnabled(true);

    siteView.setInitialScale(200);
   // JSInterface api = new JSInterface();
   // siteView.addJavascriptInterface(api, "api");
    siteView.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageFinished(final WebView view, String url) {

                if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.KITKAT){

                    view.evaluateJavascript("var selector = '#top1 > p > img'; document.head.appendChild(Object.assign(document.createElement('style'), { innerText: '*{visibility:hidden}' + selector + '{visibility:visible}' }));\n" +
                            "                document.body.style.marginLeft=\"0px\";\n" +
                            "                document.body.style.marginTop=\"0px\";\n" +
                            "                $(\"body > link:nth-child(1)\").removeAttr(\"href\");\n" +
                            "                $(\"body > link:nth-child(4)\").removeAttr(\"href\");\n" +
                            "                $(\"head > link:nth-child(18)\").removeAttr(\"href\");\n" +
                            "                document.querySelector(\"#top1 > p > img\").src=\"/ORegMx/capito.png\";", null);
                } else {
                    view.loadUrl("javascript: (function(){        var selector = '#top1 > p > img'; document.head.appendChild(Object.assign(document.createElement('style'), { innerText: '*{visibility:hidden}' + selector + '{visibility:visible}' }));\n" +
                            "document.body.style.marginLeft=\"0px\";\n" +
                            "document.body.style.marginTop=\"0px\";\n" +
                            "$(\"body > link:nth-child(1)\").removeAttr(\"href\");\n" +
                            "$(\"body > link:nth-child(4)\").removeAttr(\"href\");\n" +
                            "$(\"head > link:nth-child(18)\").removeAttr(\"href\");\n" +
                            "document.querySelector(\"#top1 > p > img\").src=\"/ORegMx/capito.png\";\n})();");                    }

        }




    siteView.loadUrl(URL);


}

我认为EvaluationJavascript无法正常工作

我该如何解决?

感谢任何帮助!

0 个答案:

没有答案