Skip to content

Commit

Permalink
fix #1455
Browse files Browse the repository at this point in the history
  • Loading branch information
pichillilorenzo committed Dec 6, 2022
1 parent 459875f commit 0f1c7e3
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -88,7 +89,12 @@ public View getView() {
return pullToRefreshLayout != null ? pullToRefreshLayout : webView;
}

@SuppressLint("RestrictedApi")
public void makeInitialLoad(HashMap<String, Object> params) {
if (webView == null) {
return;
}

Integer windowId = (Integer) params.get("windowId");
Map<String, Object> initialUrlRequest = (Map<String, Object>) params.get("initialUrlRequest");
final String initialFile = (String) params.get("initialFile");
Expand All @@ -99,6 +105,23 @@ public void makeInitialLoad(HashMap<String, Object> params) {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
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;
Expand Down Expand Up @@ -166,7 +165,7 @@ final public class InAppWebView extends InputAwareWebView implements InAppWebVie
public Map<String, WebMessageChannel> webMessageChannels = new HashMap<>();
public List<WebMessageListener> webMessageListeners = new ArrayList<>();

private List<UserScript> initialUserOnlyScript = new ArrayList<>();
private List<UserScript> initialUserOnlyScripts = new ArrayList<>();

@Nullable
public FindInteractionController findInteractionController;
Expand Down Expand Up @@ -198,7 +197,7 @@ public InAppWebView(Context context, @NonNull InAppWebViewFlutterPlugin plugin,
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);
}
Expand Down Expand Up @@ -260,7 +259,13 @@ public void prepare() {
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());
Expand Down Expand Up @@ -552,7 +557,7 @@ public boolean onLongClick(View v) {
});
}

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);
Expand All @@ -571,7 +576,7 @@ private void prepareAndAddUserScripts() {
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) {
Expand Down
5 changes: 2 additions & 3 deletions example/ios/Flutter/flutter_export_environment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 0f1c7e3

Please sign in to comment.