Skip to content

Commit 5484625

Browse files
Leo40Gitnyraa
andauthored
fix(X / Twitter - Change Link Sharing Domain): Change link domain of share copy action (#6091)
Co-authored-by: nyraa <[email protected]>
1 parent a98e8f7 commit 5484625

File tree

3 files changed

+52
-66
lines changed

3 files changed

+52
-66
lines changed

extensions/twitter/src/main/java/app/revanced/twitter/patches/links/ChangeLinkSharingDomainPatch.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,19 @@
22

33
@SuppressWarnings("unused")
44
public final class ChangeLinkSharingDomainPatch {
5-
private static final String DOMAIN_NAME = "https://fxtwitter.com";
65
private static final String LINK_FORMAT = "%s/%s/status/%s";
76

87
/**
9-
* Injection point.
8+
* Method is modified during patching. Do not change.
109
*/
11-
public static String formatResourceLink(Object... formatArgs) {
12-
String username = (String) formatArgs[0];
13-
String tweetId = (String) formatArgs[1];
14-
return String.format(LINK_FORMAT, DOMAIN_NAME, username, tweetId);
10+
private static String getShareDomain() {
11+
return "";
1512
}
1613

1714
/**
1815
* Injection point.
1916
*/
2017
public static String formatLink(long tweetId, String username) {
21-
return String.format(LINK_FORMAT, DOMAIN_NAME, username, tweetId);
18+
return String.format(LINK_FORMAT, getShareDomain(), username, tweetId);
2219
}
2320
}
Lines changed: 44 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,60 @@
11
package app.revanced.patches.twitter.misc.links
22

33
import app.revanced.patcher.extensions.InstructionExtensions.addInstructions
4-
import app.revanced.patcher.extensions.InstructionExtensions.getInstruction
5-
import app.revanced.patcher.extensions.InstructionExtensions.replaceInstruction
64
import app.revanced.patcher.patch.bytecodePatch
75
import app.revanced.patcher.patch.resourcePatch
86
import app.revanced.patcher.patch.stringOption
97
import app.revanced.patches.shared.PATCH_DESCRIPTION_CHANGE_LINK_SHARING_DOMAIN
108
import app.revanced.patches.shared.PATCH_NAME_CHANGE_LINK_SHARING_DOMAIN
11-
import app.revanced.patches.shared.misc.mapping.get
12-
import app.revanced.patches.shared.misc.mapping.resourceMappingPatch
13-
import app.revanced.patches.shared.misc.mapping.resourceMappings
149
import app.revanced.patches.twitter.misc.extension.sharedExtensionPatch
15-
import app.revanced.util.indexOfFirstLiteralInstructionOrThrow
16-
import com.android.tools.smali.dexlib2.iface.instruction.FiveRegisterInstruction
17-
import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction
10+
import app.revanced.util.findElementByAttributeValueOrThrow
11+
import app.revanced.util.returnEarly
12+
import java.net.InetAddress
13+
import java.net.UnknownHostException
14+
import java.util.logging.Logger
1815

19-
internal var tweetShareLinkTemplateId = -1L
20-
private set
16+
internal const val EXTENSION_CLASS_DESCRIPTOR = "Lapp/revanced/twitter/patches/links/ChangeLinkSharingDomainPatch;"
2117

22-
internal val changeLinkSharingDomainResourcePatch = resourcePatch {
23-
dependsOn(resourceMappingPatch)
18+
internal val domainNameOption by stringOption(
19+
key = "domainName",
20+
default = "https://fxtwitter.com",
21+
title = "Domain name",
22+
description = "The domain name to use when sharing links.",
23+
required = true,
24+
) {
25+
// Do a courtesy check if the host can be resolved.
26+
// If it does not resolve, then print a warning but use the host anyway.
27+
// Unresolvable hosts should not be rejected, since the patching environment
28+
// may not allow network connections or the network may be down.
29+
try {
30+
InetAddress.getByName(it)
31+
} catch (e: UnknownHostException) {
32+
Logger.getLogger(this::class.java.name).warning(
33+
"Host \"$it\" did not resolve to any domain."
34+
)
35+
}
36+
true
37+
}
2438

39+
internal val changeLinkSharingDomainResourcePatch = resourcePatch {
2540
execute {
26-
tweetShareLinkTemplateId = resourceMappings["string", "tweet_share_link"]
41+
val domainName = domainNameOption!!
42+
43+
val shareLinkTemplate = if (domainName.endsWith("/")) {
44+
"$domainName%1\$s/status/%2\$s"
45+
} else {
46+
"$domainName/%1\$s/status/%2\$s"
47+
}
48+
49+
document("res/values/strings.xml").use { document ->
50+
document.documentElement.childNodes.findElementByAttributeValueOrThrow(
51+
"name",
52+
"tweet_share_link"
53+
).textContent = shareLinkTemplate
54+
}
2755
}
2856
}
2957

30-
private const val EXTENSION_CLASS_DESCRIPTOR = "Lapp/revanced/twitter/patches/links/ChangeLinkSharingDomainPatch;"
31-
3258
@Suppress("unused")
3359
val changeLinkSharingDomainPatch = bytecodePatch(
3460
name = PATCH_NAME_CHANGE_LINK_SHARING_DOMAIN,
@@ -46,26 +72,11 @@ val changeLinkSharingDomainPatch = bytecodePatch(
4672
)
4773
)
4874

49-
val domainName by stringOption(
50-
key = "domainName",
51-
default = "fxtwitter.com",
52-
title = "Domain name",
53-
description = "The domain name to use when sharing links.",
54-
required = true,
55-
)
56-
5775
execute {
58-
linkSharingDomainFingerprint.let {
59-
val replacementIndex = it.stringMatches!!.first().index
60-
val domainRegister = it.method.getInstruction<OneRegisterInstruction>(
61-
replacementIndex
62-
).registerA
76+
val domainName = domainNameOption!!
6377

64-
it.method.replaceInstruction(
65-
replacementIndex,
66-
"const-string v$domainRegister, \"https://$domainName\"",
67-
)
68-
}
78+
// Replace the domain name in the link sharing extension methods.
79+
linkSharingDomainHelperFingerprint.method.returnEarly(domainName)
6980

7081
// Replace the domain name when copying a link with "Copy link" button.
7182
linkBuilderFingerprint.method.addInstructions(
@@ -76,20 +87,5 @@ val changeLinkSharingDomainPatch = bytecodePatch(
7687
return-object p0
7788
"""
7889
)
79-
80-
// Used in the Share via... dialog.
81-
linkResourceGetterFingerprint.method.apply {
82-
val templateIdConstIndex = indexOfFirstLiteralInstructionOrThrow(tweetShareLinkTemplateId)
83-
84-
// Format the link with the new domain name register (1 instruction below the const).
85-
val formatLinkCallIndex = templateIdConstIndex + 1
86-
val register = getInstruction<FiveRegisterInstruction>(formatLinkCallIndex).registerE
87-
88-
// Replace the original method call with the new method call.
89-
replaceInstruction(
90-
formatLinkCallIndex,
91-
"invoke-static { v$register }, $EXTENSION_CLASS_DESCRIPTOR->formatResourceLink([Ljava/lang/Object;)Ljava/lang/String;",
92-
)
93-
}
9490
}
9591
}
Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package app.revanced.patches.twitter.misc.links
22

33
import app.revanced.patcher.fingerprint
4-
import app.revanced.util.literal
5-
import com.android.tools.smali.dexlib2.AccessFlags
64

75
internal val openLinkFingerprint = fingerprint {
86
returns("V")
@@ -19,13 +17,8 @@ internal val linkBuilderFingerprint = fingerprint {
1917
strings("/%1\$s/status/%2\$d")
2018
}
2119

22-
// Gets Resource string for share link view available by pressing "Share via" button.
23-
internal val linkResourceGetterFingerprint = fingerprint {
24-
accessFlags(AccessFlags.PUBLIC, AccessFlags.FINAL)
25-
parameters("Landroid/content/res/Resources;")
26-
literal { tweetShareLinkTemplateId }
27-
}
28-
29-
internal val linkSharingDomainFingerprint = fingerprint {
30-
strings("https://fxtwitter.com")
20+
internal val linkSharingDomainHelperFingerprint = fingerprint {
21+
custom { method, classDef ->
22+
method.name == "getShareDomain" && classDef.type == EXTENSION_CLASS_DESCRIPTOR
23+
}
3124
}

0 commit comments

Comments
 (0)