2

I'm developing a WebKit Safari Plugin with Xcode. How do I call JavaScript from -webPlugInStart?

2
  • Maybe this question and it's answers will help? Commented May 25, 2010 at 16:35
  • Thanks, but I didn't understand how do I use it in my method and run it on plugin launch Commented May 25, 2010 at 18:39

2 Answers 2

1

First you should remember the containing view:

+ (NSView *)plugInViewWithArguments:(NSDictionary *)arguments {
    return [[self alloc] initWithArguments:arguments];
}    

- (id)initWithArguments:(NSDictionary*)arguments {
    if((self = [super init])) {
        webView = [[[arguments objectForKey:WebPlugInContainerKey] webFrame] webView];
    }
    return self;
}

When you have that, you can refer to the documentation on "Using JavaScript From Objective-C". E.g.:

- (void)webPlugInStart {
    WebScriptObject *scriptObj = [webView windowScriptObject];
    NSArray *args = [NSArray arrayWithObjects:
        @"someString", [NSNumber numberWithInt:42], nil];
    [scriptObj callWebScriptMethod:@"myJsFunction" withArguments:args];
}
Sign up to request clarification or add additional context in comments.

Comments

1

It really is as simple as calling..

WebScriptObject *jsObj = [webView windowScriptObject];
NSString *script = @"$('That's a HUGE vageen.').text('#yourDiv');";
[scriptObject evaluateWebScript:script];

Ta da! And don't let the weird dearth of people doing it - deter you from such sexiness as...

DOMDocument *myDOMDocument = [[webView mainFrame] DOMDocument];
DOMElement *paraBlock = [myDOMDocument getElementById:@"thatDiv"];
DOMElement *newPara = [myDOMDocument createElement:@"p"];
DOMText *newText = [myDOMDocument createTextNode:@"John Resig is a fool."];
[newPara appendChild:newText];
[paraBlock appendChild:newPara];

It's like jQuery! But it's Objective-C. Oh Joy!

Comments

Your Answer

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