Android JS in WebView.loadUrl()

你说的曾经没有我的故事 提交于 2019-11-30 12:56:02

问题


I want to load a webpage in WebView but remove parts of the webpage. So, I created a custom WebViewClient. And, in onPageFinished(), I did some javascript to remove some elements. Then, I made the WebView visible.

However, when I run it, it sets the view visible, and then I see the elements being removed. It is as if the JS is running in the background very slowly. It creates a poor viewing experience because it flashes the full page and then the desired partial page.

Here is my onPageFinished()

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

    view.loadUrl("javascript:"
            + "document.getElementsByClassName('header')[0].style.display='none';"
            + "document.getElementById('section_0').style.display='none';"
            + "document.getElementById('page-actions').style.display='none';"
            + "document.getElementsByClassName('languageSelector')[0].style.display='none';"
            + "document.getElementById('mw-mf-last-modified').style.display='none';"
            + "document.getElementById('footer').style.display='none';");

    loadingView.setVisibility(View.INVISIBLE);
    view.setVisibility(View.VISIBLE);
}

Any ideas on how to fix this?


回答1:


In onPageFinished():

view.loadUrl("javascript:"
            + "var FunctionOne = function () {"
            + "  var r = $.Deferred();"
            + "  try{document.getElementsByClassName('header')[0].style.display='none';}catch(e){}"
            + "  try{document.getElementById('section_0').style.display='none';}catch(e){}"
            + "  try{document.getElementById('page-actions').style.display='none';}catch(e){}"
            + "  try{document.getElementsByClassName('languageSelector')[0].style.display='none';}catch(e){}"
            + "  try{document.getElementById('mw-mf-last-modified').style.display='none';}catch(e){}"
            + "  try{document.getElementById('footer').style.display='none';}catch(e){}"
            + "  setTimeout(function () {"
            + "    r.resolve();"
            + "  }, 2500);"
            + "  return r;"
            + "};"
            + "var FunctionTwo = function () {"
            + "  window.CallToAnAndroidFunction.setVisible();"
            + "};"
            + "FunctionOne().done(FunctionTwo);");

In MainActivity.onCreate():

this.webView.addJavascriptInterface(new JsObject(webView, loadingView), "CallToAnAndroidFunction");

In MainActivity():

public class JsObject {
    private View loadingView;
    private View view;
    JsObject(View view, View loadingView){this.view = view;this.loadingView = loadingView;}
    @JavascriptInterface
    public void setVisible(){
        runOnUiThread(new Runnable() {

           @Override
           public void run() {
               view.setVisibility(View.VISIBLE);  
                loadingView.setVisibility(View.INVISIBLE);               
           }
       });
    }
}

So, it was a combination of making a JavascriptInterface and making a JS function to wait for the JS calls to finish before calling the interface (with the visibility settings).




回答2:


You could try to speed up your WebView with:

webview.getSettings().setRenderPriority(RenderPriority.HIGH);
webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);

Anyways, you shouldn't be making your WebView visible right away. Why don't you create an Interface (refer to http://developer.android.com/guide/webapps/webview.html, Binding JavaScript code to Android) and make a call from your javascript to:

public void myCallback(){ view.SetVisibilitu(View.VISIBLE) };

after the animations have ended?



来源:https://stackoverflow.com/questions/19338999/android-js-in-webview-loadurl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!