From 13a138122875b30e82df68a680e413f6ad7ba382 Mon Sep 17 00:00:00 2001 From: KAZI MMT <82371061+kazimmt@users.noreply.github.com> Date: Thu, 9 Feb 2023 20:53:36 +0600 Subject: [PATCH] feat(reddit): `hide-subreddit-banner` patch --- .../annotations/HideBannerCompatibility.kt | 13 +++++ .../reddit/ad/banner/patch/HideBannerPatch.kt | 48 +++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 src/main/kotlin/app/revanced/patches/reddit/ad/banner/annotations/HideBannerCompatibility.kt create mode 100644 src/main/kotlin/app/revanced/patches/reddit/ad/banner/patch/HideBannerPatch.kt diff --git a/src/main/kotlin/app/revanced/patches/reddit/ad/banner/annotations/HideBannerCompatibility.kt b/src/main/kotlin/app/revanced/patches/reddit/ad/banner/annotations/HideBannerCompatibility.kt new file mode 100644 index 00000000..44af30fc --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/reddit/ad/banner/annotations/HideBannerCompatibility.kt @@ -0,0 +1,13 @@ +package app.revanced.patches.reddit.ad.banner.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 HideBannerCompatibility diff --git a/src/main/kotlin/app/revanced/patches/reddit/ad/banner/patch/HideBannerPatch.kt b/src/main/kotlin/app/revanced/patches/reddit/ad/banner/patch/HideBannerPatch.kt new file mode 100644 index 00000000..c6d06c80 --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/reddit/ad/banner/patch/HideBannerPatch.kt @@ -0,0 +1,48 @@ +package app.revanced.patches.reddit.ad.banner.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.banner.annotations.HideBannerCompatibility + +@Patch +@Name("hide-subreddit-banner") +@Description("Hides banner ads from comments on subreddits.") +@HideBannerCompatibility +@Version("0.0.1") +class HideBannerPatch : ResourcePatch { + override fun execute(context: ResourceContext): PatchResult { + + context.xmlEditor[RESOURCE_FILE_PATH].use { + it.file.getElementsByTagName("merge").item(0).childNodes.apply { + val attributes = arrayOf("height", "width") + + for (i in 1 until length) { + val view = item(i) + if ( + view.hasAttributes() && + view.attributes.getNamedItem("android:id").nodeValue.endsWith("ad_view_stub") + ) { + attributes.forEach { attribute -> + view.attributes.getNamedItem("android:layout_$attribute").nodeValue = "0.0dip" + } + + break + } + } + } + } + + return PatchResultSuccess() + } + + private companion object { + const val RESOURCE_FILE_PATH = "res/layout/merge_listheader_link_detail.xml" + } +} +