Skip to content

Commit

Permalink
fix client side ad blocking, block feed ads
Browse files Browse the repository at this point in the history
  • Loading branch information
level3tjg committed Jun 20, 2024
1 parent e27e286 commit 10d9971
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
5 changes: 5 additions & 0 deletions Tweak.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
#import <Twitch/FollowingViewController.h>
#import <Twitch/HeadlinerFollowingAdManager.h>
#import <Twitch/LiveHLSURLProvider.h>
#import <Twitch/TheaterViewController.h>
#import <TwitchCoreUI/TWDefaultThemeManager.h>
#import <TwitchKit/TKGraphQL.h>

@interface _TtC6Twitch21TheaterViewController ()
- (void)removeAdControllers;
@end

static NSMutableDictionary<NSString *, TWHLSProvider *> *providers;
47 changes: 41 additions & 6 deletions Tweak.xm
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,24 @@ static NSData *TWAdBlockData(NSURLRequest *request, NSData *data) {
}
%end

%hook _TtC6Twitch14VASTAdProvider
- (instancetype)init {
if ((self = %orig)) {
MSHookIvar<NSInteger>(self, "adLoadingTimeout") = -1;
}
return self;
%hook _TtC6Twitch21TheaterViewController
%new
- (void)removeAdControllers {
_TtC6Twitch19TheaterAdController *theaterAdController =
MSHookIvar<_TtC6Twitch19TheaterAdController *>(self, "theaterAdController");
[@[ @"displayAdController", @"streamDisplayAdStateManager", @"vastAdController" ]
enumerateObjectsUsingBlock:^(NSString *name, NSUInteger idx, BOOL *stop) {
if (class_getInstanceVariable(object_getClass(theaterAdController), name.UTF8String))
MSHookIvar<id>(theaterAdController, name.UTF8String) = NULL;
}];
}
- (void)viewDidLoad {
%orig;
[NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(removeAdControllers)
userInfo:nil
repeats:YES];
}
%end

Expand Down Expand Up @@ -117,6 +129,29 @@ static NSData *TWAdBlockData(NSURLRequest *request, NSData *data) {
}
%end

%hook _TtC9TwitchKit18TKURLSessionClient
- (void)URLSession:(NSURLSession *)session
dataTask:(NSURLSessionDataTask *)dataTask
didReceiveData:(NSData *)data {
if (![NSUserDefaults.standardUserDefaults boolForKey:@"TWAdBlockEnabled"] || !data)
return %orig;
NSError *error;
NSMutableArray *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
if (!json || error) return %orig;
if ([json isKindOfClass:NSArray.class]) {
for (NSDictionary *operation in json) {
NSMutableDictionary *feedItems = operation[@"data"][@"feedItems"];
if (feedItems)
feedItems[@"edges"] = [feedItems[@"edges"] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self.node.__typename != 'FeedAd'"]];
}
}
NSData *modifiedData = [NSJSONSerialization dataWithJSONObject:json options:0 error:&error];
if (error) return %orig;
data = modifiedData;
%orig;
}
%end

%hook _TtC6Twitch18LiveHLSURLProvider
- (NSURL *)manifestURLWithToken:(NSString *)token
tokenDictionary:(NSDictionary *)tokenDictionary
Expand Down
4 changes: 4 additions & 0 deletions include/Twitch/TheaterAdController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#import <Foundation/Foundation.h>

@interface _TtC6Twitch19TheaterAdController
@end
7 changes: 7 additions & 0 deletions include/Twitch/TheaterViewController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#import <UIKit/UIKit.h>
#import "TheaterAdController.h"

@interface _TtC6Twitch21TheaterViewController : UIViewController {
_TtC6Twitch19TheaterAdController *theaterAdController;
}
@end

0 comments on commit 10d9971

Please sign in to comment.