嵌入式Webkit - 脚本回调如何? [英] Embedded Webkit - script callbacks how?

查看:98
本文介绍了嵌入式Webkit - 脚本回调如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在窗口上,当Shell.ExplorerActiveX控件嵌入在应用程序中时,可以在实现IDispatch的对象上注册外部处理程序,以便网页上的脚本可以调用到主机应用程序。

On windows, when the "Shell.Explorer" ActiveX control is embedded in an application it is possible to register an "external" handler - on object that implements IDispatch, such that scripts on the web page can call out to the hosting application.

<button onclick="window.external.Test('called from script code')">test</button>



现在,移动到Mac开发,并认为我可以得到类似的工作从WebKit嵌入我的可可应用。但是,实际上似乎没有任何设施允许脚本调用回托管应用程序。

Now, ive moved to Mac development and thought I could get something similar working from WebKit embedded in my Cocoa application. But, there really doesn't seem to be any facility to allow scripts to call back out to the hosting application.

一个建议是钩 window.alert ,并获取脚本以格式化的消息字符串作为警报字符串。
Im还想知道是否可以使用NPPVpluginScriptableNPObject将WebKit定向到一个应用程序托管的NPAPI插件。

One piece of advice was to hook window.alert and get scripts to pass a formatted message string as the alert string. Im also wondering if WebKit can perhaps be directed to an application hosted NPAPI plugin using NPPVpluginScriptableNPObject.

我错过了什么?

推荐答案

您需要实现各种WebScripting协议方法。这里是一个基本的例子:

You need to implement the various WebScripting protocol methods. Here is a basic example:

@interface WebController : NSObject
{
    IBOutlet WebView* webView;
}

@end

@implementation WebController

//this returns a nice name for the method in the JavaScript environment
+(NSString*)webScriptNameForSelector:(SEL)sel
{
    if(sel == @selector(logJavaScriptString:))
        return @"log";
    return nil;
}

//this allows JavaScript to call the -logJavaScriptString: method
+ (BOOL)isSelectorExcludedFromWebScript:(SEL)sel
{
    if(sel == @selector(logJavaScriptString:))
        return NO;
    return YES;
}

//called when the nib objects are available, so do initial setup
- (void)awakeFromNib
{
    //set this class as the web view's frame load delegate 
    //we will then be notified when the scripting environment 
    //becomes available in the page
    [webView setFrameLoadDelegate:self];

    //load a file called 'page.html' from the app bundle into the WebView
    NSString* pagePath = [[NSBundle mainBundle] pathForResource:@"page" ofType:@"html"];
    NSURL* pageURL = [NSURL fileURLWithPath:pagePath];
    [[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:pageURL]];
}


//this is a simple log command
- (void)logJavaScriptString:(NSString*) logText
{
    NSLog(@"JavaScript: %@",logText);
}

//this is called as soon as the script environment is ready in the webview
- (void)webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)windowScriptObject forFrame:(WebFrame *)frame
{
    //add the controller to the script environment
    //the "Cocoa" object will now be available to JavaScript
    [windowScriptObject setValue:self forKey:@"Cocoa"];
}

@end

控制器,现在可以从JavaScript环境和 logJavaScriptString:方法调用 Cocoa.log('foo'); 将被调用。

After implementing this code in your controller, you can now call Cocoa.log('foo'); from the JavaScript environment and the logJavaScriptString: method will be called.

这篇关于嵌入式Webkit - 脚本回调如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆