diff --git a/app/src/main/java/com/ding1ding/jsbridge/app/MainActivity.kt b/app/src/main/java/com/ding1ding/jsbridge/app/MainActivity.kt index 736d7b2..97a7b88 100644 --- a/app/src/main/java/com/ding1ding/jsbridge/app/MainActivity.kt +++ b/app/src/main/java/com/ding1ding/jsbridge/app/MainActivity.kt @@ -86,6 +86,7 @@ class MainActivity : javaScriptEnabled = true allowUniversalAccessFromFileURLs = true } + webViewClient = createWebViewClient() } @@ -117,10 +118,13 @@ class MainActivity : private fun createWebViewClient() = object : WebViewClient() { override fun onPageStarted(view: WebView?, url: String?, favicon: android.graphics.Bitmap?) { + super.onPageStarted(view, url, favicon) Logger.d(TAG) { "onPageStarted" } } override fun onPageFinished(view: WebView?, url: String?) { + super.onPageFinished(view, url) + bridge.injectJavascriptIfNeeded() Logger.d(TAG) { "onPageFinished" } } } diff --git a/library/src/main/java/com/ding1ding/jsbridge/WebViewJavascriptBridge.kt b/library/src/main/java/com/ding1ding/jsbridge/WebViewJavascriptBridge.kt index 62f22bd..d53a9fc 100644 --- a/library/src/main/java/com/ding1ding/jsbridge/WebViewJavascriptBridge.kt +++ b/library/src/main/java/com/ding1ding/jsbridge/WebViewJavascriptBridge.kt @@ -32,7 +32,6 @@ class WebViewJavascriptBridge private constructor( init { setupBridge() - setupWebViewClient() } @SuppressLint("SetJavaScriptEnabled") @@ -43,19 +42,33 @@ class WebViewJavascriptBridge private constructor( Logger.d { "Bridge setup completed" } } - private fun setupWebViewClient() { + fun setWebViewClient(client: WebViewClient?) { webView.webViewClient = object : WebViewClient() { override fun onPageFinished(view: WebView?, url: String?) { super.onPageFinished(view, url) isWebViewReady.set(true) Logger.d { "WebView page finished loading" } injectJavascriptIfNeeded() + client?.onPageFinished(view, url) } + + @Deprecated( + "Deprecated in Java", + ReplaceWith( + "client?.shouldOverrideUrlLoading(view, url) ?: super.shouldOverrideUrlLoading(view, url)", + "android.webkit.WebViewClient", + ), + ) + override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean = + client?.shouldOverrideUrlLoading(view, url) + ?: super.shouldOverrideUrlLoading(view, url) + + // Add other WebViewClient methods as needed, delegating to the client if it's not null } } @MainThread - private fun injectJavascriptIfNeeded() { + fun injectJavascriptIfNeeded() { if (isInjected.get() || !isWebViewReady.get()) { Logger.d { "JavaScript injection skipped. Injected: ${isInjected.get()}, WebView ready: ${isWebViewReady.get()}" @@ -179,7 +192,6 @@ class WebViewJavascriptBridge private constructor( fun reinitialize() { release() setupBridge() - setupWebViewClient() Logger.d { "Bridge reinitialized" } } @@ -214,8 +226,10 @@ class WebViewJavascriptBridge private constructor( context: Context, webView: WebView, lifecycle: Lifecycle? = null, + webViewClient: WebViewClient? = null, ): WebViewJavascriptBridge = WebViewJavascriptBridge(context, webView).also { bridge -> lifecycle?.addObserver(bridge) + bridge.setWebViewClient(webViewClient) Logger.d { "Bridge created and lifecycle observer added" } }