From 0f1c7e30291af9b13a25fd06e2651e506726238a Mon Sep 17 00:00:00 2001 From: Lorenzo Pichilli Date: Tue, 6 Dec 2022 02:26:29 +0100 Subject: [PATCH] fix #1455 --- CHANGELOG.md | 4 ++++ .../in_app_webview/FlutterWebView.java | 23 +++++++++++++++++++ .../webview/in_app_webview/InAppWebView.java | 17 +++++++++----- .../ios/Flutter/flutter_export_environment.sh | 5 ++-- pubspec.yaml | 2 +- 5 files changed, 41 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9837682e..e2b6601d 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 6.0.0-beta.21 + +- Fixed "Android plugin version 6 - UserScripts not executing on new tabs." [#1455](https://github.com/pichillilorenzo/flutter_inappwebview/issues/1455) + ## 6.0.0-beta.20 - Using Android `WebViewClientCompat` for Chromium-based WebView if the WebView package major version is >= 73 (https://bugs.chromium.org/p/chromium/issues/detail?id=925887) diff --git a/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/webview/in_app_webview/FlutterWebView.java b/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/webview/in_app_webview/FlutterWebView.java index 5e0b1c74..e22aefe4 100755 --- a/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/webview/in_app_webview/FlutterWebView.java +++ b/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/webview/in_app_webview/FlutterWebView.java @@ -1,5 +1,6 @@ package com.pichillilorenzo.flutter_inappwebview.webview.in_app_webview; +import android.annotation.SuppressLint; import android.content.Context; import android.hardware.display.DisplayManager; import android.os.Build; @@ -88,7 +89,12 @@ public class FlutterWebView implements PlatformWebView { return pullToRefreshLayout != null ? pullToRefreshLayout : webView; } + @SuppressLint("RestrictedApi") public void makeInitialLoad(HashMap params) { + if (webView == null) { + return; + } + Integer windowId = (Integer) params.get("windowId"); Map initialUrlRequest = (Map) params.get("initialUrlRequest"); final String initialFile = (String) params.get("initialFile"); @@ -99,6 +105,23 @@ public class FlutterWebView implements PlatformWebView { if (resultMsg != null) { ((WebView.WebViewTransport) resultMsg.obj).setWebView(webView); resultMsg.sendToTarget(); + if (WebViewFeature.isFeatureSupported(WebViewFeature.DOCUMENT_START_SCRIPT)) { + // for some reason, if a WebView is created using a window id, + // the initial plugin and user scripts injected + // with WebViewCompat.addDocumentStartJavaScript will not be added! + // https://github.com/pichillilorenzo/flutter_inappwebview/issues/1455 + // + // Also, calling the prepareAndAddUserScripts method right after won't work, + // so use the View.post method here. + webView.post(new Runnable() { + @Override + public void run() { + if (webView != null) { + webView.prepareAndAddUserScripts(); + } + } + }); + } } } else { if (initialFile != null) { diff --git a/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/webview/in_app_webview/InAppWebView.java b/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/webview/in_app_webview/InAppWebView.java index 2eafe26c..9380f92d 100755 --- a/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/webview/in_app_webview/InAppWebView.java +++ b/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/webview/in_app_webview/InAppWebView.java @@ -61,7 +61,6 @@ import androidx.webkit.WebViewCompat; import androidx.webkit.WebViewFeature; import com.pichillilorenzo.flutter_inappwebview.InAppWebViewFlutterPlugin; -import com.pichillilorenzo.flutter_inappwebview.InAppWebViewStatic; import com.pichillilorenzo.flutter_inappwebview.R; import com.pichillilorenzo.flutter_inappwebview.Util; import com.pichillilorenzo.flutter_inappwebview.content_blocker.ContentBlocker; @@ -166,7 +165,7 @@ final public class InAppWebView extends InputAwareWebView implements InAppWebVie public Map webMessageChannels = new HashMap<>(); public List webMessageListeners = new ArrayList<>(); - private List initialUserOnlyScript = new ArrayList<>(); + private List initialUserOnlyScripts = new ArrayList<>(); @Nullable public FindInteractionController findInteractionController; @@ -198,7 +197,7 @@ final public class InAppWebView extends InputAwareWebView implements InAppWebVie this.windowId = windowId; this.customSettings = customSettings; this.contextMenu = contextMenu; - this.initialUserOnlyScript = userScripts; + this.initialUserOnlyScripts = userScripts; if (plugin != null && plugin.activity != null) { plugin.activity.registerForContextMenu(this); } @@ -260,7 +259,13 @@ final public class InAppWebView extends InputAwareWebView implements InAppWebVie WebViewCompat.setWebViewRenderProcessClient(this, inAppWebViewRenderProcessClient); } - prepareAndAddUserScripts(); + if (windowId == null || !WebViewFeature.isFeatureSupported(WebViewFeature.DOCUMENT_START_SCRIPT)) { + // for some reason, if a WebView is created using a window id, + // the initial plugin and user scripts injected + // with WebViewCompat.addDocumentStartJavaScript will not be added! + // https://github.com/pichillilorenzo/flutter_inappwebview/issues/1455 + prepareAndAddUserScripts(); + } if (customSettings.useOnDownloadStart) setDownloadListener(new DownloadStartListener()); @@ -552,7 +557,7 @@ final public class InAppWebView extends InputAwareWebView implements InAppWebVie }); } - private void prepareAndAddUserScripts() { + public void prepareAndAddUserScripts() { userContentController.addPluginScript(PromisePolyfillJS.PROMISE_POLYFILL_JS_PLUGIN_SCRIPT); userContentController.addPluginScript(JavaScriptBridgeJS.JAVASCRIPT_BRIDGE_JS_PLUGIN_SCRIPT); userContentController.addPluginScript(ConsoleLogJS.CONSOLE_LOG_JS_PLUGIN_SCRIPT); @@ -571,7 +576,7 @@ final public class InAppWebView extends InputAwareWebView implements InAppWebVie if (!customSettings.useHybridComposition) { userContentController.addPluginScript(PluginScriptsUtil.CHECK_GLOBAL_KEY_DOWN_EVENT_TO_HIDE_CONTEXT_MENU_JS_PLUGIN_SCRIPT); } - this.userContentController.addUserOnlyScripts(this.initialUserOnlyScript); + this.userContentController.addUserOnlyScripts(this.initialUserOnlyScripts); } public void setIncognito(boolean enabled) { diff --git a/example/ios/Flutter/flutter_export_environment.sh b/example/ios/Flutter/flutter_export_environment.sh index 68661abb..a7647f6d 100755 --- a/example/ios/Flutter/flutter_export_environment.sh +++ b/example/ios/Flutter/flutter_export_environment.sh @@ -3,12 +3,11 @@ export "FLUTTER_ROOT=/Users/lorenzopichilli/fvm/versions/3.3.6" export "FLUTTER_APPLICATION_PATH=/Users/lorenzopichilli/Desktop/flutter_inappwebview/example" export "COCOAPODS_PARALLEL_CODE_SIGN=true" -export "FLUTTER_TARGET=integration_test/webview_flutter_test.dart" +export "FLUTTER_TARGET=lib/main.dart" export "FLUTTER_BUILD_DIR=build" export "FLUTTER_BUILD_NAME=1.0.0" export "FLUTTER_BUILD_NUMBER=1" -export "DART_DEFINES=RkxVVFRFUl9XRUJfQVVUT19ERVRFQ1Q9dHJ1ZQ==" export "DART_OBFUSCATION=false" export "TRACK_WIDGET_CREATION=true" export "TREE_SHAKE_ICONS=false" -export "PACKAGE_CONFIG=/Users/lorenzopichilli/Desktop/flutter_inappwebview/example/.dart_tool/package_config.json" +export "PACKAGE_CONFIG=.dart_tool/package_config.json" diff --git a/pubspec.yaml b/pubspec.yaml index 4c332a44..c1deee90 100755 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: flutter_inappwebview description: A Flutter plugin that allows you to add an inline webview, to use an headless webview, and to open an in-app browser window. -version: 6.0.0-beta.20 +version: 6.0.0-beta.21 homepage: https://inappwebview.dev/ repository: https://github.com/pichillilorenzo/flutter_inappwebview issue_tracker: https://github.com/pichillilorenzo/flutter_inappwebview/issues