4

When using WebView , is there a way to detect whether the document DOM tree has been loaded finish? I know that the WebViewClient provides a 'onPageFinish' callback,but it's called after all the web resources has been loaded finish.

If the web page is running a slow request (may be a bad request,and its http status is "waiting for responses"), then the "onPageFinish" won't be called until the request finish. I just want to catch the event when the web page's DOM tree loaded finish.

Any one help? Thanks a lot.

3 Answers 3

3

Use onPageCommitVisible replace onPageFinished.

 @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); prDialog = new ProgressDialog(MainActivity.this); prDialog.setMessage("Please wait ..."); prDialog.show(); } @Override public void onPageCommitVisible(WebView view, String url) { super.onPageCommitVisible(view, url); if(prDialog != null) prDialog.dismiss(); } 
Sign up to request clarification or add additional context in comments.

1 Comment

Add some explanation with answer for how this answer help OP in fixing current issue
1

Javascript has event that use to detect completed building the DOM tree called DOMContentLoaded.

JavaScript Page Load Events

You can inject Javascript with addJavascriptInterface() and let it call the function in Native side that defined with JavaScriptInterface annotation, then execute the DOMContentLoaded with evaluateJavascript like

webView.apply { settings.javaScriptEnabled = true // Inject Javascript addJavascriptInterface( DomContentLoadedInterface({ /* Do something */ }), DomContentLoadedInterface.KEY ) webViewClient = object : WebViewClient() { override fun onLoadResource(view: WebView?, url: String?) { super.onLoadResource(view, url) // Execute Javascript evaluateJavascript(DomContentLoadedInterface.SCRIPT) } } loadUrl(URL) } 

where DomContentLoadedInterface is

class DomContentLoadedInterface(private val action: () -> Unit) { @JavascriptInterface fun execute() { action() } companion object { internal const val KEY = "DomContentLoaded" internal const val SCRIPT = "javascript:document.addEventListener('DOMContentLoaded',function(){DomContentLoaded.execute()})" } } 

Note: evaluateJavascript will work in onLoadResource (https://stackoverflow.com/a/58932747/9896693) and after onPageFinished

Comments

-1

Here my example.For page

boolean isPageLoaded;
webView.setWebViewClient(new WebViewClient() {

 public boolean shouldOverrideUrlLoading(WebView view, String url) { isPageLoaded=true; return isPageLoaded; } public void onPageFinished(WebView view, String url) { if(!isPageLoaded){} } }); 

My examples for url loading finished.

if(!isPageLoaded){}

if url has ok process finished.

1 Comment

May be my question is not clear. I know the 'onPageFinished' callback,but this callback is called after all the resources request finished.If one of the requests not return, then the 'onPageFinished' won't be called.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.