diff --git a/Sources/uYouPlus.xm b/Sources/uYouPlus.xm index 07786438a1..5bd4753719 100644 --- a/Sources/uYouPlus.xm +++ b/Sources/uYouPlus.xm @@ -197,28 +197,47 @@ NSString *getAdString(NSString *description) { return @"video_display_full_buttoned_layout"; return nil; } -%hook YTInnerTubeCollectionViewController -- (void)addSectionsFromArray:(NSArray *)array { +static BOOL isAdRenderer(YTIElementRenderer *elementRenderer, int kind) { + if ([elementRenderer respondsToSelector:@selector(hasCompatibilityOptions)] && elementRenderer.hasCompatibilityOptions && elementRenderer.compatibilityOptions.hasAdLoggingData) { + HBLogDebug(@"YTX adLogging %d %@", kind, elementRenderer); + return YES; + } + NSString *description = [elementRenderer description]; + NSString *adString = getAdString(description); + if (adString) { + HBLogDebug(@"YTX getAdString %d %@ %@", kind, adString, elementRenderer); + return YES; + } + return NO; +} +static NSMutableArray *filteredArray(NSArray *array) { NSMutableArray *newArray = [array mutableCopy]; NSIndexSet *removeIndexes = [newArray indexesOfObjectsPassingTest:^BOOL(YTIItemSectionRenderer *sectionRenderer, NSUInteger idx, BOOL *stop) { if (![sectionRenderer isKindOfClass:%c(YTIItemSectionRenderer)]) return NO; - YTIItemSectionSupportedRenderers *firstObject = [sectionRenderer.contentsArray firstObject]; - YTIElementRenderer *elementRenderer = firstObject.elementRenderer; - if ([elementRenderer respondsToSelector:@selector(hasCompatibilityOptions)] && elementRenderer.hasCompatibilityOptions && elementRenderer.compatibilityOptions.hasAdLoggingData) { - HBLogDebug(@"YTX adLogging 2 %@", elementRenderer); - return YES; + NSMutableArray *contentsArray = sectionRenderer.contentsArray; + if (contentsArray.count > 1) { + NSIndexSet *removeContentsArrayIndexes = [contentsArray indexesOfObjectsPassingTest:^BOOL(YTIItemSectionSupportedRenderers *sectionSupportedRenderers, NSUInteger idx2, BOOL *stop2) { + YTIElementRenderer *elementRenderer = sectionSupportedRenderers.elementRenderer; + return isAdRenderer(elementRenderer, 3); + }]; + [contentsArray removeObjectsAtIndexes:removeContentsArrayIndexes]; } - NSString *description = [elementRenderer description]; - NSString *adString = getAdString(description); - if (adString) { - HBLogDebug(@"YTX getAdString 2 %@ %@", adString, elementRenderer); - return YES; - } - return NO; + YTIItemSectionSupportedRenderers *firstObject = [contentsArray firstObject]; + YTIElementRenderer *elementRenderer = firstObject.elementRenderer; + return isAdRenderer(elementRenderer, 2); }]; [newArray removeObjectsAtIndexes:removeIndexes]; - %orig(newArray.copy); + return newArray; +} +%hook YTInnerTubeCollectionViewController +- (void)displaySectionsWithReloadingSectionControllerByRenderer:(id)renderer { + NSMutableArray *sectionRenderers = [self valueForKey:@"_sectionRenderers"]; + [self setValue:filteredArray(sectionRenderers) forKey:@"_sectionRenderers"]; + %orig; +} +- (void)addSectionsFromArray:(NSArray *)array { + %orig(filteredArray(array)); } %end %end