Skip to content

Commit

Permalink
feat(reddit): layout-reddit-ads patch
Browse files Browse the repository at this point in the history
  • Loading branch information
inotia00 committed Feb 6, 2023
1 parent ef9f433 commit 5816679
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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"
}
}

2 comments on commit 5816679

@inotia00
Copy link
Owner Author

@inotia00 inotia00 commented on 5816679 Feb 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If someone wants to use this to leave a PR on the official repo, feel free to use it as is.
(because this fork repo is under the GPL v3)

If you do, please add some code comments or minor fixes.

@raziel6868
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If someone wants to use this to leave a PR on the official repo, feel free to use it as is. (because this fork repo is under the GPL v3)

If you do, please add some code comments or minor fixes.

OMG :)

Please sign in to comment.