Parmanoir

Exposing everything to WebKit

When using WebKit with Cocoa, you can expose any ObjC object to Javascript. But there's a catch : you need to specify which keys and selectors are allowed to be called via isKeyExcludedFromWebScript and isSelectorExcludedFromWebScript. But what if we want to expose everything ? Just add these methods to NSobject with a category :
@implementation NSObject(ExposeEverything)

+ (BOOL)isSelectorExcludedFromWebScript:(SEL)selector {
	return NO;
}
+ (BOOL)isKeyExcludedFromWebScript:(const char *)property {
	return NO;
}

@end

Now you can expose a dummy object that returns NSApp, and call in your JS code myExposedObject.NSApp().keyWindow().setHasShadow_(false). Pretty neat :), save for the trailing underscore.

Why expose everything ? I've tried RubyCocoa for Cocoa development and have had strange bugs (crashing when adding a comment), plus I don't really like Ruby as a language. It doesn't handle the increment operator ++, multiline comments, and has a strange syntax for blocks — compared to Javascript who uses the unterse function (arg1, arg2) everywhere for functions and lambdas. And Javascript's (scope | inheritance) model, where a (function/lambda | object) inherit its parent (scope | prototype), is just so elegant. I can say I grok Javascript where everything is pretty simple and powerful, even if it's limited, but Ruby just confuses me. I wonder how much development can be done in JS instead of ObjC or RubyCocoa … I'll be exploring.

kourge
2008 06 18

Ruby does have multiline comments. Multilines comments begin with =being and end with =end, although this confused me a lot when I first learned Ruby.

Also, coming from JavaScript to Ruby, I prefer the braces syntax over the do...end syntax when using blocks precisely because it's more syntactically similar to JavaScript.

Patrick Geiller
2008 06 18

=begin … =end confused me too, and googling '=begin' to guess why they need to be positioned exactly at the start of the line or they won't work whas a pain.

That said, there are things I really love in Ruby, namely the return if (condition), the ability to omit parens in if/while/…. Too bad we can't cherry pick features from languages to make our own :)


Follow me on Twitter
Planet Cocoa
Cocoa.fr

2011 02 22Distance field
2010 07 202Binding through NSApp
2010 05 122Forwarding invocations
2010 02 272Core Image black fringes
2010 02 21Quickest Way to Shell
2010 02 08Who's calling ?
2009 09 2138 ways to use Blocks in Snow Leopard
2009 08 182Bracket Mess
2009 08 124Taming JavascriptCore within and without WebView
2009 04 15Debugging with Activity Monitor
2009 03 25How Core Image Color Tracking works
2009 03 1510Custom NSThemeFrame
2009 03 10Which framework is running ?
2009 03 074CoreUI can paint pretty big
2009 02 18Localization with functions
2009 01 30Did you forget to nest alloc and init?
2009 01 16JSCocoa on the iPhone
2009 01 11Mixing WebView and JavascriptCore
2009 01 09Badge overflow
2009 01 09Find your Garbage Collection leaks with Instruments

Powered by MediaWiki