我需要在我的网页中注入一些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.");
}
}
答案 0 :(得分:2)
我最终没有最终使用它,我提出了一个更好的解决方案 - 但你可以做的是使用onProgressChanged
中的WebChromeClient
方法来执行某些操作装载阶段。
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int progress) {
// Do something cool here
}
});