Skip to content

Commit 45c1ee8

Browse files
author
LisoUseInAIKyrios
authored
feat(YouTube Music): Add Sanitize sharing links patch (#5952)
1 parent 74cdf55 commit 45c1ee8

File tree

11 files changed

+198
-102
lines changed

11 files changed

+198
-102
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package app.revanced.extension.shared.patches;
2+
3+
import app.revanced.extension.shared.settings.BaseSettings;
4+
5+
/**
6+
* YouTube and YouTube Music.
7+
*/
8+
@SuppressWarnings("unused")
9+
public final class SanitizeSharingLinksPatch {
10+
private static final String NEW_TRACKING_PARAMETER_REGEX = ".si=.+";
11+
private static final String OLD_TRACKING_PARAMETER_REGEX = ".feature=.+";
12+
13+
/**
14+
* Injection point.
15+
*/
16+
public static String sanitize(String url) {
17+
if (BaseSettings.SANITIZE_SHARED_LINKS.get()) {
18+
url = url
19+
.replaceAll(NEW_TRACKING_PARAMETER_REGEX, "")
20+
.replaceAll(OLD_TRACKING_PARAMETER_REGEX, "");
21+
}
22+
23+
if (BaseSettings.REPLACE_MUSIC_LINKS_WITH_YOUTUBE.get()) {
24+
url = url.replace("music.youtube.com", "youtube.com");
25+
}
26+
27+
return url;
28+
}
29+
}

extensions/shared/library/src/main/java/app/revanced/extension/shared/settings/BaseSettings.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,7 @@ public class BaseSettings {
3131
public static final BooleanSetting SPOOF_VIDEO_STREAMS = new BooleanSetting("revanced_spoof_video_streams", TRUE, true, "revanced_spoof_video_streams_user_dialog_message");
3232
public static final EnumSetting<AppLanguage> SPOOF_VIDEO_STREAMS_LANGUAGE = new EnumSetting<>("revanced_spoof_video_streams_language", AppLanguage.DEFAULT, new AudioStreamLanguageOverrideAvailability());
3333
public static final BooleanSetting SPOOF_STREAMING_DATA_STATS_FOR_NERDS = new BooleanSetting("revanced_spoof_streaming_data_stats_for_nerds", TRUE, parent(SPOOF_VIDEO_STREAMS));
34+
35+
public static final BooleanSetting SANITIZE_SHARED_LINKS = new BooleanSetting("revanced_sanitize_sharing_links", TRUE);
36+
public static final BooleanSetting REPLACE_MUSIC_LINKS_WITH_YOUTUBE = new BooleanSetting("revanced_replace_music_with_youtube", FALSE);
3437
}

extensions/youtube/src/main/java/app/revanced/extension/youtube/patches/RemoveTrackingQueryParameterPatch.java

Lines changed: 0 additions & 17 deletions
This file was deleted.

extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/Settings.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,6 @@ public class Settings extends BaseSettings {
349349
public static final BooleanSetting DISABLE_HAPTIC_FEEDBACK_SEEK_UNDO = new BooleanSetting("revanced_disable_haptic_feedback_seek_undo", FALSE);
350350
public static final BooleanSetting DISABLE_HAPTIC_FEEDBACK_ZOOM = new BooleanSetting("revanced_disable_haptic_feedback_zoom", FALSE);
351351
public static final BooleanSetting EXTERNAL_BROWSER = new BooleanSetting("revanced_external_browser", TRUE, true);
352-
public static final BooleanSetting REMOVE_TRACKING_QUERY_PARAMETER = new BooleanSetting("revanced_remove_tracking_query_parameter", TRUE);
353352
public static final BooleanSetting SPOOF_DEVICE_DIMENSIONS = new BooleanSetting("revanced_spoof_device_dimensions", FALSE, true,
354353
"revanced_spoof_device_dimensions_user_dialog_message");
355354
public static final EnumSetting<ClientType> SPOOF_VIDEO_STREAMS_CLIENT_TYPE = new EnumSetting<>("revanced_spoof_video_streams_client_type", ClientType.ANDROID_VR_1_61_48, true, parent(SPOOF_VIDEO_STREAMS));

patches/api/patches.api

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,10 @@ public final class app/revanced/patches/music/misc/gms/GmsCoreSupportPatchKt {
417417
public static final fun getGmsCoreSupportPatch ()Lapp/revanced/patcher/patch/BytecodePatch;
418418
}
419419

420+
public final class app/revanced/patches/music/misc/privacy/SanitizeSharingLinksPatchKt {
421+
public static final fun getSanitizeSharingLinksPatch ()Lapp/revanced/patcher/patch/BytecodePatch;
422+
}
423+
420424
public final class app/revanced/patches/music/misc/settings/PreferenceScreen : app/revanced/patches/shared/misc/settings/preference/BasePreferenceScreen {
421425
public static final field INSTANCE Lapp/revanced/patches/music/misc/settings/PreferenceScreen;
422426
public fun commit (Lapp/revanced/patches/shared/misc/settings/preference/PreferenceScreenPreference;)V
@@ -1664,6 +1668,10 @@ public final class app/revanced/patches/youtube/misc/privacy/RemoveTrackingQuery
16641668
public static final fun getRemoveTrackingQueryParameterPatch ()Lapp/revanced/patcher/patch/BytecodePatch;
16651669
}
16661670

1671+
public final class app/revanced/patches/youtube/misc/privacy/SanitizeSharingLinksPatchKt {
1672+
public static final fun getSanitizeSharingLinksPatch ()Lapp/revanced/patcher/patch/BytecodePatch;
1673+
}
1674+
16671675
public final class app/revanced/patches/youtube/misc/recyclerviewtree/hook/RecyclerViewTreeHookPatchKt {
16681676
public static final fun getAddRecyclerViewTreeHook ()Lkotlin/jvm/functions/Function1;
16691677
public static final fun getRecyclerViewTreeHookPatch ()Lapp/revanced/patcher/patch/BytecodePatch;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package app.revanced.patches.music.misc.privacy
2+
3+
import app.revanced.patches.music.misc.extension.sharedExtensionPatch
4+
import app.revanced.patches.music.misc.settings.PreferenceScreen
5+
import app.revanced.patches.music.misc.settings.settingsPatch
6+
import app.revanced.patches.shared.misc.privacy.sanitizeSharingLinksPatch
7+
8+
@Suppress("unused")
9+
val sanitizeSharingLinksPatch = sanitizeSharingLinksPatch(
10+
block = {
11+
dependsOn(
12+
sharedExtensionPatch,
13+
settingsPatch,
14+
)
15+
16+
compatibleWith(
17+
"com.google.android.apps.youtube.music"(
18+
"7.29.52",
19+
"8.10.52"
20+
)
21+
)
22+
},
23+
preferenceScreen = PreferenceScreen.MISC,
24+
replaceMusicLinksWithYouTube = true
25+
)

patches/src/main/kotlin/app/revanced/patches/youtube/misc/privacy/Fingerprints.kt renamed to patches/src/main/kotlin/app/revanced/patches/shared/misc/privacy/Fingerprints.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
package app.revanced.patches.youtube.misc.privacy
1+
package app.revanced.patches.shared.misc.privacy
22

3-
import com.android.tools.smali.dexlib2.Opcode
4-
import com.android.tools.smali.dexlib2.AccessFlags
53
import app.revanced.patcher.fingerprint
4+
import com.android.tools.smali.dexlib2.AccessFlags
5+
import com.android.tools.smali.dexlib2.Opcode
66

7-
internal val copyTextFingerprint = fingerprint {
7+
internal val youTubeCopyTextFingerprint = fingerprint {
88
returns("V")
99
parameters("L", "Ljava/util/Map;")
1010
opcodes(
@@ -21,7 +21,7 @@ internal val copyTextFingerprint = fingerprint {
2121
strings("text/plain")
2222
}
2323

24-
internal val systemShareSheetFingerprint = fingerprint {
24+
internal val youTubeSystemShareSheetFingerprint = fingerprint {
2525
returns("V")
2626
parameters("L", "Ljava/util/Map;")
2727
opcodes(
@@ -31,7 +31,7 @@ internal val systemShareSheetFingerprint = fingerprint {
3131
strings("YTShare_Logging_Share_Intent_Endpoint_Byte_Array")
3232
}
3333

34-
internal val youtubeShareSheetFingerprint = fingerprint {
34+
internal val youTubeShareSheetFingerprint = fingerprint {
3535
accessFlags(AccessFlags.PUBLIC, AccessFlags.FINAL)
3636
returns("V")
3737
parameters("L", "Ljava/util/Map;")
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package app.revanced.patches.shared.misc.privacy
2+
3+
import app.revanced.patcher.Fingerprint
4+
import app.revanced.patcher.Match
5+
import app.revanced.patcher.extensions.InstructionExtensions.addInstructions
6+
import app.revanced.patcher.extensions.InstructionExtensions.getInstruction
7+
import app.revanced.patcher.patch.BytecodePatchBuilder
8+
import app.revanced.patcher.patch.BytecodePatchContext
9+
import app.revanced.patcher.patch.bytecodePatch
10+
import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod
11+
import app.revanced.patches.all.misc.resources.addResources
12+
import app.revanced.patches.all.misc.resources.addResourcesPatch
13+
import app.revanced.patches.shared.misc.settings.preference.BasePreferenceScreen
14+
import app.revanced.patches.shared.misc.settings.preference.PreferenceCategory
15+
import app.revanced.patches.shared.misc.settings.preference.PreferenceScreenPreference.Sorting
16+
import app.revanced.patches.shared.misc.settings.preference.SwitchPreference
17+
import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction
18+
import com.android.tools.smali.dexlib2.iface.instruction.TwoRegisterInstruction
19+
20+
private const val EXTENSION_CLASS_DESCRIPTOR =
21+
"Lapp/revanced/extension/shared/patches/SanitizeSharingLinksPatch;"
22+
23+
internal fun sanitizeSharingLinksPatch(
24+
block: BytecodePatchBuilder.() -> Unit = {},
25+
executeBlock: BytecodePatchContext.() -> Unit = {},
26+
preferenceScreen: BasePreferenceScreen.Screen,
27+
replaceMusicLinksWithYouTube: Boolean = false
28+
) = bytecodePatch(
29+
name = "Sanitize sharing links",
30+
description = "Adds an option to remove the tracking parameter from links you share.",
31+
) {
32+
block()
33+
34+
dependsOn(addResourcesPatch)
35+
36+
execute {
37+
executeBlock()
38+
39+
addResources("shared", "misc.privacy.sanitizeSharingLinksPatch")
40+
41+
val sanitizePreference = SwitchPreference("revanced_sanitize_sharing_links")
42+
43+
preferenceScreen.addPreferences(
44+
if (replaceMusicLinksWithYouTube) {
45+
PreferenceCategory(
46+
titleKey = null,
47+
sorting = Sorting.UNSORTED,
48+
tag = "app.revanced.extension.shared.settings.preference.NoTitlePreferenceCategory",
49+
preferences = setOf(
50+
sanitizePreference,
51+
SwitchPreference("revanced_replace_music_with_youtube")
52+
)
53+
)
54+
} else {
55+
sanitizePreference
56+
}
57+
)
58+
59+
fun Fingerprint.hook(
60+
getInsertIndex: Match.PatternMatch.() -> Int,
61+
getUrlRegister: MutableMethod.(insertIndex: Int) -> Int,
62+
) {
63+
val insertIndex = patternMatch!!.getInsertIndex()
64+
val urlRegister = method.getUrlRegister(insertIndex)
65+
66+
method.addInstructions(
67+
insertIndex,
68+
"""
69+
invoke-static {v$urlRegister}, $EXTENSION_CLASS_DESCRIPTOR->sanitize(Ljava/lang/String;)Ljava/lang/String;
70+
move-result-object v$urlRegister
71+
"""
72+
)
73+
}
74+
75+
// YouTube share sheet.\
76+
youTubeShareSheetFingerprint.hook(getInsertIndex = { startIndex + 1 }) { insertIndex ->
77+
getInstruction<OneRegisterInstruction>(insertIndex - 1).registerA
78+
}
79+
80+
// Native system share sheet.
81+
youTubeSystemShareSheetFingerprint.hook(getInsertIndex = { endIndex }) { insertIndex ->
82+
getInstruction<OneRegisterInstruction>(insertIndex - 1).registerA
83+
}
84+
85+
youTubeCopyTextFingerprint.hook(getInsertIndex = { startIndex + 2 }) { insertIndex ->
86+
getInstruction<TwoRegisterInstruction>(insertIndex - 2).registerA
87+
}
88+
}
89+
}
Lines changed: 4 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,9 @@
11
package app.revanced.patches.youtube.misc.privacy
22

3-
import app.revanced.patcher.Fingerprint
4-
import app.revanced.patcher.Match
5-
import app.revanced.patcher.extensions.InstructionExtensions.addInstructions
6-
import app.revanced.patcher.extensions.InstructionExtensions.getInstruction
73
import app.revanced.patcher.patch.bytecodePatch
8-
import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod
9-
import app.revanced.patches.all.misc.resources.addResources
10-
import app.revanced.patches.all.misc.resources.addResourcesPatch
11-
import app.revanced.patches.shared.misc.settings.preference.SwitchPreference
12-
import app.revanced.patches.youtube.misc.extension.sharedExtensionPatch
13-
import app.revanced.patches.youtube.misc.settings.PreferenceScreen
14-
import app.revanced.patches.youtube.misc.settings.settingsPatch
15-
import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction
16-
import com.android.tools.smali.dexlib2.iface.instruction.TwoRegisterInstruction
174

18-
private const val EXTENSION_CLASS_DESCRIPTOR =
19-
"Lapp/revanced/extension/youtube/patches/RemoveTrackingQueryParameterPatch;"
20-
21-
val removeTrackingQueryParameterPatch = bytecodePatch(
22-
name = "Remove tracking query parameter",
23-
description = "Adds an option to remove the tracking parameter from links you share.",
24-
) {
25-
dependsOn(
26-
sharedExtensionPatch,
27-
settingsPatch,
28-
addResourcesPatch,
29-
)
30-
31-
compatibleWith(
32-
"com.google.android.youtube"(
33-
"19.34.42",
34-
"19.43.41",
35-
"20.07.39",
36-
"20.13.41",
37-
"20.14.43",
38-
)
39-
)
40-
41-
execute {
42-
addResources("youtube", "misc.privacy.removeTrackingQueryParameterPatch")
43-
44-
PreferenceScreen.MISC.addPreferences(
45-
SwitchPreference("revanced_remove_tracking_query_parameter"),
46-
)
47-
48-
fun Fingerprint.hook(
49-
getInsertIndex: Match.PatternMatch.() -> Int,
50-
getUrlRegister: MutableMethod.(insertIndex: Int) -> Int,
51-
) {
52-
val insertIndex = patternMatch!!.getInsertIndex()
53-
val urlRegister = method.getUrlRegister(insertIndex)
54-
55-
method.addInstructions(
56-
insertIndex,
57-
"""
58-
invoke-static {v$urlRegister}, $EXTENSION_CLASS_DESCRIPTOR->sanitize(Ljava/lang/String;)Ljava/lang/String;
59-
move-result-object v$urlRegister
60-
""",
61-
)
62-
}
63-
64-
// YouTube share sheet.\
65-
youtubeShareSheetFingerprint.hook(getInsertIndex = { startIndex + 1 }) { insertIndex ->
66-
getInstruction<OneRegisterInstruction>(insertIndex - 1).registerA
67-
}
68-
69-
// Native system share sheet.
70-
systemShareSheetFingerprint.hook(getInsertIndex = { endIndex }) { insertIndex ->
71-
getInstruction<OneRegisterInstruction>(insertIndex - 1).registerA
72-
}
73-
74-
copyTextFingerprint.hook(getInsertIndex = { startIndex + 2 }) { insertIndex ->
75-
getInstruction<TwoRegisterInstruction>(insertIndex - 2).registerA
76-
}
77-
}
5+
@Deprecated("Patch was renamed", ReplaceWith("sanitizeSharingLinksPatch"))
6+
@Suppress("unused")
7+
val removeTrackingQueryParameterPatch = bytecodePatch{
8+
dependsOn(sanitizeSharingLinksPatch)
789
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package app.revanced.patches.youtube.misc.privacy
2+
3+
import app.revanced.patches.shared.misc.privacy.sanitizeSharingLinksPatch
4+
import app.revanced.patches.youtube.misc.extension.sharedExtensionPatch
5+
import app.revanced.patches.youtube.misc.settings.PreferenceScreen
6+
import app.revanced.patches.youtube.misc.settings.settingsPatch
7+
8+
@Suppress("unused")
9+
val sanitizeSharingLinksPatch = sanitizeSharingLinksPatch(
10+
block = {
11+
dependsOn(
12+
sharedExtensionPatch,
13+
settingsPatch,
14+
)
15+
compatibleWith(
16+
"com.google.android.youtube"(
17+
"19.34.42",
18+
"19.43.41",
19+
"20.07.39",
20+
"20.13.41",
21+
"20.14.43",
22+
)
23+
)
24+
},
25+
preferenceScreen = PreferenceScreen.MISC
26+
)

0 commit comments

Comments
 (0)