简体   繁体   中英

Android Java OnPageFinished doesn't work when load Javascript in WebView

I need to get a link address from a webpage that is generated by javascript clicking on a button. I click on the link in this way

 webView.loadUrl("javascript:(function(){" +
                        "var All = document.getElementsByTagName('img');\n" +
                        "for (var i = 0; i < All.length; i++)       {\n" +
                        "if (All[i].getAttribute('title') == 'Download') {\n" +
                        "var imgButton=All[i];\n" +
                        "e=document.createEvent('HTMLEvents');\n" +
                        "e.initEvent('click',true,true);\n" +
                        "imgButton.dispatchEvent(e);\n" +
                        "}\n" +
                        "}})()");

the problem is that after running this code, the event OnPageFinished is never called. I had try with OnProgressChanged using WebChromeClient but I had the same result. I need to obtain the html code of the webpage after that the javascript is runned Any idea about how can I do it?

webView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            progressDialog = new ProgressDialog(Activity.this);
            progressDialog.setMessage("Loading...Please wait");
            progressDialog.setCanceledOnTouchOutside(false);
            progressDialog.show();
        }
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            if (progressDialog.isShowing()) {
                progressDialog.dismiss();
            }
                view.loadUrl("javascript:window.HTMLOUT.showHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Toast.makeText(getApplicationContext(), "Oh no! "+description , Toast.LENGTH_SHORT).show();
            super.onReceivedError(view, errorCode, description,failingUrl);
        }
    });

Here is the method

    @SuppressWarnings("unused")
class MyJavaScriptInterface {
    private Context ctx;

    MyJavaScriptInterface(Context ctx) {
        this.ctx = ctx;
    }
    @Keep
    @JavascriptInterface
    public void showHTML(String html) {
            try {
            Log.d("html",html);
            }catch (Exception e){
                e.printStackTrace();

            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM