Android WebView在onPageStarted和onPageFinished之间注入JavaScript

时间:2014-08-13 15:46:10

标签: java javascript android webview

我需要在我的网页中注入一些JavaScript,我将其加载到WebView,但是在onPageFinished注入它太晚了,onPageStarted太早了。我可以用任何方式在这两个事件之间注入它吗?

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.");
    }
}

1 个答案:

答案 0 :(得分:2)

我最终没有最终使用它,我提出了一个更好的解决方案 - 但你可以做的是使用onProgressChanged中的WebChromeClient方法来执行某些操作装载阶段。

webView.setWebChromeClient(new WebChromeClient() {
    @Override
    public void onProgressChanged(WebView view, int progress) {
        // Do something cool here
    }
});