[webkit-dev] KJS::JSObject question

Jack Wootton jackwootton at gmail.com
Thu May 21 03:49:49 PDT 2009


Hello,

Having read through the following document "Using JavaScript From Objective-C":

http://developer.apple.com/documentation/AppleApplications/Conceptual/SafariJSProgTopics/Tasks/ObjCFromJavaScript.html#//apple_ref/doc/uid/30001215-BBCBFJCD

It seems that the method:

WebView::windowScriptObject

would be useful to achieve what I wish to do (add a custom JavaScript
object).  However looking at the source code, it is not implemented on
windows:

http://trac.webkit.org/browser/trunk/WebKit/win/WebView.cpp#L2709



On Sun, May 17, 2009 at 6:30 PM, Darin Adler <darin at apple.com> wrote:
> On May 15, 2009, at 7:35 AM, Jack Wootton wrote:
>
>> 4. Create the context:
>>
>> JSGlobalContextRef context = JSGlobalContextCreate(globalObjectClass);
>
> You will only do this if you’re using JavaScript outside of a web page. If
> you want to do this in conjunction with WebKit you’ll need to get the
> context from WebKit APIs. On Mac OS X the relevant API is -[WebFrame
> globalContext].
>
>> 1. Who should share the context?  Is a single context used for a single
>> webpage / frame?
>
> See my answer above.
>
>> 2. Where do I define the class that will actually handle the
>> implementation of any methods on my new object?
>
> I don’t understand the “where” question. Do it wherever makes sense in your
> program, and call JSClassCreate. Then when it's time to make an object, pass
> the JSClassRef to JSObjectMake.
>
>> 3. The object has been made using "JSObjectMake", but how is it added to
>> WebKit?
>
> A common thing to do is to create an object and then put it in a property of
> the window object, which is the global object inside the web browser. That’s
> done on Mac OS X by implementing the webView:windowScriptObjectAvailable:
> method of the frame load delegate. The window script object is a
> WebScriptObject, and you can get the JSObject version of it by calling the
> JSObject method on it. Then you can set your object as a property of the
> window object with the JSObjectSetProperty function.
>
>    -- Darin
>
>



-- 
Regards
Jack


More information about the webkit-dev mailing list