Repository navigation
Expand file tree
/
Copy pathSMWebController.m
More file actions
339 lines (296 loc) · 12.2 KB
/
Copy pathSMWebController.m
File metadata and controls
339 lines (296 loc) · 12.2 KB
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
/*
* Copyright 2015-present wequick.net
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
#import "SMWebController.h"
#import "Small.h"
#import "SMWebView.h"
#import "_SMWebNavigationItem.h"
@interface SMWebController ()
{
NSTimer *_readTitleTimer;
struct {
unsigned int injected:1;
} _smFlags;
}
@end
NSString *const SMWebViewDidStartLoadNotification = @"SMWebViewDidStartLoadNotification";
NSString *const SMWebViewDidFinishLoadNotification = @"SMWebViewDidFinishLoadNotification";
NSString *const SMWebViewErrorKey = @"SMWebViewError";
static NSString *const kSmallInjectJS = @
// Avoid function debug
"Function.prototype.toString=function(s){return ''};"
// Avoid long press to show popup menu
"document.body.style.webkitTouchCallout='none';"
// Window close listener
"window._onclose=function(){"
"if(typeof(onbeforeclose)=='function'){"
"var s=onbeforeclose();"
"if(typeof(s)=='string'&&!confirm(s))"
"return false;"
"}"
"if(typeof(onclose)=='function')"
"return onclose();"
"};"
"window._close=function(ret){"
"Small.invoke('pop',{ret:ret});"
"};"
"window.close=function(){"
"var ret=_onclose();"
"if(ret==false)return;"
"_close(ret);"
"};";
static NSString *const kSmallGetMetasJS = @
"var ms=document.head.getElementsByTagName('meta');"
"var _ms={};"
"for (var i=0;i<ms.length;i++) {"
"var m=ms[i];"
"if(m.name)"
"_ms[m.name]=m.content;"
"};JSON.stringify(_ms)";
@implementation SMWebController
- (instancetype)init {
if (self = [super init]) {
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view = self.webView;
NSArray *controllers = [self.navigationController viewControllers];
if ([controllers count] > 1) {
// Customize backBarButtonItem
[self sm_setBackBarButtonItemWithTitle:nil target:self action:@selector(backButtonClick:)];
}
_readTitleTimer = [NSTimer scheduledTimerWithTimeInterval:.05 target:self selector:@selector(readTitleTimerTick:) userInfo:nil repeats:YES];
// self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshItemClick:)];
}
//- (void)refreshItemClick:(id)sender {
// _smFlags.injected = NO;
// [self.webView reload];
//}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)readTitleTimerTick:(id)sender {
if (self.title == nil || [self.title isEqualToString:@""]) {
self.title = [self.webView stringByEvaluatingJavaScriptFromString:@"document.title"];
} else {
if (_readTitleTimer != nil) {
[_readTitleTimer invalidate];
_readTitleTimer = nil;
}
}
}
- (void)dealloc {
if (_readTitleTimer != nil) {
[_readTitleTimer invalidate];
_readTitleTimer = nil;
}
}
- (void)injectJsForWebView:(UIWebView *)webView {
if (_smFlags.injected) {
return;
}
[webView stringByEvaluatingJavaScriptFromString:kSmallInjectJS];
JSContext *context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
context[@"Small"] = self;
_smFlags.injected = YES;
}
- (void)invoke:(NSString *)method parameters:(NSDictionary *)parameters callback:(JSValue *)callback {
// WebThread
dispatch_async(dispatch_get_main_queue(), ^{
// UIThread
if ([method isEqualToString:@"confirm"]) {
[(SMWebView *)self.webView confirm:parameters[@"message"]
withTitle:parameters[@"title"]
cancelButtonTitle:parameters[@"cancel"]
otherButtonTitles:parameters[@"buttons"]
complection:^(NSInteger index) {
[callback callWithArguments:@[@(index)]];
}];
} else if ([method isEqualToString:@"alert"]) {
[(SMWebView *)self.webView alert:parameters[@"message"]
withTitle:parameters[@"title"]
confirmButtonTitle:parameters[@"ok"]
complection:^(NSInteger index) {
}];
} else if ([method isEqualToString:@"pop"]) {
[self popWithWebResult:parameters[@"ret"]];
} else {
void (^jsHandler)(NSDictionary *, JSValue *) = [Small jsHandlerForMethod:method];
if (jsHandler != nil) {
jsHandler(parameters, callback);
}
}
});
}
//___________________________________________________________________________________________________
- (BOOL)webView:(SMWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = [request URL];
// If the link is an anchor reference, scroll to the anchor
NSString *anchor = [url fragment];
if (anchor != nil) {
NSString *result = [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"var y=document.body.scrollTop;var e=document.getElementsByName('%@')[0];while(e){y+=e.offsetTop-e.scrollTop+e.clientTop;e=e.offsetParent;}y", anchor]];
float anchorY = [result floatValue];
CGPoint offset = [webView.scrollView contentOffset];
offset.y = anchorY - [webView.scrollView contentInset].top;
[UIView animateWithDuration:.3 animations:^{
[webView.scrollView setContentOffset:offset];
}];
return YES;
}
switch (navigationType) {
case UIWebViewNavigationTypeOther: {
// If the web view is never hit which means this should be a url redirect by web server or javascript.
if (!webView.isHit) return YES;
if ([webView.originalRequest.URL isEqual:request.URL]) return YES;
// Open a new page while href changed by click a link or execute javascript `location.href=xx'
[Small openURL:url fromView:webView];
return NO;
}
case UIWebViewNavigationTypeFormSubmitted:
if ([[request HTTPMethod] isEqualToString:@"POST"]) {
// Open new window with the post request
UIViewController *controller = (id)[webView nextResponder];
SMWebController *webController = [[SMWebController alloc] init];
webController.webView = [[SMWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
[webController.webView loadRequest:request];
[controller.navigationController pushViewController:webController animated:YES];
return NO;
}
case UIWebViewNavigationTypeLinkClicked:
// Open the link with {Small}
if ([self.delegate respondsToSelector:@selector(webViewControllerShouldLoadURL:)]) {
if (![self.delegate webViewControllerShouldLoadURL:url]) {
return NO;
}
}
[Small openURL:url fromView:webView];
return NO;
case UIWebViewNavigationTypeReload:
_smFlags.injected = NO;
break;
default:
break;
}
return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
[[NSNotificationCenter defaultCenter] postNotificationName:SMWebViewDidStartLoadNotification object:webView];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
if (self.title == nil || [self.title isEqualToString:@""]) {
self.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
}
[self injectJsForWebView:webView];
if (_readTitleTimer) {
[_readTitleTimer invalidate];
_readTitleTimer = nil;
}
/* Parse meta data for navigation bar items
* e.g. <meta name="right-bar-item" content="type=share;onclick=rightBar()>
*/
NSString *metasJson = [webView stringByEvaluatingJavaScriptFromString:kSmallGetMetasJS];
NSDictionary *metas = [NSJSONSerialization JSONObjectWithData:[metasJson dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:nil];
if (metas != nil) {
[self initNavigationBarItemsByMetas:metas];
}
[[NSNotificationCenter defaultCenter] postNotificationName:SMWebViewDidFinishLoadNotification object:webView];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
[[NSNotificationCenter defaultCenter] postNotificationName:SMWebViewDidFinishLoadNotification object:webView userInfo:@{SMWebViewErrorKey:error}];
}
#pragma mark - Navigation bar
- (void)backButtonClick:(id)sender {
NSString *ret = [self.webView stringByEvaluatingJavaScriptFromString:@"window._onclose()"];
if ([ret isEqual:[NSNull null]] || [ret isEqualToString:@"false"]) {
return;
}
[self popWithWebResult:ret];
}
- (void)popWithWebResult:(NSString *)result {
if (self.delegate != nil) {
[self.delegate webViewControllerDidPopWithWebResult:result];
}
[self.navigationController popViewControllerAnimated:YES];
}
- (void)initNavigationBarItemsByMetas:(NSDictionary *)metas
{
// Collect bar items
// "*-bar-item":"content"
NSMutableDictionary *metaContents = [[NSMutableDictionary alloc] init];
for (NSString *name in metas) {
NSRange range = [name rangeOfString:@"-bar-item"];
if (range.length == 0) {
continue;
}
NSString *content = metas[name];
NSArray *attrs = [content componentsSeparatedByString:@","];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
for (NSString *attr in attrs) {
NSInteger index = [attr rangeOfString:@"="].location;
NSString *key, *value;
if (index == NSNotFound) {
key = @"title"; // Default to title
value = attr;
} else {
key = [attr substringToIndex:index];
value = [attr substringFromIndex:index + 1];
}
dict[key] = value;
}
NSString *pos = [name substringToIndex:range.location];
metaContents[pos] = dict;
}
// Init left bar button item
NSDictionary *metaContent = metaContents[@"left"];
if (metaContent != nil) {
NSMutableArray *leftItems = [NSMutableArray arrayWithArray:self.navigationItem.leftBarButtonItems];
if ([leftItems count] == 1) {
UIBarButtonItem *metaItem = [[_SMWebNavigationItem alloc] initWithMetaContent:metaContent target:self action:@selector(metaBarButtonItemClick:)];
[leftItems addObject:metaItem];
self.navigationItem.leftBarButtonItems = leftItems;
}
}
// Init title item
metaContent = metaContents[@"center"];
if (metaContent != nil) {
NSString *title = metaContent[@"title"];
self.title = title;
// TODO: add title click event
}
// Init right bar button item
metaContent = metaContents[@"right"];
if (metaContent != nil) {
NSMutableArray *rightItems = [NSMutableArray arrayWithArray:self.navigationItem.rightBarButtonItems];
if ([rightItems count] == 0) {
UIBarButtonItem *metaItem = [[_SMWebNavigationItem alloc] initWithMetaContent:metaContent target:self action:@selector(metaBarButtonItemClick:)];
[rightItems addObject:metaItem];
self.navigationItem.rightBarButtonItems = rightItems;
}
}
// TODO: Menu bar button item
// @"···"
}
- (void)metaBarButtonItemClick:(_SMWebNavigationItem *)sender {
if ([sender.onclick length] > 0) {
[self.webView stringByEvaluatingJavaScriptFromString:sender.onclick];
}
}
@end