From 5816679e3eb5c887c486a177b6cfa08948219211 Mon Sep 17 00:00:00 2001 From: inotia00 Date: Tue, 7 Feb 2023 07:51:40 +0900 Subject: [PATCH] feat(reddit): `layout-reddit-ads` patch --- .../annotations/LayoutAdsCompatibility.kt | 13 ++++++ .../reddit/ad/layout/patch/LayoutAdsPatch.kt | 43 +++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 src/main/kotlin/app/revanced/patches/reddit/ad/layout/annotations/LayoutAdsCompatibility.kt create mode 100644 src/main/kotlin/app/revanced/patches/reddit/ad/layout/patch/LayoutAdsPatch.kt diff --git a/src/main/kotlin/app/revanced/patches/reddit/ad/layout/annotations/LayoutAdsCompatibility.kt b/src/main/kotlin/app/revanced/patches/reddit/ad/layout/annotations/LayoutAdsCompatibility.kt new file mode 100644 index 0000000000..ad2317f31e --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/reddit/ad/layout/annotations/LayoutAdsCompatibility.kt @@ -0,0 +1,13 @@ +package app.revanced.patches.reddit.ad.layout.annotations + +import app.revanced.patcher.annotation.Compatibility +import app.revanced.patcher.annotation.Package + +@Compatibility( + [Package( + "com.reddit.frontpage", arrayOf("2023.05.0") + )] +) +@Target(AnnotationTarget.CLASS) +@Retention(AnnotationRetention.RUNTIME) +internal annotation class LayoutAdsCompatibility diff --git a/src/main/kotlin/app/revanced/patches/reddit/ad/layout/patch/LayoutAdsPatch.kt b/src/main/kotlin/app/revanced/patches/reddit/ad/layout/patch/LayoutAdsPatch.kt new file mode 100644 index 0000000000..6d5d3cdd8a --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/reddit/ad/layout/patch/LayoutAdsPatch.kt @@ -0,0 +1,43 @@ +package app.revanced.patches.reddit.ad.layout.patch + +import app.revanced.patcher.annotation.Description +import app.revanced.patcher.annotation.Name +import app.revanced.patcher.annotation.Version +import app.revanced.patcher.data.ResourceContext +import app.revanced.patcher.patch.PatchResult +import app.revanced.patcher.patch.PatchResultSuccess +import app.revanced.patcher.patch.ResourcePatch +import app.revanced.patcher.patch.annotations.Patch +import app.revanced.patches.reddit.ad.layout.annotations.LayoutAdsCompatibility + +@Patch +@Name("layout-reddit-ads") +@Description("Removes layout ads from the Reddit frontpage and subreddits.") +@LayoutAdsCompatibility +@Version("0.0.1") +class LayoutAdsPatch : ResourcePatch { + override fun execute(context: ResourceContext): PatchResult { + arrayOf( + "height", + "width" + ).forEach { replacements -> + context.xmlEditor[RESOURCE_FILE_PATH].use { + val children = it.file.getElementsByTagName("merge").item(0).childNodes + + for (i in 1 until children.length) { + val view = children.item(i) + if (!(view.hasAttributes() && view.attributes.getNamedItem("android:id").nodeValue.endsWith("ad_view_stub"))) continue + view.attributes.getNamedItem("android:layout_$replacements").nodeValue = "0.0dip" + break + } + } + } + + return PatchResultSuccess() + } + + private companion object { + const val RESOURCE_FILE_PATH = "res/layout/merge_listheader_link_detail.xml" + } +} +