From 8dda83bd630b6cb092736c5065b8404059142774 Mon Sep 17 00:00:00 2001 From: primozratej Date: Tue, 24 Sep 2024 21:27:15 +0200 Subject: [PATCH] Redirect to external screen for links in ios --- lib/pages/web_view.dart | 11 ++++++++++- lib/util/auth_in_app_browser.dart | 9 +++++---- lib/util/black_list_rules.dart | 16 ++++++++++++++++ 3 files changed, 31 insertions(+), 5 deletions(-) create mode 100644 lib/util/black_list_rules.dart diff --git a/lib/pages/web_view.dart b/lib/pages/web_view.dart index 7f32aa3..1a0d81d 100644 --- a/lib/pages/web_view.dart +++ b/lib/pages/web_view.dart @@ -11,6 +11,7 @@ import 'package:humhub/models/channel_message.dart'; import 'package:humhub/models/hum_hub.dart'; import 'package:humhub/models/manifest.dart'; import 'package:humhub/pages/opener.dart'; +import 'package:humhub/util/black_list_rules.dart'; import 'package:humhub/util/connectivity_plugin.dart'; import 'package:humhub/util/const.dart'; import 'package:humhub/util/extensions.dart'; @@ -144,11 +145,19 @@ class WebViewAppState extends ConsumerState { WebViewGlobalController.ajaxSetHeaders(headers: ref.read(humHubProvider).customHeaders); //Open in external browser - final url = action.request.url!.origin; + final url = action.request.url!.rawValue; + /// First BLOCK everything that rules out as blocked. + if (BlackListRules.check(url)) { + return NavigationActionPolicy.CANCEL; + } if (!url.startsWith(_manifest.baseUrl) && action.isForMainFrame) { _authBrowser.launchUrl(action.request); return NavigationActionPolicy.CANCEL; } + if (!action.isForMainFrame) { + await launchUrl(action.request.url!.uriValue, mode: LaunchMode.externalApplication); + return NavigationActionPolicy.CANCEL; + } // 2nd Append customHeader if url is in app redirect and CANCEL the requests without custom headers if (Platform.isAndroid || action.navigationType == NavigationType.LINK_ACTIVATED || diff --git a/lib/util/auth_in_app_browser.dart b/lib/util/auth_in_app_browser.dart index 7a243f9..bff2b75 100644 --- a/lib/util/auth_in_app_browser.dart +++ b/lib/util/auth_in_app_browser.dart @@ -14,10 +14,11 @@ class AuthInAppBrowser extends InAppBrowser { AuthInAppBrowser({required this.manifest, required this.concludeAuth}) { settings = InAppBrowserClassSettings( browserSettings: InAppBrowserSettings( - hideUrlBar: true, - hideToolbarTop: true, - toolbarTopBackgroundColor: HexColor(manifest.themeColor), - ), + hideUrlBar: true, + shouldCloseOnBackButtonPressed: true, + toolbarTopBackgroundColor: HexColor(manifest.themeColor), + toolbarTopTintColor: HexColor(manifest.themeColor), + presentationStyle: ModalPresentationStyle.OVER_FULL_SCREEN), webViewSettings: InAppWebViewSettings( javaScriptEnabled: true, useShouldOverrideUrlLoading: true, diff --git a/lib/util/black_list_rules.dart b/lib/util/black_list_rules.dart new file mode 100644 index 0000000..54fbf95 --- /dev/null +++ b/lib/util/black_list_rules.dart @@ -0,0 +1,16 @@ +class BlackListRules { + // Define the blacklist of URLs + static const List _blacklist = [ + 'https://discord.com/', + ]; + + /// Checks if the provided [url] is on the blacklist. + /// Returns true if the URL is blacklisted, false otherwise. + static bool check(String url) { + // Normalize the URL by trimming whitespace and converting to lowercase + final normalizedUrl = url.trim().toLowerCase(); + + // Check if the normalized URL is in the blacklist + return _blacklist.contains(normalizedUrl); + } +} \ No newline at end of file