forked from reddit-archive/iReddit
/
RedditWebView.m
executable file
·195 lines (150 loc) · 6.29 KB
/
RedditWebView.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
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
//
// RedditWebView.m
// Reddit
//
// Created by Ross Boucher on 3/11/09.
// Copyright 2009 280 North. All rights reserved.
//
#import "RedditWebView.h"
#import "Constants.h"
#import "LoginController.h"
#import "LoginViewController.h"
@implementation RedditWebView
id _realDelegate;
+ (NSString *)storyIDForURL:(NSURL *)aURL
{
return [[self class] storyIDForURL:aURL commentID:nil];
}
+ (NSString *)storyIDForURL:(NSURL *)aURL commentID:(NSString **)stringPtr
{
NSString *path = [aURL path];
NSString *host = [aURL host];
NSRange redditComRange = [host rangeOfString:@"reddit.com"];
NSRange commentsRange = [path rangeOfString:@"/comments/"];
if (redditComRange.location > 0 && commentsRange.location >= 0 && NSMaxRange(commentsRange) < [path length])
{
NSString *storyID = nil;
NSString *remainder = [path substringFromIndex:NSMaxRange(commentsRange)];
NSScanner *scanner = [NSScanner scannerWithString:remainder];
[scanner scanUpToCharactersFromSet:[NSCharacterSet characterSetWithCharactersInString:@"/?&"] intoString:&storyID];
NSArray *components = [remainder componentsSeparatedByString:@"/"];
NSString *lastComponent = [components lastObject];
if (stringPtr)
*stringPtr = lastComponent;
if (storyID)
return [[storyID retain] autorelease];
}
return nil;
}
- (void)awakeFromNib
{
[super setDelegate:(id<UIWebViewDelegate>)self];
}
- (id)initWithFrame:(CGRect)aFrame
{
if (self = [super initWithFrame:aFrame])
{
[super setDelegate:(id<UIWebViewDelegate>)self];
}
return self;
}
- (void)setDelegate:(id)delegate
{
_realDelegate = delegate;
}
- (id)delegate
{
return _realDelegate;
}
- (void)loginViewController:(LoginViewController *)aController didFinishWithContext:(id)aContext
{
if ([[LoginController sharedLoginController] isLoggedIn] && [aContext isEqual:@"RedditWebView"])
{
[self stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"window.MODHASH = '%@'", [[LoginController sharedLoginController] modhash]]];
}
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
if (_realDelegate && [_realDelegate respondsToSelector:@selector(webViewDidStartLoad:)])
[_realDelegate webViewDidStartLoad:webView];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
if ([[LoginController sharedLoginController] isLoggedIn])
[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"window.MODHASH = '%@'", [[LoginController sharedLoginController] modhash]]];
if (_realDelegate && [_realDelegate respondsToSelector:@selector(webViewDidFinishLoad:)])
[_realDelegate webViewDidFinishLoad:webView];
[self canGoBack];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
if (_realDelegate && [_realDelegate respondsToSelector:@selector(webView:didFailLoadWithError:)])
[_realDelegate webView:webView didFailLoadWithError:error];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
BOOL response = YES;
if (_realDelegate && [_realDelegate respondsToSelector:@selector(webView:shouldStartLoadWithRequest:navigationType:)])
response = [_realDelegate webView:webView shouldStartLoadWithRequest:request navigationType:navigationType];
if (!response)
return NO;
if ([@"http://login/" isEqualToString:[[request URL] absoluteString]])
{
[LoginViewController presentWithDelegate:(id <LoginViewControllerDelegate>)self context:@"RedditWebView"];
return NO;
}
NSString *commentID = nil;
NSString *storyID = [[self class] storyIDForURL:[request URL] commentID:&commentID];
if (storyID)
{
Story *theStory = [Story storyWithID:storyID];
if (theStory)
[self loadWithStory:theStory commentID:commentID];
else
[self loadWithStoryID:storyID commentID:commentID];
}
return YES;
}
- (void)loadWithStoryID:(NSString *)theID commentID:(NSString *)commentID
{
NSString *path = [NSString stringWithFormat:@"%@?id=%@&title=%@&author=%@&created=%@&domain=%@&base=%@&jump=%@",
[[NSBundle mainBundle] pathForResource:@"comments" ofType:@"html"],
[theID stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
[@"Loading story..." stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
[@"reddit" stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
[[NSString stringWithFormat:@"%f",[[NSDate date] timeIntervalSince1970]] stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
[@"reddit.com" stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
[RedditBaseURLString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
[(commentID ? commentID : @"no_such_id") stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]
];
NSURL *url = [[NSURL alloc] initWithScheme:@"file" host:@"localhost" path:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[url release];
[self loadRequest:request];
}
- (void)loadWithStory:(Story *)story commentID:(NSString *)commentID
{
NSString *path = [NSString stringWithFormat:@"%@?id=%@&title=%@&author=%@&created=%@&domain=%@&base=%@",
[[NSBundle mainBundle] pathForResource:@"comments" ofType:@"html"],
[story.identifier stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
[story.title stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
[story.author stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
[story.created stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
[story.domain stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
[RedditBaseURLString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
[(commentID ? commentID : @"no_such_id") stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]
];
NSURL *url = [[NSURL alloc] initWithScheme:@"file" host:@"localhost" path:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[url release];
[self loadRequest:request];
}
- (void)dealloc
{
super.delegate = nil;
[super dealloc];
}
@end