0

I have a cocoa plugin embedded on an html page. This page also defines a javascript function in the header. I'd like to programmatically call this javascript function from my plugin. This was easy enough under IE/firefox/chrome plugins. How does this work under cocoa/safari? I think the problem comes down to accessing the pages webview and getting the webScriptObject. Once I have this I can use "callWebScriptMethod" af follows:

[scriptObject callWebScriptMethod:@"sayHello" withArguments:[NSArray arrayWithObjects:"@chris"]];

However, I don't know how to access the webview of the page hosting my plugin. My plugin is defined as "WASafariPluginView : NSView " and I don't see anything in its object hierarchy I can use to obtain the "parent" webview.

Thanks,

1 Answer 1

1

When your plug-in view is instantiated, WebKit calls the +plugInViewWithArguments: method of your view's main class.

The arguments parameter to that method is a dictionary that you can query for various information. In your case, you want the object corresponding to the WebPlugInContainerKey.

This is an object conforming to the WebPlugInContainer informal protocol. If it is not nil, you can ask this object for its -webFrame, which returns a WebFrame object. You can then ask the WebFrame object for its -webView.

You can then instantiate your plug-in and store a reference to the 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];
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.