forked from PoomSmart/Return-YouTube-Dislikes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tweak.xm
669 lines (627 loc) · 25.9 KB
/
Tweak.xm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
#import <CommonCrypto/CommonDigest.h>
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <HBLog.h>
#import "Tweak.h"
#define maxRetryCount 3
#define apiUrl @"https://returnyoutubedislikeapi.com"
#define UserIDKey @"RYD-USER-ID"
#define RegistrationConfirmedKey @"RYD-USER-REGISTERED"
#define EnableVoteSubmissionKey @"RYD-VOTE-SUBMISSION"
#define DidShowEnableVoteSubmissionAlertKey @"RYD-DID-SHOW-VOTE-SUBMISSION-ALERT"
#define FETCHING @"⌛"
#define FAILED @"❌"
#define _LOC(b, x) [b localizedStringForKey:x value:nil table:nil]
#define LOC(x) _LOC(tweakBundle, x)
static NSCache <NSString *, NSNumber *> *cache;
NSBundle *RYDBundle() {
static NSBundle *bundle = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSString *tweakBundlePath = [[NSBundle mainBundle] pathForResource:@"RYD" ofType:@"bundle"];
if (tweakBundlePath)
bundle = [NSBundle bundleWithPath:tweakBundlePath];
else {
bundle = [NSBundle bundleWithPath:@"/Library/Application Support/RYD.bundle"];
if (!bundle)
bundle = [NSBundle bundleWithPath:@"/var/jb/Library/Application Support/RYD.bundle"];
}
});
return bundle;
}
static int toRYDLikeStatus(YTLikeStatus likeStatus) {
switch (likeStatus) {
case YTLikeStatusLike:
return 1;
case YTLikeStatusDislike:
return -1;
default:
return 0;
}
}
static NSString *getUserID() {
return [[NSUserDefaults standardUserDefaults] stringForKey:UserIDKey];
}
static BOOL isRegistered() {
return [[NSUserDefaults standardUserDefaults] boolForKey:RegistrationConfirmedKey];
}
static BOOL VoteSubmissionEnabled() {
return [[NSUserDefaults standardUserDefaults] boolForKey:EnableVoteSubmissionKey];
}
static const char *charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
// Ported to objc from RYD browser extension
static NSString *generateUserID() {
NSString *existingID = getUserID();
if (existingID) {
return existingID;
}
HBLogDebug(@"generateUserID()");
char userID[36 + 1];
for (int i = 0; i < 36; ++i) {
userID[i] = charset[arc4random_uniform(64)];
}
userID[36] = '\0';
NSString *result = [NSString stringWithUTF8String:userID];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:result forKey:UserIDKey];
[defaults synchronize];
return result;
}
// Ported to objc from RYD browser extension
static int countLeadingZeroes(uint8_t *hash) {
int zeroes = 0;
int value = 0;
for (int i = 0; i < CC_SHA512_DIGEST_LENGTH; i++) {
value = hash[i];
if (value == 0) {
zeroes += 8;
} else {
int count = 1;
if (value >> 4 == 0) {
count += 4;
value <<= 4;
}
if (value >> 6 == 0) {
count += 2;
value <<= 2;
}
zeroes += count - (value >> 7);
break;
}
}
return zeroes;
}
// Ported to objc from RYD browser extension
static NSString *btoa(NSString *input) {
NSMutableString *output = [NSMutableString string];
for (int i = 0; i < input.length; i += 3) {
int groupsOfSix[4] = { -1, -1, -1, -1 };
unichar ci = [input characterAtIndex:i];
groupsOfSix[0] = ci >> 2;
groupsOfSix[1] = (ci & 0x03) << 4;
if (input.length > i + 1) {
unichar ci1 = [input characterAtIndex:i + 1];
groupsOfSix[1] |= ci1 >> 4;
groupsOfSix[2] = (ci1 & 0x0f) << 2;
}
if (input.length > i + 2) {
unichar ci2 = [input characterAtIndex:i + 2];
groupsOfSix[2] |= ci2 >> 6;
groupsOfSix[3] = ci2 & 0x3f;
}
for (int j = 0; j < 4; ++j) {
if (groupsOfSix[j] == -1) {
[output appendString:@"="];
} else {
[output appendFormat:@"%c", charset[groupsOfSix[j]]];
}
}
}
return output;
}
static void fetch(
NSString *endpoint,
NSString *method,
NSDictionary *body,
void (^dataHandler)(NSDictionary *data),
BOOL (^responseCodeHandler)(NSUInteger responseCode),
void (^networkErrorHandler)(void),
void (^dataErrorHandler)(void)
) {
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", apiUrl, endpoint]];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
urlRequest.HTTPMethod = method;
if (body) {
[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSError *error = nil;
NSData *data = [NSJSONSerialization dataWithJSONObject:body options:NSJSONWritingPrettyPrinted error:&error];
if (error) {
if (dataErrorHandler) {
dataErrorHandler();
}
return;
}
HBLogDebug(@"fetch() POST body: %@", [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]);
urlRequest.HTTPBody = data;
} else {
[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"];
}
[[session dataTaskWithRequest:urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
NSUInteger responseCode = [httpResponse statusCode];
if (responseCodeHandler) {
if (!responseCodeHandler(responseCode)) {
return;
}
}
if (error || responseCode != 200) {
HBLogDebug(@"fetch() error requesting: %@ (%lu)", error, responseCode);
if (networkErrorHandler) {
networkErrorHandler();
}
return;
}
NSError *jsonError;
NSDictionary *myData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingFragmentsAllowed error:&jsonError];
if (jsonError) {
HBLogDebug(@"fetch() error decoding response: %@", jsonError);
if (dataErrorHandler) {
dataErrorHandler();
}
return;
}
dataHandler(myData);
}] resume];
}
// Ported to objc from RYD browser extension
static NSString *solvePuzzle(NSDictionary *data) {
NSString *solution = nil;
NSString *challenge = data[@"challenge"];
int difficulty = [data[@"difficulty"] intValue];
NSData *cd = [[NSData alloc] initWithBase64EncodedString:challenge options:0];
NSString *decoded = [[NSString alloc] initWithData:cd encoding:NSASCIIStringEncoding];
uint8_t c[decoded.length];
char *buffer = (char *)calloc(20, sizeof(char));
uint32_t *uInt32View = (uint32_t *)buffer;
for (int i = 0; i < decoded.length; ++i) {
c[i] = [decoded characterAtIndex:i];
}
int maxCount = (1 << difficulty) * 3;
for (int i = 4; i < 20; ++i) {
buffer[i] = c[i - 4];
}
for (int i = 0; i < maxCount; ++i) {
uInt32View[0] = i;
uint8_t hash[CC_SHA512_DIGEST_LENGTH] = {0};
CC_SHA512(buffer, 20, hash);
if (countLeadingZeroes(hash) >= difficulty) {
char chars[4] = { buffer[0], buffer[1], buffer[2], buffer[3] };
NSString *s = [[NSString alloc] initWithBytes:chars length:4 encoding:NSASCIIStringEncoding];
solution = btoa(s);
HBLogDebug(@"solvePuzzle() success (%@)", solution);
break;
}
}
free(buffer);
if (!solution) {
HBLogDebug(@"solvePuzzle() failed");
}
return solution;
}
// Ported to objc from RYD browser extension
static void registerUser() {
NSString *userId = generateUserID();
HBLogDebug(@"registerUser() (%@)", userId);
NSString *puzzleEndpoint = [NSString stringWithFormat:@"/puzzle/registration?userId=%@", userId];
fetch(
puzzleEndpoint,
@"GET",
nil,
^(NSDictionary *data) {
NSString *solution = solvePuzzle(data);
if (!solution) {
HBLogDebug(@"registerUser() skipped");
return;
}
fetch(
puzzleEndpoint,
@"POST",
@{ @"solution": solution },
^(NSDictionary *data) {
if ([data isKindOfClass:[NSNumber class]] && ![(NSNumber *)data boolValue]) {
HBLogInfo(@"registerUser() failed");
return;
}
if (!isRegistered()) {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:RegistrationConfirmedKey];
[[NSUserDefaults standardUserDefaults] synchronize];
}
HBLogDebug(@"registerUser() success or already registered");
},
NULL,
^() {
HBLogDebug(@"registerUser() puzzle failed (network)");
},
^() {
HBLogDebug(@"registerUser() puzzle failed (data)");
}
);
},
NULL,
^() {
HBLogDebug(@"registerUser() failed (network)");
},
^() {
HBLogDebug(@"registerUser() failed (data)");
}
);
}
// Ported to objc from RYD browser extension
static void sendVote(NSString *videoId, YTLikeStatus s) {
NSString *userId = getUserID();
if (!userId || !isRegistered()) {
registerUser();
return;
}
int likeStatus = toRYDLikeStatus(s);
HBLogDebug(@"sendVote(%@, %d)", videoId, likeStatus);
fetch(
@"/interact/vote",
@"POST",
@{
@"userId": userId,
@"videoId": videoId,
@"value": @(likeStatus)
},
^(NSDictionary *data) {
NSString *solution = solvePuzzle(data);
if (!solution) {
HBLogDebug(@"sendVote() skipped");
return;
}
fetch(
@"/interact/confirmVote",
@"POST",
@{
@"userId": userId,
@"videoId": videoId,
@"solution": solution
},
^(NSDictionary *data) {
HBLogDebug(@"sendVote() success");
},
NULL,
^() {
HBLogDebug(@"sendVote() confirm failed (network)");
},
^() {
HBLogDebug(@"sendVote() confirm failed (data)");
}
);
},
^BOOL(NSUInteger responseCode) {
if (responseCode == 401) {
HBLogDebug(@"sendVote() error 401, trying again");
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
registerUser();
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
sendVote(videoId, s);
});
});
return NO;
}
return YES;
},
^() {
HBLogDebug(@"sendVote() failed (network)");
},
^() {
HBLogDebug(@"sendVote() failed (data)");
}
);
}
static NSString *getXPointYFormat(NSString *count, char c) {
char firstInt = [count characterAtIndex:0];
char secondInt = [count characterAtIndex:1];
if (secondInt == '0') {
return [NSString stringWithFormat:@"%c%c", firstInt, c];
}
return [NSString stringWithFormat:@"%c.%c%c", firstInt, secondInt, c];
}
static NSString *getNormalizedDislikes(NSNumber *dislikeNumber, NSString *error) {
if (!dislikeNumber) {
return FAILED;
}
if (error) {
return error;
}
NSString *dislikeCount = [dislikeNumber stringValue];
NSUInteger digits = dislikeCount.length;
if (digits <= 3) { // 0 - 999
return dislikeCount;
}
if (digits == 4) { // 1000 - 9999
return getXPointYFormat(dislikeCount, 'K');
}
if (digits <= 6) { // 10_000 - 999_999
return [NSString stringWithFormat:@"%@K", [dislikeCount substringToIndex:digits - 3]];
}
if (digits <= 9) { // 1_000_000 - 999_999_999
return [NSString stringWithFormat:@"%@M", [dislikeCount substringToIndex:digits - 6]];
}
return [NSString stringWithFormat:@"%@B", [dislikeCount substringToIndex:digits - 9]]; // 1_000_000_000+
}
static void getDislikeFromVideoWithHandler(NSString *videoId, int retryCount, void (^handler)(NSNumber *dislikeCount, NSString *error)) {
if (retryCount <= 0) {
return;
}
if ([cache objectForKey:videoId]) {
handler([cache objectForKey:videoId], nil);
return;
}
fetch(
[NSString stringWithFormat:@"/votes?videoId=%@", videoId],
@"GET",
nil,
^(NSDictionary *data) {
NSNumber *dislikeNumber = data[@"dislikes"];
[cache setObject:dislikeNumber forKey:videoId];
handler(dislikeNumber, nil);
},
^BOOL(NSUInteger responseCode) {
if (responseCode == 502 || responseCode == 503) {
handler(nil, @"CON"); // connection error
return NO;
}
if (responseCode == 401 || responseCode == 403 || responseCode == 407) {
handler(nil, @"AUTH"); // unauthorized
return NO;
}
if (responseCode == 429) {
handler(nil, @"RL"); // rate limit
return NO;
}
if (responseCode == 404) {
handler(nil, @"NULL"); // non-existing video
return NO;
}
if (responseCode == 400) {
handler(nil, @"INV"); // malformed video
return NO;
}
return YES;
},
^() {
handler(nil, FAILED);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
getDislikeFromVideoWithHandler(videoId, retryCount - 1, handler);
});
},
^() {
handler(nil, FAILED);
}
);
}
%hook YTSlimVideoDetailsActionView
+ (YTSlimVideoDetailsActionView *)actionViewWithSlimMetadataButtonSupportedRenderer:(YTISlimMetadataButtonSupportedRenderers *)renderer withElementsContextBlock:(id)block {
if ([renderer rendererOneOfCase] == 153515154) {
return [[%c(YTSlimVideoDetailsActionView) alloc] initWithSlimMetadataButtonSupportedRenderer:renderer];
}
return %orig;
}
- (id)initWithSlimMetadataButtonSupportedRenderer:(id)arg1 {
self = %orig;
if (self) {
YTISlimMetadataButtonSupportedRenderers *renderer = [self valueForKey:@"_supportedRenderer"];
if ([renderer slimButton_isDislikeButton]) {
YTISlimMetadataToggleButtonRenderer *meta = renderer.slimMetadataToggleButtonRenderer;
getDislikeFromVideoWithHandler(meta.target.videoId, maxRetryCount, ^(NSNumber *dislikeNumber, NSString *error) {
dispatch_async(dispatch_get_main_queue(), ^{
[self.label setFormattedString:[%c(YTIFormattedString) formattedStringWithString:getNormalizedDislikes(dislikeNumber, error)]];
[self setNeedsLayout];
});
});
}
}
return self;
}
- (void)setToggled:(BOOL)toggled {
YTISlimMetadataButtonSupportedRenderers *renderer = [self valueForKey:@"_supportedRenderer"];
BOOL isDislikeButton = [renderer slimButton_isDislikeButton];
YTISlimMetadataToggleButtonRenderer *meta = renderer.slimMetadataToggleButtonRenderer;
YTIToggleButtonRenderer *buttonRenderer = meta.button.toggleButtonRenderer;
BOOL changed = NO;
if (isDislikeButton) {
changed = self.toggled != toggled;
YTIFormattedString *formattedText = [%c(YTIFormattedString) formattedStringWithString:FETCHING];
buttonRenderer.defaultText = formattedText;
buttonRenderer.toggledText = formattedText;
}
%orig;
if (changed && isDislikeButton) {
getDislikeFromVideoWithHandler(meta.target.videoId, maxRetryCount, ^(NSNumber *dislikeNumber, NSString *error) {
NSString *defaultText = getNormalizedDislikes(dislikeNumber, error);
NSString *toggledText = getNormalizedDislikes(@([dislikeNumber unsignedIntegerValue] + 1), error);
YTIFormattedString *formattedDefaultText = [%c(YTIFormattedString) formattedStringWithString:defaultText];
YTIFormattedString *formattedToggledText = [%c(YTIFormattedString) formattedStringWithString:toggledText];
buttonRenderer.defaultText = formattedDefaultText;
buttonRenderer.toggledText = formattedToggledText;
if ([self isKindOfClass:%c(YTSlimVideoDetailsActionView)]) {
[self.label setFormattedString:toggled ? formattedToggledText : formattedDefaultText];
[self setNeedsLayout];
}
});
}
}
%end
%hook YTFullscreenEngagementActionBarButtonView
- (void)updateButtonAndLabelForToggled:(BOOL)toggled {
YTFullscreenEngagementActionBarButtonRenderer *renderer = [self valueForKey:@"_buttonRenderer"];
BOOL isDislikeButton = [renderer isDislikeButton];
YTISlimMetadataToggleButtonRenderer *meta = [renderer valueForKey:@"_toggleButtonRenderer"];
YTIToggleButtonRenderer *buttonRenderer = meta.button.toggleButtonRenderer;
if (isDislikeButton) {
YTIFormattedString *formattedText = [%c(YTIFormattedString) formattedStringWithString:FETCHING];
buttonRenderer.defaultText = formattedText;
buttonRenderer.toggledText = formattedText;
}
%orig;
if (isDislikeButton) {
getDislikeFromVideoWithHandler(meta.target.videoId, maxRetryCount, ^(NSNumber *dislikeNumber, NSString *error) {
NSString *defaultText = getNormalizedDislikes(dislikeNumber, error);
NSString *toggledText = getNormalizedDislikes(@([dislikeNumber unsignedIntegerValue] + 1), error);
YTIFormattedString *formattedDefaultText = [%c(YTIFormattedString) formattedStringWithString:defaultText];
YTIFormattedString *formattedToggledText = [%c(YTIFormattedString) formattedStringWithString:toggledText];
buttonRenderer.defaultText = formattedDefaultText;
buttonRenderer.toggledText = formattedToggledText;
if ([self isKindOfClass:%c(YTFullscreenEngagementActionBarButtonView)]) {
[self.label setFormattedString:toggled ? formattedToggledText : formattedDefaultText];
[self setNeedsLayout];
}
});
}
}
%end
%hook YTReelWatchLikesController
- (void)updateLikeButtonWithRenderer:(YTILikeButtonRenderer *)renderer {
%orig;
YTQTMButton *dislikeButton = self.dislikeButton;
[dislikeButton setTitle:FETCHING forState:UIControlStateNormal];
[dislikeButton setTitle:FETCHING forState:UIControlStateSelected];
YTLikeStatus likeStatus = renderer.likeStatus;
getDislikeFromVideoWithHandler(renderer.target.videoId, maxRetryCount, ^(NSNumber *dislikeNumber, NSString *error) {
NSString *formattedDislikeCount = getNormalizedDislikes(dislikeNumber, error);
NSString *formattedToggledDislikeCount = getNormalizedDislikes(@([dislikeNumber unsignedIntegerValue] + 1), error);
YTIFormattedString *formattedText = [%c(YTIFormattedString) formattedStringWithString:formattedDislikeCount];
YTIFormattedString *formattedToggledText = [%c(YTIFormattedString) formattedStringWithString:formattedToggledDislikeCount];
if (renderer.hasDislikeCountText) {
renderer.dislikeCountText = formattedText;
}
if (renderer.hasDislikeCountWithDislikeText) {
renderer.dislikeCountWithDislikeText = formattedToggledText;
}
if (renderer.hasDislikeCountWithUndislikeText) {
renderer.dislikeCountWithUndislikeText = formattedText;
}
dispatch_async(dispatch_get_main_queue(), ^{
if (likeStatus == YTLikeStatusDislike) {
[dislikeButton setTitle:[renderer.dislikeCountWithUndislikeText stringWithFormattingRemoved] forState:UIControlStateNormal];
[dislikeButton setTitle:[renderer.dislikeCountText stringWithFormattingRemoved] forState:UIControlStateSelected];
} else {
[dislikeButton setTitle:[renderer.dislikeCountText stringWithFormattingRemoved] forState:UIControlStateNormal];
[dislikeButton setTitle:[renderer.dislikeCountWithDislikeText stringWithFormattingRemoved] forState:UIControlStateSelected];
}
});
});
}
%end
%hook YTLikeService
- (void)makeRequestWithStatus:(YTLikeStatus)likeStatus target:(YTILikeTarget *)target clickTrackingParams:(id)arg3 requestParams:(id)arg4 responseBlock:(id)arg5 errorBlock:(id)arg6 {
if (VoteSubmissionEnabled()) {
sendVote(target.videoId, likeStatus);
}
%orig;
}
- (void)makeRequestWithStatus:(YTLikeStatus)likeStatus target:(YTILikeTarget *)target clickTrackingParams:(id)arg3 queueContextParams:(id)arg4 requestParams:(id)arg5 responseBlock:(id)arg6 errorBlock:(id)arg7 {
if (VoteSubmissionEnabled()) {
sendVote(target.videoId, likeStatus);
}
%orig;
}
%end
%hook _ASDisplayView
- (void)didMoveToSuperview {
%orig;
ELMContainerNode *node = (ELMContainerNode *)self.keepalive_node;
if (![node.accessibilityIdentifier isEqualToString:@"id.video.dislike.button"]) {
return;
}
UIViewController *vc = [node closestViewController];
if (![vc isKindOfClass:%c(YTWatchNextResultsViewController)]) {
return;
}
NSString *likeCount = nil;
if (node.yogaChildren.count != 2) {
_ASDisplayView *superview = (_ASDisplayView *)self.superview;
ELMContainerNode *snode = (ELMContainerNode *)superview.keepalive_node;
ELMContainerNode *likeNode = snode.yogaChildren[0];
if (![likeNode.accessibilityIdentifier isEqualToString:@"id.video.like.button"] || likeNode.yogaChildren.count < 2) {
return;
}
ELMTextNode *likeTextNode = likeNode.yogaChildren[1];
if (![likeTextNode isKindOfClass:%c(ELMTextNode)]) {
return;
}
likeCount = likeTextNode.attributedText.string;
NSMutableArray *newArray = [node.yogaChildren mutableCopy];
[newArray addObject:likeTextNode];
node.yogaChildren = newArray;
}
ELMTextNode *candidate = node.yogaChildren[1];
if (![candidate isKindOfClass:%c(ELMTextNode)]) {
return;
}
NSObject *wc = [vc valueForKey:@"_metadataPanelStateProvider"];
YTPlayerViewController *pvc;
if ([wc isKindOfClass:%c(YTWatchController)]) {
pvc = [wc valueForKey:@"_playerViewController"];
} else if ([wc isKindOfClass:%c(YTPlaybackStrippedWatchController)]) {
YTWatchPlaybackController *wpc = ((YTPlaybackStrippedWatchController *)wc).watchPlaybackController;
pvc = [wpc valueForKey:@"_playerViewController"];
}
NSString *videoId = [pvc currentVideoID];
NSMutableAttributedString *mutableText = [[NSMutableAttributedString alloc] initWithAttributedString:candidate.attributedText];
mutableText.mutableString.string = FETCHING;
candidate.attributedText = mutableText;
getDislikeFromVideoWithHandler(videoId, maxRetryCount, ^(NSNumber *dislikeNumber, NSString *error) {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *dislikeCount = getNormalizedDislikes(dislikeNumber, error);
mutableText.mutableString.string = likeCount ? [NSString stringWithFormat:@"%@ | %@", likeCount, dislikeCount] : dislikeCount;
candidate.attributedText = mutableText;
});
});
}
%end
static void enableVoteSubmission(BOOL enabled) {
[[NSUserDefaults standardUserDefaults] setBool:enabled forKey:EnableVoteSubmissionKey];
}
%hook YTSettingsViewController
- (void)setSectionItems:(NSMutableArray <YTSettingsSectionItem *> *)sectionItems forCategory:(NSInteger)category title:(NSString *)title titleDescription:(NSString *)titleDescription headerHidden:(BOOL)headerHidden {
if (category == 1) {
NSUInteger statsForNerdsIndex = [sectionItems indexOfObjectPassingTest:^BOOL (YTSettingsSectionItem *item, NSUInteger idx, BOOL *stop) {
return item.settingItemId == 265;
}];
if (statsForNerdsIndex != NSNotFound) {
NSBundle *tweakBundle = RYDBundle();
YTSettingsSectionItem *vote = [%c(YTSettingsSectionItem) switchItemWithTitle:LOC(@"ENABLE_VOTE_SUBMIT")
titleDescription:[NSString stringWithFormat:LOC(@"ENABLE_VOTE_SUBMIT_DESC"), apiUrl]
accessibilityIdentifier:nil
switchOn:VoteSubmissionEnabled()
switchBlock:^BOOL (YTSettingsCell *cell, BOOL enabled) {
enableVoteSubmission(enabled);
return YES;
}
settingItemId:0];
[sectionItems insertObject:vote atIndex:statsForNerdsIndex + 1];
}
}
%orig(sectionItems, category, title, titleDescription, headerHidden);
}
%end
%ctor {
cache = [NSCache new];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (![defaults boolForKey:DidShowEnableVoteSubmissionAlertKey] && !VoteSubmissionEnabled()) {
[defaults setBool:YES forKey:DidShowEnableVoteSubmissionAlertKey];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSBundle *tweakBundle = RYDBundle();
YTAlertView *alertView = [%c(YTAlertView) confirmationDialogWithAction:^{
enableVoteSubmission(YES);
} actionTitle:_LOC([NSBundle mainBundle], @"settings.yes")];
alertView.title = @"Return YouTube Dislike";
alertView.subtitle = [NSString stringWithFormat:LOC(@"WANT_TO_ENABLE"), apiUrl, LOC(@"ENABLE_VOTE_SUBMIT")];
[alertView show];
});
}
}