[Cocoa-sharp] URL, URLRequest, WebScriptObject patch
netshade
netshade at gmail.com
Tue Sep 12 14:13:13 EDT 2006
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/cocoa-sharp/attachments/20060912/80c195c6/attachment.html
-------------- next part --------------
A non-text attachment was scrubbed...
Name: cocoa-webkit.patch
Type: application/octet-stream
Size: 5673 bytes
Desc: not available
Url : http://lists.ximian.com/pipermail/cocoa-sharp/attachments/20060912/80c195c6/attachment.obj
-------------- next part --------------
A non-text attachment was scrubbed...
Name: WebScriptObject.cs
Type: application/octet-stream
Size: 1628 bytes
Desc: not available
Url : http://lists.ximian.com/pipermail/cocoa-sharp/attachments/20060912/80c195c6/attachment-0001.obj
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ConcreteMutableData.cs
Type: application/octet-stream
Size: 295 bytes
Desc: not available
Url : http://lists.ximian.com/pipermail/cocoa-sharp/attachments/20060912/80c195c6/attachment-0002.obj
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Enumerator.cs
Type: application/octet-stream
Size: 259 bytes
Desc: not available
Url : http://lists.ximian.com/pipermail/cocoa-sharp/attachments/20060912/80c195c6/attachment-0003.obj
-------------- next part --------------
A non-text attachment was scrubbed...
Name: IdEnumerator.cs
Type: application/octet-stream
Size: 267 bytes
Desc: not available
Url : http://lists.ximian.com/pipermail/cocoa-sharp/attachments/20060912/80c195c6/attachment-0004.obj
-------------- next part --------------
A non-text attachment was scrubbed...
Name: PathStore2.cs
Type: application/octet-stream
Size: 259 bytes
Desc: not available
Url : http://lists.ximian.com/pipermail/cocoa-sharp/attachments/20060912/80c195c6/attachment-0005.obj
More information about the Cocoa-sharp
mailing list