This repository was archived by the owner on Feb 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathGAJSWebViewEngine.m
More file actions
executable file
·232 lines (194 loc) · 7.64 KB
/
GAJSWebViewEngine.m
File metadata and controls
executable file
·232 lines (194 loc) · 7.64 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
//
// GAJSEngine.h
// GAJavaScriptTracker
//
// Created by Dominik Pich
// Copyright © 2012 doo GmbH / Dominik Pich. All rights reserved.
//
#import "GAJSWebViewEngine.h"
@interface GAJSWebViewEngine ()
@property(nonatomic, readwrite) WebView *webView;
@end
@implementation GAJSWebViewEngine {
BOOL _webviewLoaded;
NSTimer *_batchTimer;
NSMutableArray *_webViewPendingScripts;
}
@synthesize htmlName=_htmlName;
@synthesize htmlVariables = _htmlVariables;
@synthesize webView=_webView;
@synthesize batchSize=_batchSize;
@synthesize batchInterval=_batchInterval;
@synthesize debugwebview=_debugwebview;
- (WebView*)createWebView {
WebView *webView;
if(!_debugwebview) {
//alloc
webView = [[WebView alloc] initWithFrame:CGRectMake(0, 0, 10, 10) frameName:@"GAJSWebViewEngine" groupName:@"GAJS"];
}else{
webView = self.debugwebview;
}
//set properties
webView.shouldUpdateWhileOffscreen = YES;
webView.UIDelegate = self;
webView.resourceLoadDelegate = self;
webView.frameLoadDelegate = self;
//get file
NSURL *file = [[NSBundle bundleForClass:self.class] URLForResource:_htmlName
withExtension:@"html"];
if(!file) {
@throw [NSException exceptionWithName:@"GAJSException"
reason:[NSString stringWithFormat:@"File not found in bundle: %@.html", _htmlName]
userInfo:nil];
}
//load content
NSString *library = [NSString stringWithContentsOfURL:file
encoding:NSUTF8StringEncoding
error:nil];
if(!library.length) {
@throw [NSException exceptionWithName:@"GAJSException"
reason:[NSString stringWithFormat:@"File couldnt be read as UTF-8 content / file has no content: %@.js", file.lastPathComponent]
userInfo:nil];
}
//update string
if(_htmlVariables.count)
{
for (NSString *key in _htmlVariables.allKeys) {
id k = [NSString stringWithFormat:@"%%%@%%", key];
id v = [_htmlVariables objectForKey:key];
library = [library stringByReplacingOccurrencesOfString:k withString:v];
}
}
//load
// [[webView mainFrame] loadHTMLString:library baseURL:[file URLByDeletingLastPathComponent]];
NSString *d = NSTemporaryDirectory();
NSString *p = [[d stringByAppendingPathComponent:[[NSProcessInfo processInfo] globallyUniqueString]] stringByAppendingPathExtension:@"html"];
[library writeToFile:p atomically:NO encoding:NSUTF8StringEncoding error:nil];
id p2 = [[file.path stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"ga.js"];
[[NSFileManager defaultManager] copyItemAtPath:p2 toPath:[d stringByAppendingPathComponent:@"ga.js"] error:nil];
[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:p]]];
return webView;
}
- (void)setHtmlName:(NSString *)htmlName {
_webviewLoaded = NO;
_webView = nil;
_htmlName = htmlName;
}
- (void)setBatchSize:(NSUInteger)batchSize {
if(!batchSize) batchSize = 1;
_batchSize = batchSize;
if(_webViewPendingScripts.count >= _batchSize) {
[self sendOffBatchedJS];
}
[self setupBatching];
}
- (void)setBatchInterval:(NSTimeInterval)batchInterval {
if(!batchInterval) batchInterval = DEFAULT_BATCH_INTERVAL;
_batchInterval = batchInterval;
if(_webViewPendingScripts.count >= _batchSize) {
[self sendOffBatchedJS];
}
[self setupBatching];
}
#pragma mark -
- (void)batchTimerFired:(NSTimer*)timer {
[self sendOffBatchedJS];
#if FREE_WEBVIEW_AFTER_BATCH
[self setNeedsReload];
#endif
}
- (void)sendOffBatchedJS {
if(_webviewLoaded) {
for(id aJSString in _webViewPendingScripts) {
//run it
DLog(@"[JSC] Evaluate JS: %@ %@ %@", aJSString, _webView.customUserAgent, _webView.applicationNameForUserAgent);
NSString *result = [_webView stringByEvaluatingJavaScriptFromString:aJSString];
if (!result) {
DLog(@"[JSC] No result returned");
}
}
[_webViewPendingScripts removeAllObjects];
}
else {
_webView = [self createWebView];
}
}
- (void)setupBatching {
[_batchTimer invalidate];
_batchTimer = nil;
if(_batchSize>1) {
NSTimeInterval interval = _batchInterval>0 ?_batchInterval : DEFAULT_BATCH_INTERVAL;
_batchTimer = [NSTimer scheduledTimerWithTimeInterval:interval
target:self
selector:@selector(batchTimerFired:)
userInfo:nil
repeats:YES];
}
}
#pragma mark -
- (void)runJS:(NSString *)aJSString
{
if(!aJSString.length) {
@throw [NSException exceptionWithName:@"GAJSEngineException"
reason:@"empty JS String for running"
userInfo:nil];
}
//enqueue it
if(!_webViewPendingScripts) {
_webViewPendingScripts = [NSMutableArray array];
}
[_webViewPendingScripts addObject:aJSString];
if(_webViewPendingScripts.count >= _batchSize) {
[self sendOffBatchedJS];
}
}
- (void)loadJSLibraryFromBundle:(NSString*)libraryName {
//find in our bundle
NSURL *file = [[NSBundle bundleForClass:self.class] URLForResource:libraryName
withExtension:@"js"];
if(!file) {
@throw [NSException exceptionWithName:@"GAJSException"
reason:[NSString stringWithFormat:@"File not found in bundle: %@.js", libraryName]
userInfo:nil];
}
[self loadJSLibraryFromURL:file];
}
- (void)loadJSLibraryFromURL:(NSURL*)url {
//load content
NSString *library = [NSString stringWithContentsOfURL:url
encoding:NSUTF8StringEncoding
error:nil];
if(!library.length) {
@throw [NSException exceptionWithName:@"GAJSException"
reason:[NSString stringWithFormat:@"File couldnt be read as UTF-8 content / file has no content: %@.js", url.lastPathComponent]
userInfo:nil];
}
DLog(@"[JSC] loading library %@...", url.lastPathComponent);
[self runJS:library];
}
- (void)flushJS {
if(_webViewPendingScripts.count >= 1) {
[self sendOffBatchedJS];
}
}
#pragma mark -
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
DLog(@"did load webview");
_webviewLoaded = YES;
if(_webViewPendingScripts.count >= _batchSize) {
[self sendOffBatchedJS];
}
}
#if DEBUG_WEBVIEW_ENGINE
- (void)webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
DLog(@"[javascript-alert] %@", message);
}
- (NSURLRequest *)webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)dataSource {
DLog(@"[request] %@", request);
return request;
}
- (void)webView:(WebView *)sender resource:(id)identifier didReceiveResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)dataSource {
DLog(@"[response] %@", response);
}
#endif
@end