[webkit-dev] Need advise and code review for use of JSAPI to read and write html values from C++
Maciej Stachowiak
mjs at apple.com
Tue Mar 18 13:48:40 PDT 2008
On Mar 18, 2008, at 9:51 AM, Richard Bailey wrote:
>
> I looked for but did not find samples for doing this. If I missed
> such samples and documentation, please send me links.
>
> The code below is my first draft (compiled and verified) to read and
> write values from an HTML page.
>
> I appreciate any suggestions you can offer.
>
> Also, is the JS API the best route to use?
It's best to use the native DOM API, if your platform supports it. For
the Mac port, the Objective-C DOM API would be the way to go. I don't
know if other ports support this yet, in which case, what you are
doing is ok. You shouldn't need the newline in your script though.
>
>
> Thanks!
> Richard
>
>
> static JSValueRef myMesssageBox(JSContextRef ctx, JSObjectRef /
> *function*/, JSObjectRef thisObject, size_t argumentCount, const
> JSValueRef arguments[], JSValueRef* /*exception*/)
> {
> JSValueRef undefined = JSValueMakeUndefined(ctx);
>
> MessageBox( NULL, TEXT("Test"), TEXT("test2"), MB_OK);
> return undefined;
> }
>
> static JSValueRef myTestReadPageData(JSContextRef ctx, JSObjectRef /
> *function*/, JSObjectRef thisObject, size_t argumentCount, const
> JSValueRef arguments[], JSValueRef* /*exception*/)
> {
> JSValueRef undefined = JSValueMakeUndefined(ctx);
>
> const char* const script =
> " document.getElementById('editableText').innerHTML;\
> ";
>
> JSStringRef scriptRef = JSStringCreateWithUTF8CString(script);
>
> JSValueRef exception = 0;
> JSValueRef resultRef = 0;
> resultRef = JSEvaluateScript(ctx, scriptRef, 0, 0, 0, &exception);
> _ASSERT(!exception);
>
> JSStringRef strResult = JSValueToStringCopy( ctx, resultRef,
> &exception);
> _ASSERT(!exception);
>
> size_t strLen = JSStringGetLength(strResult);
> const JSChar* jsstr = JSStringGetCharactersPtr( strResult);
>
>
> TCHAR str[MAX_PATH];
> wcsncpy( str, jsstr, strLen);
> str[strLen] = 0;
>
> MessageBox( NULL, str, TEXT("test2"), MB_OK);
> return undefined;
> }
>
> static JSValueRef myTestWritePageData(JSContextRef ctx, JSObjectRef /
> *function*/, JSObjectRef thisObject, size_t argumentCount, const
> JSValueRef arguments[], JSValueRef* /*exception*/)
> {
> JSValueRef undefined = JSValueMakeUndefined(ctx);
>
> const char* const script =
> " document.getElementById('setableText').innerHTML='Text set
> from CPP';\
> ";
>
> JSStringRef scriptRef = JSStringCreateWithUTF8CString(script);
>
> JSValueRef exception = 0;
> JSValueRef resultRef = 0;
> resultRef = JSEvaluateScript(ctx, scriptRef, 0, 0, 0, &exception);
> _ASSERT(!exception);
>
> JSStringRef strResult = JSValueToStringCopy( ctx, resultRef,
> &exception);
> _ASSERT(!exception);
>
> size_t strLen = JSStringGetLength(strResult);
> const JSChar* jsstr = JSStringGetCharactersPtr( strResult);
>
>
> TCHAR str[MAX_PATH];
> wcsncpy( str, jsstr, strLen);
> str[strLen] = 0;
>
> MessageBox( NULL, str, TEXT("test2"), MB_OK);
> return undefined;
> }
>
>
> HRESULT WinPrototypeWebHost::windowScriptObjectAvailable(
> /* [in] */ IWebView *webView,
> /* [in] */ JSContextRef context,
> /* [in] */ JSObjectRef windowScriptObject)
> {
> if (!m_mainFrame)
> return S_OK;
>
> static JSStaticFunction staticFunctions[] = {
> { "MyMessageBox", myMesssageBox, kJSPropertyAttributeNone },
> { "TestReadPageData", myTestReadPageData,
> kJSPropertyAttributeNone },
> { "TestWritePageData", myTestWritePageData,
> kJSPropertyAttributeNone },
> { 0, 0, 0 }
> };
>
> JSClassDefinition myControllerDefinition = {
> 0, kJSClassAttributeNone, "MyController", 0, 0,
> staticFunctions,
> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
> };
>
> JSClassRef controllerClass =
> JSClassCreate(&myControllerDefinition);
> _ASSERT(controllerClass);
>
> m_controllerScriptObject = JSObjectMake(context,
> controllerClass, reinterpret_cast<void*>(this));
> _ASSERT(m_controllerScriptObject);
>
> JSRetainPtr<JSStringRef> controllerObjectString(Adopt,
> JSStringCreateWithUTF8CString("MyController"));
> JSObjectSetProperty(context, windowScriptObject,
> controllerObjectString.get(), m_controllerScriptObject,
> kJSPropertyAttributeNone, 0);
>
> return S_OK;
> }
>
>
> _______________________________________________
> webkit-dev mailing list
> webkit-dev at lists.webkit.org
> http://lists.webkit.org/mailman/listinfo.cgi/webkit-dev
More information about the webkit-dev
mailing list