forked from shaojiankui/JKCategories
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebView+Debug.m
184 lines (148 loc) · 5.53 KB
/
WebView+Debug.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
//
// WebView+Debug.m
// VOL
//
// Created by Pablo Guillen Schlippe on 26.07.11.
// Copyright 2011 Medienhaus. All rights reserved.
//
#ifdef DEBUG
#import <objc/runtime.h>
#import "WebView+Debug.h"
@class WebView;
@class WebFrame;
@class WebScriptCallFrame;
#pragma mark -
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
static NSString* getAddress() {
id myhost =[NSClassFromString(@"NSHost") performSelector:@selector(currentHost)];
if (myhost) {
for (NSString* address in [myhost performSelector:@selector(addresses)]) {
if ([address rangeOfString:@"::"].location == NSNotFound) {
return address;
}
}
}
return @"127.0.0.1";
}
void enableRemoteWebInspector() {
[NSClassFromString(@"WebView") performSelector:@selector(_enableRemoteInspector)];
NSLog(@"Point your browser at http://%@:9999", getAddress());
}
#pragma clang diagnostic pop
#pragma mark -
@interface ScriptDebuggerDelegate : NSObject
-(id)functionNameForFrame:(WebScriptCallFrame*)frame;
-(id)callerForFrame:(WebScriptCallFrame*)frame;
-(id)exceptionForFrame:(WebScriptCallFrame*)frame;
@end
#pragma mark -
@implementation ScriptDebuggerDelegate
// We only have access to the public methods declared in the header / class
// The private methods can also be accessed but raise a warning.
// Use runtime selectors to suppress warnings
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
#pragma clang diagnostic ignored "-Wundeclared-selector"
-(id)functionNameForFrame:(WebScriptCallFrame*)frame {
SEL functionNameSelector = @selector(functionName);
return [(id)frame performSelector:functionNameSelector];
}
-(id)callerForFrame:(WebScriptCallFrame*)frame {
SEL callerSelector = @selector(caller);
return [(id)frame performSelector:callerSelector];
}
-(id)exceptionForFrame:(WebScriptCallFrame*)frame {
SEL exceptionSelector = @selector(exception);
return [(id)frame performSelector:exceptionSelector];
}
#pragma clang diagnostic pop
- (void)webView:(WebView *)webView didParseSource:(NSString *)source
baseLineNumber:(unsigned)lineNumber
fromURL:(NSURL *)url
sourceId:(int)sid
forWebFrame:(WebFrame *)webFrame {
if (kDidParseSource)
NSLog(@"ScriptDebugger called didParseSource: \nsourceId=%d, \nurl=%@", sid, url);
}
// some source failed to parse
- (void)webView:(WebView *)webView failedToParseSource:(NSString *)source
baseLineNumber:(unsigned)lineNumber
fromURL:(NSURL *)url
withError:(NSError *)error
forWebFrame:(WebFrame *)webFrame {
if (kFailedToParseSource)
NSLog(@"ScriptDebugger called failedToParseSource:\
\nurl=%@ \nline=%d \nerror=%@ \nsource=%@",
url, lineNumber, error, source);
}
- (void)webView:(WebView *)webView exceptionWasRaised:(WebScriptCallFrame *)frame
sourceId:(int)sid
line:(int)lineno
forWebFrame:(WebFrame *)webFrame {
if (kExceptionWasRaised)
NSLog(@"ScriptDebugger exception:\
\nsourceId=%d \nline=%d \nfunction=%@, \ncaller=%@, \nexception=%@",
sid,
lineno,
[self functionNameForFrame:frame],
[self callerForFrame:frame],
[self exceptionForFrame:frame]);
}
// just entered a stack frame (i.e. called a function, or started global scope)
- (void)webView:(WebView *)webView didEnterCallFrame:(WebScriptCallFrame *)frame
sourceId:(int)sid
line:(int)lineno
forWebFrame:(WebFrame *)webFrame {
if (kDidEnterCallFrame)
NSLog(@"ScriptDebugger didEnterCallFrame:\
\nsourceId=%d \nline=%d \nfunction=%@, \ncaller=%@, \nexception=%@",
sid,
lineno,
[self functionNameForFrame:frame],
[self callerForFrame:frame],
[self exceptionForFrame:frame]);
}
// about to execute some code
- (void)webView:(WebView *)webView willExecuteStatement:(WebScriptCallFrame *)frame
sourceId:(int)sid
line:(int)lineno
forWebFrame:(WebFrame *)webFrame {
if (kWillExecuteStatement)
NSLog(@"ScriptDebugger willExecuteStatement:\
\nsourceId=%d \nline=%d \nfunction=%@, \ncaller=%@, \nexception=%@",
sid,
lineno,
[self functionNameForFrame:frame],
[self callerForFrame:frame],
[self exceptionForFrame:frame]);
}
// about to leave a stack frame (i.e. return from a function)
- (void)webView:(WebView *)webView willLeaveCallFrame:(WebScriptCallFrame *)frame
sourceId:(int)sid
line:(int)lineno
forWebFrame:(WebFrame *)webFrame {
if (kWillLeaveCallFrame)
NSLog(@"ScriptDebugger willLeaveCallFrame:\
\nsourceId=%d \nline=%d \nfunction=%@, \ncaller=%@, \nexception=%@",
sid,
lineno,
[self functionNameForFrame:frame],
[self callerForFrame:frame],
[self exceptionForFrame:frame]);
}
@end
#pragma mark -
@interface UIWebView ()
-(id)setScriptDebugDelegate:(id)delegate;
@end
#pragma mark -
@implementation UIWebView (DebugCategory)
- (void)webView:(id)sender didClearWindowObject:(id)windowObject
forFrame:(WebFrame*)frame {
ScriptDebuggerDelegate* delegate = [[ScriptDebuggerDelegate alloc] init];
objc_setAssociatedObject(sender, @"ScriptDebuggerDelegate", delegate, OBJC_ASSOCIATION_RETAIN);
[sender setScriptDebugDelegate:delegate];
}
@end
#endif