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; }
_______________________________________________ webkit-dev mailing list webkit-dev@lists.webkit.org http://lists.webkit.org/mailman/listinfo.cgi/webkit-dev