This is an automated email from the ASF dual-hosted git repository.

GitToTheHub pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/cordova-plugin-inappbrowser.git


The following commit(s) were added to refs/heads/master by this push:
     new 160e414  fix(android): `beforeload` event fires only once (#1146)
160e414 is described below

commit 160e414633bfc635cb28aa4f07d59efebce64da6
Author: Manuel Beck <[email protected]>
AuthorDate: Mon May 11 09:44:11 2026 +0200

    fix(android): `beforeload` event fires only once (#1146)
    
    - On Android, when beforeload=yes was enabled, the beforeload event would 
only fire on the initial page load, then stop firing for subsequent navigations.
    - Added onCreateWindow override in InAppChromeClient to route popup 
navigations through InAppBrowserClient.shouldOverrideUrlLoading() instead of 
direct loadUrl()
    - Filter out transient URLs like about:blank to avoid state corruption
    - Re-enable waitForBeforeload in onPageFinished() so subsequent user 
navigations trigger beforeload again
    - Re-enable waitForBeforeload in onReceivedError() to ensure beforeload 
works even after load errors
    - Generated-By: GPT-5.3-Codex
---
 src/android/InAppBrowser.java | 73 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 73 insertions(+)

diff --git a/src/android/InAppBrowser.java b/src/android/InAppBrowser.java
index 6a039c6..90c21f6 100644
--- a/src/android/InAppBrowser.java
+++ b/src/android/InAppBrowser.java
@@ -35,6 +35,7 @@ import android.net.http.SslError;
 import android.net.Uri;
 import android.os.Build;
 import android.os.Bundle;
+import android.os.Message;
 import android.text.InputType;
 import android.util.TypedValue;
 import android.view.Gravity;
@@ -930,6 +931,68 @@ public class InAppBrowser extends CordovaPlugin {
                 inAppWebView.setId(Integer.valueOf(6));
                 // File Chooser Implemented ChromeClient
                 inAppWebView.setWebChromeClient(new 
InAppChromeClient(thatWebView) {
+                    @Override
+                    public boolean onCreateWindow(WebView view, boolean 
isDialog, boolean isUserGesture, Message resultMsg) {
+                        // New-window navigations (for example window.open or 
target=_blank)
+                        // are delivered here by WebView. We do not show a 
second visible
+                        // browser window; instead, we route that navigation 
back into the
+                        // current InAppBrowser WebView so existing lifecycle 
and beforeload
+                        // handling remain consistent.
+                        final WebView inAppWebView = view;
+                        final WebViewClient webViewClient = new 
WebViewClient() {
+                            /**
+                             * New (added in API 24)
+                             * For Android 7 and above.
+                             */
+                            @Override
+                            public boolean shouldOverrideUrlLoading(WebView 
view, WebResourceRequest request) {
+                                return 
handleNewWindowUrl(request.getUrl().toString(), request.getMethod());
+                            }
+
+                            /**
+                             * Legacy (deprecated in API 24)
+                             * For Android 6 and below.
+                             */
+                            @Override
+                            public boolean shouldOverrideUrlLoading(WebView 
view, String url) {
+                                return handleNewWindowUrl(url, null);
+                            }
+
+                            private boolean handleNewWindowUrl(String 
targetUrl, String method) {
+                                // WebView commonly initializes popup flows 
with about:blank.
+                                // Forwarding this placeholder URL into the 
main WebView can
+                                // replace the current page and break 
subsequent navigation,
+                                // so it must be ignored.
+                                if ("about:blank".equals(targetUrl)) {
+                                    return false;
+                                }
+
+                                // Reuse the main client so beforeload and 
scheme routing are
+                                // applied exactly like regular navigations.
+                                if (currentClient != null && 
currentClient.shouldOverrideUrlLoading(targetUrl, method)) {
+                                    return true;
+                                }
+
+                                // If the main client did not consume the 
request, continue by
+                                // loading the URL in the currently visible 
InAppBrowser view.
+                                inAppWebView.loadUrl(targetUrl);
+                                return true;
+                            }
+                        };
+
+                        // Attach a temporary transport WebView required by 
the Android
+                        // onCreateWindow contract. Its client forwards 
navigation decisions
+                        // to the active InAppBrowser WebView/client above.
+                        final WebView newWebView = new 
WebView(view.getContext());
+                        newWebView.setWebViewClient(webViewClient);
+
+                        final WebView.WebViewTransport transport = 
(WebView.WebViewTransport) resultMsg.obj;
+                        transport.setWebView(newWebView);
+                        resultMsg.sendToTarget();
+
+                        return true;
+                    }
+
                     public boolean onShowFileChooser (WebView webView, 
ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams 
fileChooserParams)
                     {
                         LOG.d(LOG_TAG, "File Chooser 5.0+");
@@ -1389,6 +1452,11 @@ public class InAppBrowser extends CordovaPlugin {
         public void onPageFinished(WebView view, String url) {
             super.onPageFinished(view, url);
 
+            // Re-arm beforeload after allowing the previously approved 
navigation.
+            if (beforeload != null && !beforeload.isEmpty()) {
+                this.waitForBeforeload = true;
+            }
+
             // Set the namespace for postMessage()
             
injectDeferredObject("window.webkit={messageHandlers:{cordova_iab:cordova_iab}}",
 null);
 
@@ -1413,6 +1481,11 @@ public class InAppBrowser extends CordovaPlugin {
         public void onReceivedError(WebView view, int errorCode, String 
description, String failingUrl) {
             super.onReceivedError(view, errorCode, description, failingUrl);
 
+            // Ensure future navigations can still trigger beforeload after an 
error.
+            if (beforeload != null && !beforeload.isEmpty()) {
+                this.waitForBeforeload = true;
+            }
+
             try {
                 JSONObject obj = new JSONObject();
                 obj.put("type", LOAD_ERROR_EVENT);


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to