forked from TomSwift/UIWebView-TS_JavaScriptContext
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIWebView+TS_JavaScriptContext.m
105 lines (75 loc) · 2.89 KB
/
UIWebView+TS_JavaScriptContext.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
//
// 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>
static const char kTSJavaScriptContext[] = "ts_javaScriptContext";
static NSHashTable* g_webViews = nil;
@interface UIWebView (TS_JavaScriptCore_private)
- (void) ts_didCreateJavaScriptContext:(JSContext *)ts_javaScriptContext;
@end
@protocol TSWebFrame <NSObject>
- (id) parentFrame;
@end
@implementation NSObject (TS_JavaScriptContext)
- (void) webView: (id) unused didCreateJavaScriptContext: (JSContext*) ctx forFrame: (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 = [NSString stringWithFormat: @"ts_jscWebView_%lud", (unsigned long)webView.hash ];
[webView stringByEvaluatingJavaScriptFromString: [NSString stringWithFormat: @"var %@ = '%@'", cookie, cookie ] ];
if ( [ctx[cookie].toString isEqualToString: cookie] )
{
[webView ts_didCreateJavaScriptContext: ctx];
return;
}
}
};
if ( [NSThread isMainThread] )
{
notifyDidCreateJavaScriptContext();
}
else
{
dispatch_async( dispatch_get_main_queue(), notifyDidCreateJavaScriptContext );
}
}
@end
@implementation UIWebView (TS_JavaScriptContext)
+ (id) allocWithZone:(struct _NSZone *)zone
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
g_webViews = [NSHashTable weakObjectsHashTable];
});
NSAssert( [NSThread isMainThread], @"uh oh - why aren't we on the main thread?");
id webView = [super allocWithZone: zone];
[g_webViews addObject: webView];
return webView;
}
- (void) ts_didCreateJavaScriptContext:(JSContext *)ts_javaScriptContext
{
[self willChangeValueForKey: @"ts_javaScriptContext"];
objc_setAssociatedObject( self, kTSJavaScriptContext, ts_javaScriptContext, OBJC_ASSOCIATION_RETAIN);
[self didChangeValueForKey: @"ts_javaScriptContext"];
if ( [self.delegate respondsToSelector: @selector(webView:didCreateJavaScriptContext:)] )
{
id<TSWebViewDelegate> delegate = ( id<TSWebViewDelegate>)self.delegate;
[delegate webView: self didCreateJavaScriptContext: ts_javaScriptContext];
}
}
- (JSContext*) ts_javaScriptContext
{
JSContext* javaScriptContext = objc_getAssociatedObject( self, kTSJavaScriptContext );
return javaScriptContext;
}
@end