当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Android WebView inject JavaScript between onPageStarted and onPageFinished

问题描述:

I need to inject some JavaScript into my web page that I'm loading into a WebView, but injecting it at onPageFinished is too late and onPageStarted is too soon. Any way I can inject it between this two events?

webView.setWebViewClient(new WebViewClient() {

@Override

public void onPageFinished(WebView view, String url) {

super.onPageFinished(view, url);

progress.setVisibility(view.GONE);

view.loadUrl("javascript:registerObjectDetailsCallback(" +

"function(details) {" +

"if (details.length == 1) {" +

"window.location.href = 'callback:' + escape(details[0]);" +

"} else if (details.length > 1) {" +

"alert('Error: callback set by registerObjectDetailsCallback was passed multiple selection');" +

"}" +

"}" +

");");

Log.d("onPageFinished: ", "JavaScript registered.");

}

}

网友答案:

I didn't end up using this in the end, I came up with a better solution - but what you can do is use the onProgressChanged method in WebChromeClient in order to perform actions at certain stages of loading.

webView.setWebChromeClient(new WebChromeClient() {
    @Override
    public void onProgressChanged(WebView view, int progress) {
        // Do something cool here
    }
});
分享给朋友:
您可能感兴趣的文章:
随机阅读: