From d39b1ef37475c87dd744ff3e2f07f24efd37dadb Mon Sep 17 00:00:00 2001 From: Lorenzo Pichilli Date: Wed, 16 Nov 2022 15:30:17 +0100 Subject: [PATCH] Fixed User Script remove methods, Fixed macOS available checks for XCode 14.1 --- CHANGELOG.md | 5 ++ .../Types/WKUserContentController.swift | 4 +- .../in_app_webview_controller.dart | 76 +++++++++++++++---- .../in_app_webview_settings.dart | 4 +- .../in_app_webview_settings.g.dart | 4 +- lib/src/types/user_script.dart | 2 +- lib/src/types/user_script.g.dart | 2 +- macos/Classes/InAppWebView/InAppWebView.swift | 4 +- .../InAppWebView/InAppWebViewSettings.swift | 2 + .../InAppWebView/WebViewChannelDelegate.swift | 2 +- .../Types/WKUserContentController.swift | 4 +- pubspec.yaml | 2 +- 12 files changed, 86 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 695460d9..17c23f50 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 6.0.0-beta.14 + +- Fixed User Script remove methods +- Fixed macOS available checks for XCode 14.1 + ## 6.0.0-beta.13 - Added `ContentBlockerActionType.BLOCK_COOKIES` and `ContentBlockerActionType.IGNORE_PREVIOUS_RULES` for iOS and macOS platforms diff --git a/ios/Classes/Types/WKUserContentController.swift b/ios/Classes/Types/WKUserContentController.swift index 53c901a7..eecba322 100644 --- a/ios/Classes/Types/WKUserContentController.swift +++ b/ios/Classes/Types/WKUserContentController.swift @@ -276,7 +276,7 @@ extension WKUserContentController { var userScriptsUpdated: [WKUserScript] = [] for script in userScripts { - if !userScripts.contains(script) { + if !scriptsToRemove.contains(script) { userScriptsUpdated.append(script) } } @@ -296,6 +296,7 @@ extension WKUserContentController { for script in allUserOnlyScripts { if let scriptName = script.groupName, scriptName == groupName { scriptsToRemove.append(script) + userOnlyScripts[script.injectionTime]!.remove(script) } } removeUserScripts(scriptsToRemove: scriptsToRemove, shouldAddPreviousScripts: shouldAddPreviousScripts) @@ -307,6 +308,7 @@ extension WKUserContentController { for script in allPluginScripts { if let scriptName = script.groupName, scriptName == groupName { scriptsToRemove.append(script) + pluginScripts[script.injectionTime]!.remove(script) } } removeUserScripts(scriptsToRemove: scriptsToRemove, shouldAddPreviousScripts: shouldAddPreviousScripts) diff --git a/lib/src/in_app_webview/in_app_webview_controller.dart b/lib/src/in_app_webview/in_app_webview_controller.dart index d935365a..23a0e213 100644 --- a/lib/src/in_app_webview/in_app_webview_controller.dart +++ b/lib/src/in_app_webview/in_app_webview_controller.dart @@ -56,7 +56,10 @@ class InAppWebViewController { static MethodChannel _staticChannel = IN_APP_WEBVIEW_STATIC_CHANNEL; Map javaScriptHandlersMap = HashMap(); - List _userScripts = []; + final Map> _userScripts = { + UserScriptInjectionTime.AT_DOCUMENT_START: [], + UserScriptInjectionTime.AT_DOCUMENT_END: [] + }; Set _webMessageListenerObjNames = Set(); Map _injectedScriptsFromURL = {}; @@ -88,8 +91,23 @@ class InAppWebViewController { } }); this._webview = webview; - this._userScripts = - List.from(webview.initialUserScripts ?? []); + + final initialUserScripts = webview.initialUserScripts; + if (initialUserScripts != null) { + for (final userScript in initialUserScripts) { + if (userScript.injectionTime == + UserScriptInjectionTime.AT_DOCUMENT_START) { + this + ._userScripts[UserScriptInjectionTime.AT_DOCUMENT_START] + ?.add(userScript); + } else { + this + ._userScripts[UserScriptInjectionTime.AT_DOCUMENT_END] + ?.add(userScript); + } + } + } + this._init(); } @@ -99,8 +117,21 @@ class InAppWebViewController { UnmodifiableListView? initialUserScripts) { this._channel = channel; this._inAppBrowser = inAppBrowser; - this._userScripts = - List.from(initialUserScripts ?? []); + + if (initialUserScripts != null) { + for (final userScript in initialUserScripts) { + if (userScript.injectionTime == + UserScriptInjectionTime.AT_DOCUMENT_START) { + this + ._userScripts[UserScriptInjectionTime.AT_DOCUMENT_START] + ?.add(userScript); + } else { + this + ._userScripts[UserScriptInjectionTime.AT_DOCUMENT_END] + ?.add(userScript); + } + } + } this._init(); } @@ -2793,8 +2824,9 @@ class InAppWebViewController { Map args = {}; args.putIfAbsent('userScript', () => userScript.toMap()); - if (!_userScripts.contains(userScript)) { - _userScripts.add(userScript); + if (!(_userScripts[userScript.injectionTime]?.contains(userScript) ?? + false)) { + _userScripts[userScript.injectionTime]?.add(userScript); await _channel.invokeMethod('addUserScript', args); } } @@ -2834,12 +2866,12 @@ class InAppWebViewController { assert(_webview?.windowId == null || defaultTargetPlatform != TargetPlatform.iOS); - var index = _userScripts.indexOf(userScript); - if (index == -1) { + var index = _userScripts[userScript.injectionTime]?.indexOf(userScript); + if (index == null || index == -1) { return false; } - _userScripts.remove(userScript); + _userScripts[userScript.injectionTime]?.remove(userScript); Map args = {}; args.putIfAbsent('userScript', () => userScript.toMap()); args.putIfAbsent('index', () => index); @@ -2863,6 +2895,22 @@ class InAppWebViewController { assert(_webview?.windowId == null || defaultTargetPlatform != TargetPlatform.iOS); + final List userScriptsAtDocumentStart = List.from( + _userScripts[UserScriptInjectionTime.AT_DOCUMENT_START] ?? []); + for (final userScript in userScriptsAtDocumentStart) { + if (userScript.groupName == groupName) { + _userScripts[userScript.injectionTime]?.remove(userScript); + } + } + + final List userScriptsAtDocumentEnd = + List.from(_userScripts[UserScriptInjectionTime.AT_DOCUMENT_END] ?? []); + for (final userScript in userScriptsAtDocumentEnd) { + if (userScript.groupName == groupName) { + _userScripts[userScript.injectionTime]?.remove(userScript); + } + } + Map args = {}; args.putIfAbsent('groupName', () => groupName); await _channel.invokeMethod('removeUserScriptsByGroupName', args); @@ -2884,8 +2932,8 @@ class InAppWebViewController { assert(_webview?.windowId == null || defaultTargetPlatform != TargetPlatform.iOS); - for (var i = 0; i < userScripts.length; i++) { - await removeUserScript(userScript: userScripts[i]); + for (final userScript in userScripts) { + await removeUserScript(userScript: userScript); } } @@ -2903,7 +2951,9 @@ class InAppWebViewController { assert(_webview?.windowId == null || defaultTargetPlatform != TargetPlatform.iOS); - _userScripts.clear(); + _userScripts[UserScriptInjectionTime.AT_DOCUMENT_START]?.clear(); + _userScripts[UserScriptInjectionTime.AT_DOCUMENT_END]?.clear(); + Map args = {}; await _channel.invokeMethod('removeAllUserScripts', args); } diff --git a/lib/src/in_app_webview/in_app_webview_settings.dart b/lib/src/in_app_webview/in_app_webview_settings.dart index f2719551..00914197 100755 --- a/lib/src/in_app_webview/in_app_webview_settings.dart +++ b/lib/src/in_app_webview/in_app_webview_settings.dart @@ -1174,7 +1174,7 @@ class InAppWebViewSettings_ { /// ///**NOTE for iOS**: available on iOS 15.4+. /// - ///**NOTE for MacOS**: available on MacOS 12.0+. + ///**NOTE for MacOS**: available on MacOS 12.3+. /// ///**Supported Platforms/Implementations**: ///- iOS @@ -1199,7 +1199,7 @@ class InAppWebViewSettings_ { /// ///**NOTE for iOS**: available on iOS 15.4+. /// - ///**NOTE for MacOS**: available on MacOS 12.0+. + ///**NOTE for MacOS**: available on MacOS 12.3+. /// ///**Supported Platforms/Implementations**: ///- iOS diff --git a/lib/src/in_app_webview/in_app_webview_settings.g.dart b/lib/src/in_app_webview/in_app_webview_settings.g.dart index 39821929..3e9af0ce 100644 --- a/lib/src/in_app_webview/in_app_webview_settings.g.dart +++ b/lib/src/in_app_webview/in_app_webview_settings.g.dart @@ -1130,7 +1130,7 @@ class InAppWebViewSettings { /// ///**NOTE for iOS**: available on iOS 15.4+. /// - ///**NOTE for MacOS**: available on MacOS 12.0+. + ///**NOTE for MacOS**: available on MacOS 12.3+. /// ///**Supported Platforms/Implementations**: ///- iOS @@ -1155,7 +1155,7 @@ class InAppWebViewSettings { /// ///**NOTE for iOS**: available on iOS 15.4+. /// - ///**NOTE for MacOS**: available on MacOS 12.0+. + ///**NOTE for MacOS**: available on MacOS 12.3+. /// ///**Supported Platforms/Implementations**: ///- iOS diff --git a/lib/src/types/user_script.dart b/lib/src/types/user_script.dart index 8836dbaa..6e159842 100644 --- a/lib/src/types/user_script.dart +++ b/lib/src/types/user_script.dart @@ -27,7 +27,7 @@ class UserScript_ { ///Specify true to inject the script only into the main frame, or false to inject it into all frames. ///The default value is `true`. /// - ///**NOTE**: available only on iOS. + ///**NOTE**: available only on iOS and MacOS. bool forMainFrameOnly; ///A set of matching rules for the allowed origins. diff --git a/lib/src/types/user_script.g.dart b/lib/src/types/user_script.g.dart index aafa9dc4..414f7c8e 100644 --- a/lib/src/types/user_script.g.dart +++ b/lib/src/types/user_script.g.dart @@ -25,7 +25,7 @@ class UserScript { ///Specify true to inject the script only into the main frame, or false to inject it into all frames. ///The default value is `true`. /// - ///**NOTE**: available only on iOS. + ///**NOTE**: available only on iOS and MacOS. bool forMainFrameOnly; ///A set of matching rules for the allowed origins. diff --git a/macos/Classes/InAppWebView/InAppWebView.swift b/macos/Classes/InAppWebView/InAppWebView.swift index 7f015033..5c8bf04f 100755 --- a/macos/Classes/InAppWebView/InAppWebView.swift +++ b/macos/Classes/InAppWebView/InAppWebView.swift @@ -175,7 +175,7 @@ public class InAppWebView: WKWebView, WKUIDelegate, configuration.preferences.isTextInteractionEnabled = settings.isTextInteractionEnabled } - if #available(macOS 12.0, *) { + if #available(macOS 12.3, *) { configuration.preferences.isSiteSpecificQuirksModeEnabled = settings.isSiteSpecificQuirksModeEnabled configuration.preferences.isElementFullscreenEnabled = settings.isElementFullscreenEnabled } @@ -725,7 +725,7 @@ public class InAppWebView: WKWebView, WKUIDelegate, self.underPageBackgroundColor = NSColor(hexString: underPageBackgroundColor) } } - if #available(macOS 12.0, *) { + if #available(macOS 12.3, *) { if newSettingsMap["isSiteSpecificQuirksModeEnabled"] != nil, settings?.isSiteSpecificQuirksModeEnabled != newSettings.isSiteSpecificQuirksModeEnabled { configuration.preferences.isSiteSpecificQuirksModeEnabled = newSettings.isSiteSpecificQuirksModeEnabled } diff --git a/macos/Classes/InAppWebView/InAppWebViewSettings.swift b/macos/Classes/InAppWebView/InAppWebViewSettings.swift index 5a83d2d1..6ecbd559 100755 --- a/macos/Classes/InAppWebView/InAppWebViewSettings.swift +++ b/macos/Classes/InAppWebView/InAppWebViewSettings.swift @@ -100,6 +100,8 @@ public class InAppWebViewSettings: ISettings { } if #available(macOS 12.0, *) { realSettings["underPageBackgroundColor"] = webView.underPageBackgroundColor.hexString + } + if #available(macOS 12.3, *) { realSettings["isSiteSpecificQuirksModeEnabled"] = configuration.preferences.isSiteSpecificQuirksModeEnabled realSettings["isElementFullscreenEnabled"] = configuration.preferences.isElementFullscreenEnabled } diff --git a/macos/Classes/InAppWebView/WebViewChannelDelegate.swift b/macos/Classes/InAppWebView/WebViewChannelDelegate.swift index 59ac3dd3..c8957c23 100644 --- a/macos/Classes/InAppWebView/WebViewChannelDelegate.swift +++ b/macos/Classes/InAppWebView/WebViewChannelDelegate.swift @@ -595,7 +595,7 @@ public class WebViewChannelDelegate : ChannelDelegate { break case .isInFullscreen: if let webView = webView { - if #available(macOS 12.0, *) { + if #available(macOS 13.0, *) { result(webView.fullscreenState == .inFullscreen) } else { result(webView.inFullscreen) diff --git a/macos/Classes/Types/WKUserContentController.swift b/macos/Classes/Types/WKUserContentController.swift index fb17db60..59f8f126 100644 --- a/macos/Classes/Types/WKUserContentController.swift +++ b/macos/Classes/Types/WKUserContentController.swift @@ -276,7 +276,7 @@ extension WKUserContentController { var userScriptsUpdated: [WKUserScript] = [] for script in userScripts { - if !userScripts.contains(script) { + if !scriptsToRemove.contains(script) { userScriptsUpdated.append(script) } } @@ -296,6 +296,7 @@ extension WKUserContentController { for script in allUserOnlyScripts { if let scriptName = script.groupName, scriptName == groupName { scriptsToRemove.append(script) + userOnlyScripts[script.injectionTime]!.remove(script) } } removeUserScripts(scriptsToRemove: scriptsToRemove, shouldAddPreviousScripts: shouldAddPreviousScripts) @@ -307,6 +308,7 @@ extension WKUserContentController { for script in allPluginScripts { if let scriptName = script.groupName, scriptName == groupName { scriptsToRemove.append(script) + pluginScripts[script.injectionTime]!.remove(script) } } removeUserScripts(scriptsToRemove: scriptsToRemove, shouldAddPreviousScripts: shouldAddPreviousScripts) diff --git a/pubspec.yaml b/pubspec.yaml index 6e760d4a..287fa7ec 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.13 +version: 6.0.0-beta.14 homepage: https://inappwebview.dev/ repository: https://github.com/pichillilorenzo/flutter_inappwebview issue_tracker: https://github.com/pichillilorenzo/flutter_inappwebview/issues