[webkit-dev] Need advise and code review for use of JSAPI to read and write html values from C++

Richard Bailey richoncode at gmail.com
Tue Mar 18 09:51:13 PDT 2008


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?

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;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.webkit.org/pipermail/webkit-dev/attachments/20080318/a2592762/attachment-0001.html 


More information about the webkit-dev mailing list