You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ma...@apache.org on 2026/05/06 16:22:16 UTC
(cordova-plugin-inappbrowser) 01/01: fix(android): `beforeload` event Fires only once
This is an automated email from the ASF dual-hosted git repository.
GitToTheHub pushed a commit to branch pr-android-beforeload-event-fires-only-once
in repository https://gitbox.apache.org/repos/asf/cordova-plugin-inappbrowser.git
commit 5590c719f65a0bc3b33a99184955da5ba04e3a94
Author: Manuel Beck <ma...@outlook.de>
AuthorDate: Wed May 6 18:21:58 2026 +0200
fix(android): `beforeload` event Fires only once
- 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 | 51 +++++++++++++++++++++++++++++++++++++++++++
1 file changed, 51 insertions(+)
diff --git a/src/android/InAppBrowser.java b/src/android/InAppBrowser.java
index bb3495f..56dd817 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;
@@ -923,6 +924,46 @@ 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) {
+ final WebView inAppWebView = view;
+ final WebViewClient webViewClient = new WebViewClient() {
+ @Override
+ public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
+ String targetUrl = request.getUrl().toString();
+ if ("about:blank".equals(targetUrl)) {
+ return false;
+ }
+ if (currentClient != null && currentClient.shouldOverrideUrlLoading(targetUrl, request.getMethod())) {
+ return true;
+ }
+ inAppWebView.loadUrl(targetUrl);
+ return true;
+ }
+
+ @Override
+ public boolean shouldOverrideUrlLoading(WebView view, String url) {
+ if ("about:blank".equals(url)) {
+ return false;
+ }
+ if (currentClient != null && currentClient.shouldOverrideUrlLoading(url, null)) {
+ return true;
+ }
+ inAppWebView.loadUrl(url);
+ return true;
+ }
+ };
+
+ 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+");
@@ -1373,6 +1414,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);
@@ -1397,6 +1443,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: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org