- Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathUIWebView+TS_JavaScriptContext.m
More file actions
Latest commit
105 lines (75 loc) · 2.89 KB
/
Copy pathUIWebView+TS_JavaScriptContext.m
File metadata and controls
105 lines (75 loc) · 2.89 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
//
// UIWebView+TS_JavaScriptContext.m
//
// Created by Nicholas Hodapp on 11/15/13.
// Copyright (c) 2013 CoDeveloper, LLC. All rights reserved.
//
#import"UIWebView+TS_JavaScriptContext.h"
#import<JavaScriptCore/JavaScriptCore.h>
#import<objc/runtime.h>
staticconstcharkTSJavaScriptContext[] = "ts_javaScriptContext";
staticNSHashTable* g_webViews = nil;
@interfaceUIWebView (TS_JavaScriptCore_private)
- (void) ts_didCreateJavaScriptContext:(JSContext *)ts_javaScriptContext;
@end
@protocolTSWebFrame <NSObject>
- (id) parentFrame;
@end
@implementationNSObject (TS_JavaScriptContext)
- (void) webView: (id) unuseddidCreateJavaScriptContext: (JSContext*) ctxforFrame: (id<TSWebFrame>) frame
{
NSParameterAssert( [frame respondsToSelector:@selector( parentFrame )] );
// only interested in root-level frames
if ( [frame respondsToSelector:@selector( parentFrame) ] && [frame parentFrame] != nil )
return;
void (^notifyDidCreateJavaScriptContext)() = ^{
for ( UIWebView* webView in g_webViews )
{
NSString* cookie = [NSStringstringWithFormat:@"ts_jscWebView_%lud", (unsignedlong)webView.hash ];
[webView stringByEvaluatingJavaScriptFromString: [NSStringstringWithFormat:@"var %@ = '%@'", cookie, cookie ] ];
if ( [ctx[cookie].toString isEqualToString: cookie] )
{
[webView ts_didCreateJavaScriptContext: ctx];
return;
}
}
};
if ( [NSThreadisMainThread] )
{
notifyDidCreateJavaScriptContext();
}
else
{
dispatch_async( dispatch_get_main_queue(), notifyDidCreateJavaScriptContext );
}
}
@end
@implementationUIWebView (TS_JavaScriptContext)
+ (id) allocWithZone:(struct _NSZone *)zone
{
staticdispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
g_webViews = [NSHashTableweakObjectsHashTable];
});
NSAssert( [NSThreadisMainThread], @"uh oh - why aren't we on the main thread?");
id webView = [superallocWithZone: zone];
[g_webViews addObject: webView];
return webView;
}
- (void) ts_didCreateJavaScriptContext:(JSContext *)ts_javaScriptContext
{
[selfwillChangeValueForKey:@"ts_javaScriptContext"];
objc_setAssociatedObject( self, kTSJavaScriptContext, ts_javaScriptContext, OBJC_ASSOCIATION_RETAIN);
[selfdidChangeValueForKey:@"ts_javaScriptContext"];
if ( [self.delegate respondsToSelector:@selector(webView:didCreateJavaScriptContext:)] )
{
id<TSWebViewDelegate> delegate = ( id<TSWebViewDelegate>)self.delegate;
[delegate webView:selfdidCreateJavaScriptContext: ts_javaScriptContext];
}
}
- (JSContext*) ts_javaScriptContext
{
JSContext* javaScriptContext = objc_getAssociatedObject( self, kTSJavaScriptContext );
return javaScriptContext;
}
@end