From 5a1203d2a8f65a69c0817c2181f8b18a4521c1fc Mon Sep 17 00:00:00 2001 From: xtaodada Date: Fri, 12 Jul 2024 14:55:49 +0800 Subject: [PATCH] feat: Open Url Out Bot WebView Regex --- .../telegram/ui/bots/BotWebViewContainer.java | 16 ++++++++++++++++ .../NekoExperimentalSettingsActivity.java | 11 +++++++++++ .../main/kotlin/xyz/nextalone/nagram/NaConfig.kt | 6 ++++++ .../src/main/res/values-zh-rCN/strings_na.xml | 1 + TMessagesProj/src/main/res/values/strings_na.xml | 1 + 5 files changed, 35 insertions(+) diff --git a/TMessagesProj/src/main/java/org/telegram/ui/bots/BotWebViewContainer.java b/TMessagesProj/src/main/java/org/telegram/ui/bots/BotWebViewContainer.java index 4cf055d8d..4f70c14bf 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/bots/BotWebViewContainer.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/bots/BotWebViewContainer.java @@ -100,6 +100,9 @@ import java.util.List; import java.util.Map; import java.util.Objects; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.regex.Pattern; + +import xyz.nextalone.nagram.NaConfig; public abstract class BotWebViewContainer extends FrameLayout implements NotificationCenter.NotificationCenterDelegate { private final static String DURGER_KING_USERNAME = "DurgerKingBot"; @@ -1983,6 +1986,19 @@ public abstract class BotWebViewContainer extends FrameLayout implements Notific @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { Uri uriNew = Uri.parse(url); + + // ----- Nagram Hook start ----- + String urlPatternStr = NaConfig.INSTANCE.getOpenUrlOutBotWebViewRegex().String(); + if (botWebViewContainer != null && !urlPatternStr.isEmpty()) { + Pattern urlPattern = Pattern.compile(urlPatternStr, Pattern.MULTILINE | Pattern.CASE_INSENSITIVE); + if (urlPattern.matcher(url).find()) { + botWebViewContainer.onOpenUri(uriNew); + d("shouldOverrideUrlLoading("+url+") = true"); + return true; + } + } + // ----- Nagram Hook end ----- + if (botWebViewContainer != null && Browser.isInternalUri(uriNew, null)) { if (MessagesController.getInstance(botWebViewContainer.currentAccount).webAppAllowedProtocols != null && MessagesController.getInstance(botWebViewContainer.currentAccount).webAppAllowedProtocols.contains(uriNew.getScheme())) { diff --git a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/settings/NekoExperimentalSettingsActivity.java b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/settings/NekoExperimentalSettingsActivity.java index e7182eaa0..831108900 100644 --- a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/settings/NekoExperimentalSettingsActivity.java +++ b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/settings/NekoExperimentalSettingsActivity.java @@ -45,6 +45,7 @@ import org.telegram.ui.Components.UndoView; import java.io.File; import java.io.FileInputStream; import java.util.ArrayList; +import java.util.regex.Pattern; import kotlin.Unit; @@ -108,6 +109,16 @@ public class NekoExperimentalSettingsActivity extends BaseNekoXSettingsActivity private final AbstractConfigCell regexFiltersEnableInChatsRow = cellGroup.appendCell(new ConfigCellTextCheck(NaConfig.INSTANCE.getRegexFiltersEnableInChats())); private final AbstractConfigCell disableChatActionRow = cellGroup.appendCell(new ConfigCellTextCheck(NekoConfig.disableChatAction)); private final AbstractConfigCell disableChoosingStickerRow = cellGroup.appendCell(new ConfigCellTextCheck(NekoConfig.disableChoosingSticker)); + private final AbstractConfigCell openUrlOutBotWebViewRegexRow = cellGroup.appendCell(new ConfigCellTextInput(null, NaConfig.INSTANCE.getOpenUrlOutBotWebViewRegex(), + null, null, + (input) -> { + try { + Pattern.compile(input, Pattern.MULTILINE | Pattern.CASE_INSENSITIVE); + return input; + } catch (Exception ignored) { + return ""; + } + })); private final AbstractConfigCell divider1 = cellGroup.appendCell(new ConfigCellDivider()); // Story diff --git a/TMessagesProj/src/main/kotlin/xyz/nextalone/nagram/NaConfig.kt b/TMessagesProj/src/main/kotlin/xyz/nextalone/nagram/NaConfig.kt index 23e6e9494..1dae2d16d 100644 --- a/TMessagesProj/src/main/kotlin/xyz/nextalone/nagram/NaConfig.kt +++ b/TMessagesProj/src/main/kotlin/xyz/nextalone/nagram/NaConfig.kt @@ -638,6 +638,12 @@ object NaConfig { ConfigItem.configTypeInt, 0 ) + val openUrlOutBotWebViewRegex = + addConfig( + "OpenUrlOutBotWebViewRegex", + ConfigItem.configTypeString, + "" + ) private fun addConfig( k: String, diff --git a/TMessagesProj/src/main/res/values-zh-rCN/strings_na.xml b/TMessagesProj/src/main/res/values-zh-rCN/strings_na.xml index b1e4506a1..7a8dfbcc1 100644 --- a/TMessagesProj/src/main/res/values-zh-rCN/strings_na.xml +++ b/TMessagesProj/src/main/res/values-zh-rCN/strings_na.xml @@ -158,4 +158,5 @@ 点击消息时间显示详细信息 在频道点击标签默认搜索页面 在其他对话点击标签默认搜索页面 + 指定 URL Regex 跳出 bot webview diff --git a/TMessagesProj/src/main/res/values/strings_na.xml b/TMessagesProj/src/main/res/values/strings_na.xml index 75e83dff0..ff20d0c52 100644 --- a/TMessagesProj/src/main/res/values/strings_na.xml +++ b/TMessagesProj/src/main/res/values/strings_na.xml @@ -158,4 +158,5 @@ Click message time to display details Channel Click Hashtag Default Search Page Chat Click Hashtag Default Search Page + Url regex to jump out of bot webview