#import <WebKit/WebKit.h>
#import "WebScriptObject+Value.h"
@interface WebViewEx : WebView
//js 调用 OC代码,建议param用json格式,调用方式 window.external.execute("method","param");
//throwevent 2个参数的时候
//@property(copy) id (^jsCallBlock)(NSString *methodName, id param);
//thoreevent 3个参数的时候
@property(copy) id (^jsCallbackBlock)(NSString *methodName, id param,NSString *callbackName);
//OC 调用 js代码
- (id)callJavascript:(NSString *)script;
//加载url时带上cookie
- (void)loadUrl:(NSString *)url withCookie:(NSString *)cookie;
- (id)callWebScriptMethod:(NSString *)name withArguments:(NSArray *)arguments;
@end
@implementation WebViewEx
//- (BOOL)mouseDownCanMoveWindow{
//
// return YES;
//}
- (id)initWithFrame:(NSRect)frameRect
{
self = [super initWithFrame:frameRect];
if (self) {
[self.windowScriptObject setValue:self forKey:@"BDHScript"];
[self setFrameLoadDelegate:self];
[self setContinuousSpellCheckingEnabled:NO];
}
return self;
}
- (void)awakeFromNib
{
//BDHScript.ThrowEvent
//[self.windowScriptObject setValue:self forKey:@"external"];
BEGIN_INVOKE_ON_MAIN_THREAD
[self.windowScriptObject setValue:self forKey:@"BDHScript"];
END_INVOKE_ON_MAIN_THREAD
self.frameLoadDelegate = self;
[self setContinuousSpellCheckingEnabled:NO];
}
- (void)webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)windowObject forFrame:(WebFrame *)frame
{
[self.windowScriptObject setValue:self forKey:@"BDHScript"];
NSLog(@"-----------------load ready");
}
- (id)callJavascript:(NSString *)script
{
// script = [script stringByReplacingOccurrencesOfString:@"\n" withString:@""];
if (self.windowScriptObject == nil) return nil;
__block id data ;
BEGIN_INVOKE_ON_MAIN_THREAD
NSString *jsScript = [NSString stringWithFormat:@"try { %@ } catch (e) { e }", script];
// run it
data = [self.windowScriptObject evaluateWebScript:jsScript];
END_INVOKE_ON_MAIN_THREAD
if ([data isMemberOfClass:[WebUndefined class]]) {
//无返回值
return nil;
}
return data;
}
- (void)loadUrl:(NSString *)url withCookie:(NSString *)cookie
{
BEGIN_INVOKE_ON_MAIN_THREAD
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
if (cookie.length > 0) {
[request setValue:cookie forHTTPHeaderField:@"Cookie"];
}
[self.mainFrame loadRequest:request];
END_INVOKE_ON_MAIN_THREAD
}
+ (NSString *)webScriptNameForSelector:(SEL)sel
{
// if (sel == @selector(execute:param:))
// return @"execute";
if (sel == @selector(throwEvent:json:callback:))
return @"ThrowEvent";
return nil;
}
+ (BOOL)isSelectorExcludedFromWebScript:(SEL)sel
{
// if (sel == @selector(execute:param:))
// return NO;
if (sel == @selector(throwEvent:json:callback:))
return NO;
return YES;
}
//变量互访问
//+ (BOOL) isKeyExcludedFromWebScript:(const char *)name
//+ (NSString *) webScriptNameForKey:(const char *)name
//- (id)execute:(NSString *)method param:(NSString *)param
//{
// if (self.jsCallBlock != nil) {
// return self.jsCallBlock(method, param);
// }
// return nil;
//}
- (id)throwEvent:(NSString *)method json:(id)param callback:(NSString *)callback
{
if ([method isEqualToString:@"jsLog"]) {
NSString *log = [param description];
if (log.length > 200) {
log = [log substringToIndex:200];
}
SCLogI(@"jsLog: %@",log);
return nil;
}
// if ([callback isMemberOfClass:[WebUndefined class]]) {
// return [self throwEvent:method param:param];
// }
SCLogI(@"throwEvent: %@",method);
#ifdef DEBUG
SCLog(@"throwEvent param:%@",param);
#endif
if (self.jsCallbackBlock != nil) {
id response = self.jsCallbackBlock(method, param,callback);
// if ([method isEqualToString:@"getAppList"]) {
// return nil;
// }
if (response != nil && ![callback isMemberOfClass:[WebUndefined class]] && [callback isKindOfClass:[NSString class]] && callback.length > 0) {
id res = [self callWebScriptMethod:callback withArguments:@[response]];
return res;
}
}
return nil;
}
- (id)callWebScriptMethod:(NSString *)name withArguments:(NSArray *)arguments{
SCLogI(@"before: js call method : %@",name);
if (arguments.count == 0
|| arguments == nil)
{
return nil;
}
__block id res;
@try {
BEGIN_INVOKE_ON_MAIN_THREAD
res = [[self windowScriptObject] callWebScriptMethod:name withArguments:arguments];
END_INVOKE_ON_MAIN_THREAD
}
@catch (NSException *exception) {
SCLogException(exception);
}
SCLogI(@"after: js call method : %@",name);
return res;
}
//禁用拖拽,pdf拖进去显示。。
-(NSDragOperation)draggingEntered:(id<NSDraggingInfo>)draggingInfo {
return NSDragOperationNone;
}
// Stop the NSTableView implementation getting in the way
- (NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender
{
return NSDragOperationNone;
}
-(BOOL)performDragOperation:(id<NSDraggingInfo>)draggingInfo {
return NO;
}
@end