feat(YouTube - Hide video action buttons): Hide individual action buttons (#451)

This commit is contained in:
LisoUseInAIKyrios 2023-08-07 18:34:42 +04:00 committed by GitHub
parent c12f4a3871
commit 3e05724e64
9 changed files with 133 additions and 60 deletions

View File

@ -99,12 +99,12 @@ public final class AdsFilter extends Filter {
} }
@Override @Override
public boolean isFiltered(String path, @Nullable String identifier, byte[] protobufBufferArray, public boolean isFiltered(@Nullable String identifier, String path, byte[] protobufBufferArray,
FilterGroupList matchedList, FilterGroup matchedGroup, int matchedIndex) { FilterGroupList matchedList, FilterGroup matchedGroup, int matchedIndex) {
if (exceptions.matches(path)) if (exceptions.matches(path))
return false; return false;
return super.isFiltered(path, identifier, protobufBufferArray, matchedList, matchedGroup, matchedIndex); return super.isFiltered(identifier, path, protobufBufferArray, matchedList, matchedGroup, matchedIndex);
} }
/** /**

View File

@ -1,59 +1,113 @@
package app.revanced.integrations.patches.components; package app.revanced.integrations.patches.components;
import android.os.Build;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import app.revanced.integrations.settings.SettingsEnum; import app.revanced.integrations.settings.SettingsEnum;
@RequiresApi(api = Build.VERSION_CODES.N)
final class ButtonsFilter extends Filter { final class ButtonsFilter extends Filter {
private static final String VIDEO_ACTION_BAR_PATH = "video_action_bar.eml";
private final StringFilterGroup actionBarRule; private final StringFilterGroup actionBarRule;
private final StringFilterGroup bufferFilterPathRule;
private final ByteArrayFilterGroupList bufferButtonsGroupList = new ByteArrayFilterGroupList();
public ButtonsFilter() { public ButtonsFilter() {
actionBarRule = new StringFilterGroup( actionBarRule = new StringFilterGroup(
null, null,
"video_action_bar" VIDEO_ACTION_BAR_PATH
); );
identifierFilterGroups.addAll(actionBarRule);
bufferFilterPathRule = new StringFilterGroup(
null,
"|CellType|CollectionType|CellType|ContainerType|button.eml|"
);
pathFilterGroups.addAll( pathFilterGroups.addAll(
new StringFilterGroup( new StringFilterGroup(
SettingsEnum.HIDE_LIKE_DISLIKE_BUTTON, SettingsEnum.HIDE_LIKE_DISLIKE_BUTTON,
"|like_button", "|segmented_like_dislike_button"
"dislike_button"
), ),
new StringFilterGroup( new StringFilterGroup(
SettingsEnum.HIDE_DOWNLOAD_BUTTON, SettingsEnum.HIDE_DOWNLOAD_BUTTON,
"download_button" "|download_button.eml|"
), ),
new StringFilterGroup( new StringFilterGroup(
SettingsEnum.HIDE_PLAYLIST_BUTTON, SettingsEnum.HIDE_PLAYLIST_BUTTON,
"save_to_playlist_button" "|save_to_playlist_button"
), ),
new StringFilterGroup( new StringFilterGroup(
SettingsEnum.HIDE_CLIP_BUTTON, SettingsEnum.HIDE_CLIP_BUTTON,
"|clip_button.eml|" "|clip_button.eml|"
), ),
new StringFilterGroup( bufferFilterPathRule
SettingsEnum.HIDE_ACTION_BUTTONS, );
"ContainerType|video_action_button",
"|CellType|CollectionType|CellType|ContainerType|button.eml|" bufferButtonsGroupList.addAll(
new ByteArrayAsStringFilterGroup(
SettingsEnum.HIDE_LIVE_CHAT_BUTTON,
"yt_outline_message_bubble_overlap"
), ),
actionBarRule new ByteArrayAsStringFilterGroup(
SettingsEnum.HIDE_REPORT_BUTTON,
"yt_outline_flag"
),
new ByteArrayAsStringFilterGroup(
SettingsEnum.HIDE_SHARE_BUTTON,
"yt_outline_share"
),
new ByteArrayAsStringFilterGroup(
SettingsEnum.HIDE_REMIX_BUTTON,
"yt_outline_youtube_shorts_plus"
),
// Check for clip button both here and using a path filter,
// as there's a chance the path is a generic action button and won't contain 'clip_button'
new ByteArrayAsStringFilterGroup(
SettingsEnum.HIDE_CLIP_BUTTON,
"yt_outline_scissors"
),
new ByteArrayAsStringFilterGroup(
SettingsEnum.HIDE_SHOP_BUTTON,
"yt_outline_bag"
),
new ByteArrayAsStringFilterGroup(
SettingsEnum.HIDE_THANKS_BUTTON,
"yt_outline_dollar_sign_heart"
)
); );
} }
private boolean isEveryFilterGroupEnabled() { private boolean isEveryFilterGroupEnabled() {
for (StringFilterGroup rule : pathFilterGroups) for (FilterGroup rule : pathFilterGroups)
if (!rule.isEnabled()) return false;
for (FilterGroup rule : bufferButtonsGroupList)
if (!rule.isEnabled()) return false; if (!rule.isEnabled()) return false;
return true; return true;
} }
@Override @Override
public boolean isFiltered(String path, @Nullable String identifier, byte[] protobufBufferArray, public boolean isFiltered(@Nullable String identifier, String path, byte[] protobufBufferArray,
FilterGroupList matchedList, FilterGroup matchedGroup, int matchedIndex) { FilterGroupList matchedList, FilterGroup matchedGroup, int matchedIndex) {
if (matchedGroup == actionBarRule) { if (matchedGroup == actionBarRule) {
return isEveryFilterGroupEnabled(); if (!isEveryFilterGroupEnabled()) {
return false;
}
} else if (matchedGroup == bufferFilterPathRule) {
if (!path.startsWith(VIDEO_ACTION_BAR_PATH)) {
return false; // Some other unknown button and not part of the player action buttons.
}
if (!bufferButtonsGroupList.check(protobufBufferArray).isFiltered()) {
return false; // Action button is not set to hide.
}
} }
return super.isFiltered(path, identifier, protobufBufferArray, matchedList, matchedGroup, matchedIndex); return super.isFiltered(identifier, path, protobufBufferArray, matchedList, matchedGroup, matchedIndex);
} }
} }

View File

@ -172,12 +172,12 @@ public final class LayoutComponentsFilter extends Filter {
} }
@Override @Override
public boolean isFiltered(String path, @Nullable String identifier, byte[] protobufBufferArray, public boolean isFiltered(@Nullable String identifier, String path, byte[] protobufBufferArray,
FilterGroupList matchedList, FilterGroup matchedGroup, int matchedIndex) { FilterGroupList matchedList, FilterGroup matchedGroup, int matchedIndex) {
if (matchedGroup != custom && exceptions.matches(path)) if (matchedGroup != custom && exceptions.matches(path))
return false; // Exceptions are not filtered. return false; // Exceptions are not filtered.
return super.isFiltered(path, identifier, protobufBufferArray, matchedList, matchedGroup, matchedIndex); return super.isFiltered(identifier, path, protobufBufferArray, matchedList, matchedGroup, matchedIndex);
} }

View File

@ -1,16 +1,26 @@
package app.revanced.integrations.patches.components; package app.revanced.integrations.patches.components;
import android.os.Build; import android.os.Build;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi; import androidx.annotation.RequiresApi;
import app.revanced.integrations.settings.SettingsEnum;
import app.revanced.integrations.utils.*;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.*; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Spliterator;
import java.util.function.Consumer; import java.util.function.Consumer;
import app.revanced.integrations.settings.SettingsEnum;
import app.revanced.integrations.utils.ByteTrieSearch;
import app.revanced.integrations.utils.LogHelper;
import app.revanced.integrations.utils.ReVancedUtils;
import app.revanced.integrations.utils.StringTrieSearch;
import app.revanced.integrations.utils.TrieSearch;
abstract class FilterGroup<T> { abstract class FilterGroup<T> {
final static class FilterGroupResult { final static class FilterGroupResult {
SettingsEnum setting; SettingsEnum setting;
@ -81,8 +91,7 @@ class StringFilterGroup extends FilterGroup<String> {
@Override @Override
public FilterGroupResult check(final String string) { public FilterGroupResult check(final String string) {
return new FilterGroupResult(setting, return new FilterGroupResult(setting, isEnabled() && ReVancedUtils.containsAny(string, filters));
(setting == null || setting.getBoolean()) && ReVancedUtils.containsAny(string, filters));
} }
} }
@ -276,23 +285,12 @@ abstract class Filter {
protected final StringFilterGroupList pathFilterGroups = new StringFilterGroupList(); protected final StringFilterGroupList pathFilterGroups = new StringFilterGroupList();
protected final StringFilterGroupList identifierFilterGroups = new StringFilterGroupList(); protected final StringFilterGroupList identifierFilterGroups = new StringFilterGroupList();
/**
* A collection of {@link ByteArrayFilterGroup} that are always searched for (no matter what).
*
* If possible, avoid adding values to this list and instead use a path or identifier filter
* for the item you are looking for. Then inside
* {@link #isFiltered(String, String, byte[], FilterGroupList, FilterGroup, int)},
* the buffer can then be searched using using a different
* {@link ByteArrayFilterGroupList} or a {@link ByteArrayFilterGroup}.
* This way, the expensive buffer searching only occurs if the cheap and fast path/identifier is already found.
*/
protected final ByteArrayFilterGroupList protobufBufferFilterGroups = new ByteArrayFilterGroupList();
/** /**
* Called after an enabled filter has been matched. * Called after an enabled filter has been matched.
* Default implementation is to always filter the matched item. * Default implementation is to always filter the matched item.
* Subclasses can perform additional or different checks if needed. * Subclasses can perform additional or different checks if needed.
* * <p>
* Method is called off the main thread. * Method is called off the main thread.
* *
* @param matchedList The list the group filter belongs to. * @param matchedList The list the group filter belongs to.
@ -301,15 +299,13 @@ abstract class Filter {
* @return True if the litho item should be filtered out. * @return True if the litho item should be filtered out.
*/ */
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
boolean isFiltered(String path, @Nullable String identifier, byte[] protobufBufferArray, boolean isFiltered(@Nullable String identifier, String path, byte[] protobufBufferArray,
FilterGroupList matchedList, FilterGroup matchedGroup, int matchedIndex) { FilterGroupList matchedList, FilterGroup matchedGroup, int matchedIndex) {
if (SettingsEnum.DEBUG.getBoolean()) { if (SettingsEnum.DEBUG.getBoolean()) {
if (pathFilterGroups == matchedList) { if (pathFilterGroups == matchedList) {
LogHelper.printDebug(() -> getClass().getSimpleName() + " Filtered path: " + path); LogHelper.printDebug(() -> getClass().getSimpleName() + " Filtered path: " + path);
} else if (identifierFilterGroups == matchedList) { } else if (identifierFilterGroups == matchedList) {
LogHelper.printDebug(() -> getClass().getSimpleName() + " Filtered identifier: " + identifier); LogHelper.printDebug(() -> getClass().getSimpleName() + " Filtered identifier: " + identifier);
} else if (protobufBufferFilterGroups == matchedList) {
LogHelper.printDebug(() -> getClass().getSimpleName() + " Filtered from protobuf-buffer");
} }
} }
return true; return true;
@ -323,13 +319,14 @@ public final class LithoFilterPatch {
* Simple wrapper to pass the litho parameters through the prefix search. * Simple wrapper to pass the litho parameters through the prefix search.
*/ */
private static final class LithoFilterParameters { private static final class LithoFilterParameters {
final String path; @Nullable
final String identifier; final String identifier;
final String path;
final byte[] protoBuffer; final byte[] protoBuffer;
LithoFilterParameters(StringBuilder lithoPath, String lithoIdentifier, ByteBuffer protoBuffer) { LithoFilterParameters(@Nullable String lithoIdentifier, StringBuilder lithoPath, ByteBuffer protoBuffer) {
this.path = lithoPath.toString();
this.identifier = lithoIdentifier; this.identifier = lithoIdentifier;
this.path = lithoPath.toString();
this.protoBuffer = protoBuffer.array(); this.protoBuffer = protoBuffer.array();
} }
@ -367,7 +364,9 @@ public final class LithoFilterPatch {
int value = buffer[end]; int value = buffer[end];
if (value < minimumAscii || value > maximumAscii || end == length - 1) { if (value < minimumAscii || value > maximumAscii || end == length - 1) {
if (end - start >= minimumAsciiStringLength) { if (end - start >= minimumAsciiStringLength) {
builder.append(new String(buffer, start, end - start)); for (int i = start; i < end; i++) {
builder.append((char) buffer[i]);
}
builder.append(delimitingCharacter); builder.append(delimitingCharacter);
} }
start = end + 1; start = end + 1;
@ -383,22 +382,24 @@ public final class LithoFilterPatch {
private static final StringTrieSearch pathSearchTree = new StringTrieSearch(); private static final StringTrieSearch pathSearchTree = new StringTrieSearch();
private static final StringTrieSearch identifierSearchTree = new StringTrieSearch(); private static final StringTrieSearch identifierSearchTree = new StringTrieSearch();
private static final ByteTrieSearch protoSearchTree = new ByteTrieSearch();
/**
* Because litho filtering is multi-threaded and the buffer is passed in from a different injection point,
* the buffer is saved to a ThreadLocal so each calling thread does not interfere with other threads.
*/
private static final ThreadLocal<ByteBuffer> bufferThreadLocal = new ThreadLocal<>();
static { static {
for (Filter filter : filters) { for (Filter filter : filters) {
filterGroupLists(pathSearchTree, filter, filter.pathFilterGroups); filterGroupLists(pathSearchTree, filter, filter.pathFilterGroups);
filterGroupLists(identifierSearchTree, filter, filter.identifierFilterGroups); filterGroupLists(identifierSearchTree, filter, filter.identifierFilterGroups);
filterGroupLists(protoSearchTree, filter, filter.protobufBufferFilterGroups);
} }
LogHelper.printDebug(() -> "Using: " LogHelper.printDebug(() -> "Using: "
+ pathSearchTree.numberOfPatterns() + " path filters" + pathSearchTree.numberOfPatterns() + " path filters"
+ " (" + pathSearchTree.getEstimatedMemorySize() + " KB), " + " (" + pathSearchTree.getEstimatedMemorySize() + " KB), "
+ identifierSearchTree.numberOfPatterns() + " identifier filters" + identifierSearchTree.numberOfPatterns() + " identifier filters"
+ " (" + identifierSearchTree.getEstimatedMemorySize() + " KB), " + " (" + identifierSearchTree.getEstimatedMemorySize() + " KB)");
+ protoSearchTree.numberOfPatterns() + " buffer filters"
+ " (" + protoSearchTree.getEstimatedMemorySize() + " KB)");
} }
private static <T> void filterGroupLists(TrieSearch<T> pathSearchTree, private static <T> void filterGroupLists(TrieSearch<T> pathSearchTree,
@ -411,7 +412,7 @@ public final class LithoFilterPatch {
pathSearchTree.addPattern(pattern, (textSearched, matchedStartIndex, callbackParameter) -> { pathSearchTree.addPattern(pattern, (textSearched, matchedStartIndex, callbackParameter) -> {
if (!group.isEnabled()) return false; if (!group.isEnabled()) return false;
LithoFilterParameters parameters = (LithoFilterParameters) callbackParameter; LithoFilterParameters parameters = (LithoFilterParameters) callbackParameter;
return filter.isFiltered(parameters.path, parameters.identifier, parameters.protoBuffer, return filter.isFiltered(parameters.identifier, parameters.path, parameters.protoBuffer,
list, group, matchedStartIndex); list, group, matchedStartIndex);
} }
); );
@ -423,23 +424,36 @@ public final class LithoFilterPatch {
* Injection point. Called off the main thread. * Injection point. Called off the main thread.
*/ */
@SuppressWarnings("unused") @SuppressWarnings("unused")
public static boolean filter(@NonNull StringBuilder pathBuilder, @Nullable String lithoIdentifier, public static void setProtoBuffer(@NonNull ByteBuffer protobufBuffer) {
@NonNull ByteBuffer protobufBuffer) { bufferThreadLocal.set(protobufBuffer);
}
/**
* Injection point. Called off the main thread, and commonly called by multiple threads at the same time.
*/
@SuppressWarnings("unused")
public static boolean filter(@Nullable String lithoIdentifier, @NonNull StringBuilder pathBuilder) {
try { try {
// It is assumed that protobufBuffer is empty as well in this case. // It is assumed that protobufBuffer is empty as well in this case.
if (pathBuilder.length() == 0) if (pathBuilder.length() == 0)
return false; return false;
LithoFilterParameters parameter = new LithoFilterParameters(pathBuilder, lithoIdentifier, protobufBuffer); ByteBuffer protobufBuffer = bufferThreadLocal.get();
if (protobufBuffer == null) {
LogHelper.printException(() -> "Proto buffer is null"); // Should never happen
return false;
}
LithoFilterParameters parameter = new LithoFilterParameters(lithoIdentifier, pathBuilder, protobufBuffer);
LogHelper.printDebug(() -> "Searching " + parameter); LogHelper.printDebug(() -> "Searching " + parameter);
if (pathSearchTree.matches(parameter.path, parameter)) return true;
if (parameter.identifier != null) { if (parameter.identifier != null) {
if (identifierSearchTree.matches(parameter.identifier, parameter)) return true; if (identifierSearchTree.matches(parameter.identifier, parameter)) return true;
} }
if (protoSearchTree.matches(parameter.protoBuffer, parameter)) return true; if (pathSearchTree.matches(parameter.path, parameter)) return true;
} catch (Exception ex) { } catch (Exception ex) {
LogHelper.printException(() -> "Litho filter failure", ex); LogHelper.printException(() -> "Litho filter failure", ex);
} finally {
bufferThreadLocal.remove(); // Cleanup and remove the buffer.
} }
return false; return false;

View File

@ -15,7 +15,7 @@ public final class PlaybackSpeedMenuFilterPatch extends Filter {
} }
@Override @Override
boolean isFiltered(String path, @Nullable String identifier, byte[] protobufBufferArray, boolean isFiltered(@Nullable String identifier, String path, byte[] protobufBufferArray,
FilterGroupList matchedList, FilterGroup matchedGroup, int matchedIndex) { FilterGroupList matchedList, FilterGroup matchedGroup, int matchedIndex) {
isPlaybackSpeedMenuVisible = true; isPlaybackSpeedMenuVisible = true;

View File

@ -62,12 +62,12 @@ public class PlayerFlyoutMenuItemsFilter extends Filter {
} }
@Override @Override
boolean isFiltered(String path, @Nullable String identifier, byte[] protobufBufferArray, boolean isFiltered(@Nullable String identifier, String path, byte[] protobufBufferArray,
FilterGroupList matchedList, FilterGroup matchedGroup, int matchedIndex) { FilterGroupList matchedList, FilterGroup matchedGroup, int matchedIndex) {
// Only 1 group is added to the parent class, so the matched group must be the overflow menu. // Only 1 group is added to the parent class, so the matched group must be the overflow menu.
if (matchedIndex == 0 && flyoutFilterGroupList.check(protobufBufferArray).isFiltered()) { if (matchedIndex == 0 && flyoutFilterGroupList.check(protobufBufferArray).isFiltered()) {
// Super class handles logging. // Super class handles logging.
return super.isFiltered(path, identifier, protobufBufferArray, matchedList, matchedGroup, matchedIndex); return super.isFiltered(identifier, path, protobufBufferArray, matchedList, matchedGroup, matchedIndex);
} }
return false; return false;
} }

View File

@ -64,7 +64,7 @@ public final class ShortsFilter extends Filter {
} }
@Override @Override
boolean isFiltered(String path, @Nullable String identifier, byte[] protobufBufferArray, boolean isFiltered(@Nullable String identifier, String path, byte[] protobufBufferArray,
FilterGroupList matchedList, FilterGroup matchedGroup, int matchedIndex) { FilterGroupList matchedList, FilterGroup matchedGroup, int matchedIndex) {
if (matchedGroup == soundButton || matchedGroup == infoPanel || matchedGroup == channelBar) return true; if (matchedGroup == soundButton || matchedGroup == infoPanel || matchedGroup == channelBar) return true;

View File

@ -17,7 +17,7 @@ public final class VideoQualityMenuFilterPatch extends Filter {
} }
@Override @Override
boolean isFiltered(String path, @Nullable String identifier, byte[] protobufBufferArray, boolean isFiltered(@Nullable String identifier, String path, byte[] protobufBufferArray,
FilterGroupList matchedList, FilterGroup matchedGroup, int matchedIndex) { FilterGroupList matchedList, FilterGroup matchedGroup, int matchedIndex) {
isVideoQualityMenuVisible = true; isVideoQualityMenuVisible = true;

View File

@ -89,10 +89,15 @@ public enum SettingsEnum {
// Action buttons // Action buttons
HIDE_LIKE_DISLIKE_BUTTON("revanced_hide_like_dislike_button", BOOLEAN, FALSE), HIDE_LIKE_DISLIKE_BUTTON("revanced_hide_like_dislike_button", BOOLEAN, FALSE),
HIDE_LIVE_CHAT_BUTTON("revanced_hide_live_chat_button", BOOLEAN, FALSE),
HIDE_SHARE_BUTTON("revanced_hide_share_button", BOOLEAN, FALSE),
HIDE_REPORT_BUTTON("revanced_hide_report_button", BOOLEAN, FALSE),
HIDE_REMIX_BUTTON("revanced_hide_remix_button", BOOLEAN, TRUE),
HIDE_DOWNLOAD_BUTTON("revanced_hide_download_button", BOOLEAN, FALSE), HIDE_DOWNLOAD_BUTTON("revanced_hide_download_button", BOOLEAN, FALSE),
HIDE_THANKS_BUTTON("revanced_hide_thanks_button", BOOLEAN, TRUE),
HIDE_CLIP_BUTTON("revanced_hide_clip_button", BOOLEAN, TRUE),
HIDE_PLAYLIST_BUTTON("revanced_hide_playlist_button", BOOLEAN, FALSE), HIDE_PLAYLIST_BUTTON("revanced_hide_playlist_button", BOOLEAN, FALSE),
HIDE_CLIP_BUTTON("revanced_hide_clip_button", BOOLEAN, FALSE, "revanced_hide_clip_button_user_dialog_message"), HIDE_SHOP_BUTTON("revanced_hide_shop_button", BOOLEAN, TRUE),
HIDE_ACTION_BUTTONS("revanced_hide_action_buttons", BOOLEAN, FALSE),
// Layout // Layout
DISABLE_RESUMING_SHORTS_PLAYER("revanced_disable_resuming_shorts_player", BOOLEAN, FALSE), DISABLE_RESUMING_SHORTS_PLAYER("revanced_disable_resuming_shorts_player", BOOLEAN, FALSE),