From 396ba77c207b438651ba6b83fb4b31e623544c00 Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Mon, 15 Jul 2024 02:55:42 +0400 Subject: [PATCH] feat(YouTube): Add `Bypass image region restrictions` patch (#667) --- .../BypassImageRegionRestrictionsPatch.java | 46 +++++++++++++++++++ .../youtube/settings/Settings.java | 1 + 2 files changed, 47 insertions(+) create mode 100644 app/src/main/java/app/revanced/integrations/youtube/patches/BypassImageRegionRestrictionsPatch.java diff --git a/app/src/main/java/app/revanced/integrations/youtube/patches/BypassImageRegionRestrictionsPatch.java b/app/src/main/java/app/revanced/integrations/youtube/patches/BypassImageRegionRestrictionsPatch.java new file mode 100644 index 0000000000..2fac8e39c2 --- /dev/null +++ b/app/src/main/java/app/revanced/integrations/youtube/patches/BypassImageRegionRestrictionsPatch.java @@ -0,0 +1,46 @@ +package app.revanced.integrations.youtube.patches; + +import static app.revanced.integrations.youtube.settings.Settings.BYPASS_IMAGE_REGION_RESTRICTIONS; + +import java.util.regex.Pattern; + +import app.revanced.integrations.shared.Logger; +import app.revanced.integrations.youtube.settings.Settings; + +@SuppressWarnings("unused") +public final class BypassImageRegionRestrictionsPatch { + + private static final boolean BYPASS_IMAGE_REGION_RESTRICTIONS_ENABLED = BYPASS_IMAGE_REGION_RESTRICTIONS.get(); + + private static final String REPLACEMENT_IMAGE_DOMAIN = "https://yt4.ggpht.com"; + + /** + * YouTube static images domain. Includes user and channel avatar images and community post images. + */ + private static final Pattern YOUTUBE_STATIC_IMAGE_DOMAIN_PATTERN + = Pattern.compile("^https://(yt3|lh[3-6]|play-lh)\\.(ggpht|googleusercontent)\\.com"); + + /** + * Injection point. Called off the main thread and by multiple threads at the same time. + * + * @param originalUrl Image url for all image urls loaded. + */ + public static String overrideImageURL(String originalUrl) { + try { + if (BYPASS_IMAGE_REGION_RESTRICTIONS_ENABLED) { + String replacement = YOUTUBE_STATIC_IMAGE_DOMAIN_PATTERN + .matcher(originalUrl).replaceFirst(REPLACEMENT_IMAGE_DOMAIN); + + if (Settings.DEBUG.get() && !replacement.equals(originalUrl)) { + Logger.printDebug(() -> "Replaced: '" + originalUrl + "' with: '" + replacement + "'"); + } + + return replacement; + } + } catch (Exception ex) { + Logger.printException(() -> "overrideImageURL failure", ex); + } + + return originalUrl; + } +} diff --git a/app/src/main/java/app/revanced/integrations/youtube/settings/Settings.java b/app/src/main/java/app/revanced/integrations/youtube/settings/Settings.java index ddf5532897..470cf7873b 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/settings/Settings.java +++ b/app/src/main/java/app/revanced/integrations/youtube/settings/Settings.java @@ -189,6 +189,7 @@ public class Settings extends BaseSettings { public static final StringSetting SPOOF_APP_VERSION_TARGET = new StringSetting("revanced_spoof_app_version_target", "17.33.42", true, parent(SPOOF_APP_VERSION)); public static final BooleanSetting TABLET_LAYOUT = new BooleanSetting("revanced_tablet_layout", FALSE, true, "revanced_tablet_layout_user_dialog_message"); public static final BooleanSetting WIDE_SEARCHBAR = new BooleanSetting("revanced_wide_searchbar", FALSE, true); + public static final BooleanSetting BYPASS_IMAGE_REGION_RESTRICTIONS = new BooleanSetting("revanced_bypass_image_region_restrictions", FALSE, true); public static final BooleanSetting REMOVE_VIEWER_DISCRETION_DIALOG = new BooleanSetting("revanced_remove_viewer_discretion_dialog", FALSE, "revanced_remove_viewer_discretion_dialog_user_dialog_message");