我在html页面上嵌入了一个cocoa插件。该页面还在标题中定义了一个javascript函数。我想以编程方式从我的插件中调用此javascript函数。这在IE / firefox / chrome插件下很容易。这如何在可可/野生动物园下工作?我认为问题归结为访问webview页面并获取webScriptObject。有了这个,我可以使用“callWebScriptMethod”af:
[scriptObject callWebScriptMethod:@"sayHello" withArguments:[NSArray arrayWithObjects:"@chris"]];
但是,我不知道如何访问托管我的插件的页面的webview。我的插件被定义为“WASafariPluginView:NSView”,我在其对象层次结构中看不到任何可用于获取“父”webview的内容。
谢谢,
答案 0 :(得分:1)
当您的插件视图被实例化时,WebKit会调用视图主类的+plugInViewWithArguments:
方法。
该方法的arguments参数是一个字典,您可以查询各种信息。在您的情况下,您希望对象与WebPlugInContainerKey
。
这是符合WebPlugInContainer
非正式协议的对象。如果它不是nil
,您可以向此对象询问其-webFrame
,它返回WebFrame
个对象。然后,您可以向WebFrame
对象询问其-webView
。
然后,您可以实例化插件并存储对WebView
的引用。
<强> YourPluginView.h:强>
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
@interface YourPluginView : NSView <WebPlugInViewFactory>
{
WebView* webViewIvar;
}
- (id)initWithWebView:(WebView *)aWebView;
@end
<强> YourPluginView.m:强>
//the WebPlugInViewFactory protocol required method
+ (NSView *)plugInViewWithArguments:(NSDictionary *)arguments
{
WebView* containerView = [[[arguments objectForKey:WebPlugInContainerKey] webFrame] webView];
YourPluginView* view = [[self alloc] initWithWebView:containerView];
return view;
}
- (id)initWithWebView:(WebView *)aWebView
{
self = [super init];
if(self)
{
webViewIvar = [aWebView retain];
}
return self;
}
- (void)dealloc
{
[webViewIvar release];
[super dealloc];
}