[Cocoa-sharp] URL, URLRequest, WebScriptObject patch
C.J. Adams-Collier
cjcollier at colliertech.org
Thu Sep 14 15:35:05 EDT 2006
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Please include an overview in the root ChangeLog. Geoff and I will go
over the patches tomorrow around noon, U.S. Pacific time
netshade wrote:
> Included is a patch against cocoa# and webkit# to add the
> windowScriptObject prop to WebView, and to add loading by
> URLRequest to WebFrame . There is also a fix in ObjCClass removing
> a reference to contains() on the registered class table, when I
> imagine it should have been containsKey().
>
> Also included is a batch of new files, most of which honestly I
> have no idea what they do. However, in the course of working on my
> own app, I got exceptions thrown by ObjCClass showing the classes
> as missing. Adding them seemed to shut it up.
>
> I should add that the reason I added these items was because I
> wanted to bind a mono object to a Javascript variable in the
> WebView. I now have that working, and will give a quick overview
> on how to do it yourself:
>
> ( In the interests of time, skipping typical cocoa# setup junk)
>
> Create a class that subclasses Cocoa.Object. Aside from your
> constructors, add three methods ( stubbed below ):
>
> [Export("isSelectorExcludedFromWebScript:")] public static bool
> _isSelectorExcludedFromWebScript(string selector) { return false; }
>
>
> [Export("webScriptNameForSelector:")] public static Cocoa.String
> _webScriptNameForSelector(string selector){ return new
> Cocoa.String("console"); }
>
> [Export("console:")] public void console(IntPtr cob){ Cocoa.Object
> ob = Cocoa.Object.FromIntPtr(cob); Console.WriteLine("FROM MONO
> "+ob.ToString()); }
>
>
> The top two method exports (isSelectorExcludedFromwebScript and
> webScriptNameForSelector) are taken from the informal WebKit
> scripting protocol ( the googling for which should take you to the
> official Apple docs ). isSelectorExcludedFromWebScript: is called
> to check if the method referred to by a given selector is able to
> be called; in this case, we just return false and allow every
> method in the class to be called. webScriptNameForSelector: is
> called to ascertain what the JavaScript name for a given method
> will be; if this method didn't exist, all the methods in your class
> would be munged up according to those scripting protocol docs I
> mentioned. ( Namely, colons : would become underscores _ )
>
> Finally we export the console method to make it visible to WebKit.
>
> This object you've created will be the object that you insert into
> the WebView's javascript runtime. In order to do that, you'll need
> to edit your application controller ( or wherever you're working
> with the WebView - and if you're lost, consult the CocoaDoc
> application in SVN to figure out how to get a WebView in your
> Cocoa# app and work with it ) to act as a delegate for the WebView
> ( for reasons I'm about to mention ).
>
> In interface builder, create a connection between your webview and
> your application controller ( or whatever ) by ctrl+clicking on the
> webview and dragging the line that appears to the controller. Set
> the frameLoadDelegate of your WebView to the application
> controller. Then add a method similar to the following to your
> application controller:
>
> [Export("webView:windowScriptObjectAvailable:")] public void
> HandleLoad(WebView pane,WebScriptObject wso){ MyScriptableObject ob
> = new MyScriptableObject(); //above would be the object you created
> earlier
>
> wso.SetValueForKey(new Cocoa.String("logger"),ob); }
>
>
>
> You'll also want to add a directive somewhere in here ( in a method
> that responds to awakeFromNib:, for instance ) that tells the
> WebFrame to load up an HTML file that has content similar to the
> following:
>
> <script language="JavaScript"> logger.console(".. to Objective C to
> JavaScript"); </script>
>
>
> You should see a line like this printed out to the console:
>
>> FROM MONO .. to Objective C to JavaScript
>
>
> Hope someone else finds this useful.
>
>
> CZ
>
>
> ----------------------------------------------------------------------
>
>
> _______________________________________________ Cocoa-sharp mailing
> list Cocoa-sharp at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/cocoa-sharp
- --
<cjcollier at colliertech.org>
http://wp.colliertech.org/cj/
+1 206 226 5809
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFFCa7pbS8rWWzCfqgRAul4AJ4hAb+qoGYFFh6sCb3HWnBvy1lVrgCgpdw/
nZZvW0Wvt8B5+ov0TxJANjI=
=4hEW
-----END PGP SIGNATURE-----
More information about the Cocoa-sharp
mailing list