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