Skip to content

Commit

Permalink
chore: finishing touches for 0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
castdrian committed Oct 27, 2024
1 parent e8732a8 commit 661057e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 42 deletions.
2 changes: 1 addition & 1 deletion Tweak.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ NS_ASSUME_NONNULL_BEGIN
- (void)addAction:(YTActionSheetAction *)action;
- (void)dismiss;
- (id)valueForKey:(NSString *)key;
- (UIImage *)createBlockIconWithOriginalAction:(YTActionSheetAction *)originalAction;
- (UIImage *)createBlockIconWithOriginalAction:(nullable YTActionSheetAction *)originalAction;
- (UIViewController *)findViewControllerForView:(UIView *)view;
- (void)extractChannelNameFromNode:(id)node completion:(void (^)(NSString *channelName))completion;
@end
Expand Down
85 changes: 44 additions & 41 deletions Tweak.x
Original file line number Diff line number Diff line change
Expand Up @@ -115,51 +115,54 @@
}

- (void)addAction:(YTActionSheetAction *)action {
if ([action.title isEqualToString:@"Play next in queue"]) {
__weak typeof(self) weakSelf = self;

UIImage *blockIcon = [self createBlockIconWithOriginalAction:action];
NSInteger style = 0;

YTActionSheetAction *blockChannelAction = [%c(YTActionSheetAction) actionWithTitle:@"Block Channel"
iconImage:blockIcon
style:style
handler:^(YTActionSheetAction *action) {
__strong typeof(self) strongSelf = weakSelf;
@try {
UIView *sourceView = [strongSelf valueForKey:@"sourceView"];
id node = [sourceView valueForKey:@"asyncdisplaykit_node"];

NSString *debugDescription = [node debugDescription];
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"cellNode = <YTVideoWithContextNode: (0x[0-9a-f]+)>" options:0 error:nil];
NSTextCheckingResult *match = [regex firstMatchInString:debugDescription options:0 range:NSMakeRange(0, debugDescription.length)];
%orig;

static void *blockActionKey = &blockActionKey;
if (objc_getAssociatedObject(self, blockActionKey)) {
return;
}

__weak typeof(self) weakSelf = self;
UIImage *blockIcon = [self createBlockIconWithOriginalAction:nil];

YTActionSheetAction *blockChannelAction = [%c(YTActionSheetAction) actionWithTitle:@"Block Channel"
iconImage:blockIcon
style:0
handler:^(YTActionSheetAction *action) {
__strong typeof(self) strongSelf = weakSelf;
@try {
UIView *sourceView = [strongSelf valueForKey:@"sourceView"];
id node = [sourceView valueForKey:@"asyncdisplaykit_node"];

NSString *debugDescription = [node debugDescription];
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"cellNode = <YTVideoWithContextNode: (0x[0-9a-f]+)>" options:0 error:nil];
NSTextCheckingResult *match = [regex firstMatchInString:debugDescription options:0 range:NSMakeRange(0, debugDescription.length)];

if (match) {
NSString *address = [debugDescription substringWithRange:[match rangeAtIndex:1]];
void *videoNodePtr;
sscanf([address UTF8String], "%p", &videoNodePtr);
id videoNode = (__bridge id)videoNodePtr;

if (match) {
NSString *address = [debugDescription substringWithRange:[match rangeAtIndex:1]];
void *videoNodePtr;
sscanf([address UTF8String], "%p", &videoNodePtr);
id videoNode = (__bridge id)videoNodePtr;

if ([videoNode isKindOfClass:NSClassFromString(@"YTVideoWithContextNode")]) {
[self extractChannelNameFromNode:videoNode completion:^(NSString *channelName) {
if (channelName) {
[[ChannelManager sharedInstance] addBlockedChannel:channelName];
UIViewController *viewController = (UIViewController *)strongSelf;
[[%c(YTToastResponderEvent) eventWithMessage:[NSString stringWithFormat:@"Blocked channel: %@", channelName]
firstResponder:viewController] send];
}
}];
}
if ([videoNode isKindOfClass:NSClassFromString(@"YTVideoWithContextNode")]) {
[self extractChannelNameFromNode:videoNode completion:^(NSString *channelName) {
if (channelName) {
[[ChannelManager sharedInstance] addBlockedChannel:channelName];
UIViewController *viewController = (UIViewController *)strongSelf;
[[%c(YTToastResponderEvent) eventWithMessage:[NSString stringWithFormat:@"Blocked %@", channelName]
firstResponder:viewController] send];
}
}];
}
} @catch (NSException *e) {
NSLog(@"[Gonerino] Exception in block action: %@", e);
}
}];

[self addAction:blockChannelAction];
}
} @catch (NSException *e) {
NSLog(@"[Gonerino] Exception in block action: %@", e);
}
}];

%orig;
objc_setAssociatedObject(self, blockActionKey, @YES, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

[self addAction:blockChannelAction];
}

%new
Expand Down

0 comments on commit 661057e

Please sign in to comment.