Skip to content

Commit 0f1c7e3

Browse files
fix #1455
1 parent 459875f commit 0f1c7e3

File tree

5 files changed

+41
-10
lines changed

5 files changed

+41
-10
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 6.0.0-beta.21
2+
3+
- Fixed "Android plugin version 6 - UserScripts not executing on new tabs." [#1455](https://github.com/pichillilorenzo/flutter_inappwebview/issues/1455)
4+
15
## 6.0.0-beta.20
26

37
- 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)

android/src/main/java/com/pichillilorenzo/flutter_inappwebview/webview/in_app_webview/FlutterWebView.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.pichillilorenzo.flutter_inappwebview.webview.in_app_webview;
22

3+
import android.annotation.SuppressLint;
34
import android.content.Context;
45
import android.hardware.display.DisplayManager;
56
import android.os.Build;
@@ -88,7 +89,12 @@ public View getView() {
8889
return pullToRefreshLayout != null ? pullToRefreshLayout : webView;
8990
}
9091

92+
@SuppressLint("RestrictedApi")
9193
public void makeInitialLoad(HashMap<String, Object> params) {
94+
if (webView == null) {
95+
return;
96+
}
97+
9298
Integer windowId = (Integer) params.get("windowId");
9399
Map<String, Object> initialUrlRequest = (Map<String, Object>) params.get("initialUrlRequest");
94100
final String initialFile = (String) params.get("initialFile");
@@ -99,6 +105,23 @@ public void makeInitialLoad(HashMap<String, Object> params) {
99105
if (resultMsg != null) {
100106
((WebView.WebViewTransport) resultMsg.obj).setWebView(webView);
101107
resultMsg.sendToTarget();
108+
if (WebViewFeature.isFeatureSupported(WebViewFeature.DOCUMENT_START_SCRIPT)) {
109+
// for some reason, if a WebView is created using a window id,
110+
// the initial plugin and user scripts injected
111+
// with WebViewCompat.addDocumentStartJavaScript will not be added!
112+
// https://github.com/pichillilorenzo/flutter_inappwebview/issues/1455
113+
//
114+
// Also, calling the prepareAndAddUserScripts method right after won't work,
115+
// so use the View.post method here.
116+
webView.post(new Runnable() {
117+
@Override
118+
public void run() {
119+
if (webView != null) {
120+
webView.prepareAndAddUserScripts();
121+
}
122+
}
123+
});
124+
}
102125
}
103126
} else {
104127
if (initialFile != null) {

android/src/main/java/com/pichillilorenzo/flutter_inappwebview/webview/in_app_webview/InAppWebView.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161
import androidx.webkit.WebViewFeature;
6262

6363
import com.pichillilorenzo.flutter_inappwebview.InAppWebViewFlutterPlugin;
64-
import com.pichillilorenzo.flutter_inappwebview.InAppWebViewStatic;
6564
import com.pichillilorenzo.flutter_inappwebview.R;
6665
import com.pichillilorenzo.flutter_inappwebview.Util;
6766
import com.pichillilorenzo.flutter_inappwebview.content_blocker.ContentBlocker;
@@ -166,7 +165,7 @@ final public class InAppWebView extends InputAwareWebView implements InAppWebVie
166165
public Map<String, WebMessageChannel> webMessageChannels = new HashMap<>();
167166
public List<WebMessageListener> webMessageListeners = new ArrayList<>();
168167

169-
private List<UserScript> initialUserOnlyScript = new ArrayList<>();
168+
private List<UserScript> initialUserOnlyScripts = new ArrayList<>();
170169

171170
@Nullable
172171
public FindInteractionController findInteractionController;
@@ -198,7 +197,7 @@ public InAppWebView(Context context, @NonNull InAppWebViewFlutterPlugin plugin,
198197
this.windowId = windowId;
199198
this.customSettings = customSettings;
200199
this.contextMenu = contextMenu;
201-
this.initialUserOnlyScript = userScripts;
200+
this.initialUserOnlyScripts = userScripts;
202201
if (plugin != null && plugin.activity != null) {
203202
plugin.activity.registerForContextMenu(this);
204203
}
@@ -260,7 +259,13 @@ public void prepare() {
260259
WebViewCompat.setWebViewRenderProcessClient(this, inAppWebViewRenderProcessClient);
261260
}
262261

263-
prepareAndAddUserScripts();
262+
if (windowId == null || !WebViewFeature.isFeatureSupported(WebViewFeature.DOCUMENT_START_SCRIPT)) {
263+
// for some reason, if a WebView is created using a window id,
264+
// the initial plugin and user scripts injected
265+
// with WebViewCompat.addDocumentStartJavaScript will not be added!
266+
// https://github.com/pichillilorenzo/flutter_inappwebview/issues/1455
267+
prepareAndAddUserScripts();
268+
}
264269

265270
if (customSettings.useOnDownloadStart)
266271
setDownloadListener(new DownloadStartListener());
@@ -552,7 +557,7 @@ public boolean onLongClick(View v) {
552557
});
553558
}
554559

555-
private void prepareAndAddUserScripts() {
560+
public void prepareAndAddUserScripts() {
556561
userContentController.addPluginScript(PromisePolyfillJS.PROMISE_POLYFILL_JS_PLUGIN_SCRIPT);
557562
userContentController.addPluginScript(JavaScriptBridgeJS.JAVASCRIPT_BRIDGE_JS_PLUGIN_SCRIPT);
558563
userContentController.addPluginScript(ConsoleLogJS.CONSOLE_LOG_JS_PLUGIN_SCRIPT);
@@ -571,7 +576,7 @@ private void prepareAndAddUserScripts() {
571576
if (!customSettings.useHybridComposition) {
572577
userContentController.addPluginScript(PluginScriptsUtil.CHECK_GLOBAL_KEY_DOWN_EVENT_TO_HIDE_CONTEXT_MENU_JS_PLUGIN_SCRIPT);
573578
}
574-
this.userContentController.addUserOnlyScripts(this.initialUserOnlyScript);
579+
this.userContentController.addUserOnlyScripts(this.initialUserOnlyScripts);
575580
}
576581

577582
public void setIncognito(boolean enabled) {

example/ios/Flutter/flutter_export_environment.sh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
export "FLUTTER_ROOT=/Users/lorenzopichilli/fvm/versions/3.3.6"
44
export "FLUTTER_APPLICATION_PATH=/Users/lorenzopichilli/Desktop/flutter_inappwebview/example"
55
export "COCOAPODS_PARALLEL_CODE_SIGN=true"
6-
export "FLUTTER_TARGET=integration_test/webview_flutter_test.dart"
6+
export "FLUTTER_TARGET=lib/main.dart"
77
export "FLUTTER_BUILD_DIR=build"
88
export "FLUTTER_BUILD_NAME=1.0.0"
99
export "FLUTTER_BUILD_NUMBER=1"
10-
export "DART_DEFINES=RkxVVFRFUl9XRUJfQVVUT19ERVRFQ1Q9dHJ1ZQ=="
1110
export "DART_OBFUSCATION=false"
1211
export "TRACK_WIDGET_CREATION=true"
1312
export "TREE_SHAKE_ICONS=false"
14-
export "PACKAGE_CONFIG=/Users/lorenzopichilli/Desktop/flutter_inappwebview/example/.dart_tool/package_config.json"
13+
export "PACKAGE_CONFIG=.dart_tool/package_config.json"

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: flutter_inappwebview
22
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.
3-
version: 6.0.0-beta.20
3+
version: 6.0.0-beta.21
44
homepage: https://inappwebview.dev/
55
repository: https://github.com/pichillilorenzo/flutter_inappwebview
66
issue_tracker: https://github.com/pichillilorenzo/flutter_inappwebview/issues

0 commit comments

Comments
 (0)