可可 – 嵌入式Webkit – 脚本回调怎么样?

可可 – 嵌入式Webkit – 脚本回调怎么样?,第1张

概述在 Windows上,当将“Shell.Explorer”ActiveX控件嵌入到应用程序中时,可以注册一个实现IDispatch的对象上的“外部”处理程序,以便Web页面上的脚本可以调用到主机应用程序. <button onclick="window.external.Test('called from script code')">test</button> 现在,ive移动到Mac开发,并认 在 Windows上,当将“Shell.Explorer”ActiveX控件嵌入到应用程序中时,可以注册一个实现Idispatch的对象上的“外部”处理程序,以便Web页面上的脚本可以调用到主机应用程序.

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

现在,ive移动到Mac开发,并认为我可以从嵌入在我的Cocoa应用程序中的WebKit获得类似的工作.但是,似乎没有任何设施允许脚本回拨到主机应用程序.

一个建议是挂起window.alert,并获得脚本来传递格式化的消息字符串作为警报字符串.
我也想知道WebKit是否可能被引导到使用NPPVpluginScriptableNPObject的应用程序托管的NPAPI插件.

我错过了什么吗?是否真的很难托管WebVIEw并允许脚本与主机进行交互?

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

@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

在你的控制器中实现这个代码之后,你可以调用Cocoa.log(‘foo’);来自JavaScript环境和logJavaScriptString:方法将被调用.

总结

以上是内存溢出为你收集整理的可可 – 嵌入式Webkit – 脚本回调怎么样?全部内容,希望文章能够帮你解决可可 – 嵌入式Webkit – 脚本回调怎么样?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1028059.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存