2626
2727namespace Rtt
2828{
29-
3029// ----------------------------------------------------------------------------
3130
31+ static const char * kCoronaEventPrefix = " JS_" ;
32+
3233AndroidWebViewObject::AndroidWebViewObject (
3334 const Rect& bounds, AndroidDisplayObjectRegistry *displayObjectRegistry, NativeToJavaBridge *ntjb )
3435: Super( bounds, displayObjectRegistry, ntjb ),
@@ -251,6 +252,85 @@ AndroidWebViewObject::DeleteCookies( lua_State *L )
251252 return 0 ;
252253}
253254
255+ int
256+ AndroidWebViewObject::InjectJS ( lua_State *L )
257+ {
258+ const LuaProxyVTable& table = PlatformDisplayObject::GetWebViewObjectProxyVTable ();
259+ AndroidWebViewObject *view = (AndroidWebViewObject *)luaL_todisplayobject (L, 1 , table);
260+ if ( view )
261+ {
262+ const char *jsCode = lua_tostring ( L, 2 );
263+ view->InjectJSCode ( jsCode );
264+ }
265+
266+ return 0 ;
267+ }
268+
269+ int
270+ AndroidWebViewObject::RegisterCallback ( lua_State *L )
271+ {
272+ const LuaProxyVTable& table = PlatformDisplayObject::GetWebViewObjectProxyVTable ();
273+ AndroidWebViewObject *view = (AndroidWebViewObject *)luaL_todisplayobject ( L, 1 , table );
274+ if ( view )
275+ {
276+ const char *eventName = lua_tostring ( L, 2 );
277+ String jsEventName (kCoronaEventPrefix );
278+ jsEventName.Append ( eventName );
279+ view->AddEventListener ( L, 3 , jsEventName.GetString () );
280+ }
281+
282+ return 0 ;
283+ }
284+
285+ int
286+ AndroidWebViewObject::On ( lua_State *L )
287+ {
288+ const LuaProxyVTable& table = PlatformDisplayObject::GetWebViewObjectProxyVTable ();
289+ AndroidWebViewObject *view = (AndroidWebViewObject *)luaL_todisplayobject ( L, 1 , table );
290+ if ( view )
291+ {
292+ const char *eventName = lua_tostring ( L, 2 );
293+ String jsEventName (kCoronaEventPrefix );
294+ jsEventName.Append ( eventName );
295+ view->AddEventListener ( L, 3 , jsEventName.GetString () );
296+ }
297+
298+ return 0 ;
299+ }
300+
301+ int
302+ AndroidWebViewObject::Send ( lua_State *L )
303+ {
304+ const LuaProxyVTable& table = PlatformDisplayObject::GetWebViewObjectProxyVTable ();
305+ AndroidWebViewObject *view = (AndroidWebViewObject *)luaL_todisplayobject ( L, 1 , table );
306+ if ( view )
307+ {
308+ const char * eventName = lua_tostring ( L, 2 );
309+ const char * jsonContent = " {}" ;
310+ if ( 0 == LuaContext::JsonEncode ( L, 3 ) )
311+ {
312+ jsonContent = lua_tostring ( L, -1 );
313+ }
314+
315+ String s ( " window.dispatchEvent(new CustomEvent('" );
316+ s.Append ( kCoronaEventPrefix );
317+ s.Append ( eventName );
318+ s.Append ( " ', {detail: " );
319+ s.Append ( jsonContent );
320+ s.Append ( " }));" );
321+
322+ view->InjectJSCode ( s.GetString () );
323+ }
324+
325+ return 0 ;
326+ }
327+
328+ void
329+ AndroidWebViewObject::InjectJSCode ( const char *jsCode )
330+ {
331+ fNativeToJavaBridge ->WebViewRequestInjectJS ( GetId (), jsCode );
332+ }
333+
254334int
255335AndroidWebViewObject::ValueForKey ( lua_State *L, const char key[] ) const
256336{
@@ -263,6 +343,26 @@ AndroidWebViewObject::ValueForKey( lua_State *L, const char key[] ) const
263343 lua_pushlightuserdata ( L, fNativeToJavaBridge );
264344 lua_pushcclosure ( L, Request, 1 );
265345 }
346+ else if ( strcmp ( " injectJS" , key ) == 0 )
347+ {
348+ lua_pushlightuserdata ( L, fNativeToJavaBridge );
349+ lua_pushcclosure ( L, InjectJS, 1 );
350+ }
351+ else if ( strcmp ( " registerCallback" , key ) == 0 )
352+ {
353+ lua_pushlightuserdata ( L, fNativeToJavaBridge );
354+ lua_pushcclosure ( L, RegisterCallback, 1 );
355+ }
356+ else if ( strcmp ( " on" , key ) == 0 )
357+ {
358+ lua_pushlightuserdata ( L, fNativeToJavaBridge );
359+ lua_pushcclosure ( L, On, 1 );
360+ }
361+ else if ( strcmp ( " send" , key ) == 0 )
362+ {
363+ lua_pushlightuserdata ( L, fNativeToJavaBridge );
364+ lua_pushcclosure ( L, Send, 1 );
365+ }
266366 else if ( strcmp ( " stop" , key ) == 0 )
267367 {
268368 lua_pushlightuserdata ( L, fNativeToJavaBridge );
0 commit comments