forked from gilshahar7/ApolloOpener
-
Notifications
You must be signed in to change notification settings - Fork 0
/
XXXApolloOpener.m
74 lines (57 loc) · 2.37 KB
/
XXXApolloOpener.m
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
#import <Opener/Opener.h>
@interface XXXApolloOpener : HBLOHandler
@end
@implementation XXXApolloOpener
- (instancetype)init {
self = [super init];
if (self) {
self.name = @"Apollo Opener";
self.identifier = @"com.gilshahar7.apolloopener";
}
return self;
}
- (id)openURL:(NSURL *)url sender:(NSString *)sender {
if ([url.host isEqualToString:@"www.reddit.com"] ||
[url.host isEqualToString:@"reddit.com"] ||
[url.host isEqualToString:@"m.reddit.com"] ||
[url.host isEqualToString:@"old.reddit.com"] ||
[url.host isEqualToString:@"reddit.app.link] ||
[url.host containsString:@".reddit.com"]) {
if ([url.host isEqualToString:@"reddit.app.link"]) {
NSURLComponents *urlComponents = [NSURLComponents componentsWithURL:url
resolvingAgainstBaseURL:NO];
NSArray *queryItems = urlComponents.queryItems;
NSString *og_redirect = [self valueForKey:@"$og_redirect"
fromQueryItems:queryItems];
if ([og_redirect length] != 0) {
url = [NSURL URLWithString:og_redirect];
} else {
return nil;
}
}
return [self createApolloURLFromURL:url];
}
return nil;
}
- (NSURL *)createApolloURLFromURL:(NSURL *)url {
if ([url.path isEqualToString:@"/"] || [url.path isEqualToString:@""]) {
return [NSURL URLWithString:@"apollo://"];
} else {
return [NSURL URLWithString:[NSString stringWithFormat:@"apollo://reddit.com%@/?%@", url.path, url.query]];
}
}
/*
Thanks to:
- https://stackoverflow.com/questions/8756683/best-way-to-parse-url-string-to-get-values-for-keys
- https://stackoverflow.com/questions/8756683/best-way-to-parse-url-string-to-get-values-for-keys/26406426#26406426
- gilshahar7 for original code
- Vantablack for the fix
*/
- (NSString *)valueForKey:(NSString *)key fromQueryItems:(NSArray *)queryItems {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name=%@", key];
NSURLQueryItem *queryItem = [[queryItems
filteredArrayUsingPredicate:predicate]
firstObject];
return queryItem.value;
}
@end