Callback C++ in Javascript

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

Callback C++ in Javascript

Postby Staxcelrom » Wed Jul 06, 2022 11:01 am

Hello,

Can you please explain whether it is possible:
-to call c++ callback from js code and
-return the value of a variable from js to c++ code?


1.For example:

Code: Select all
 (*frame).ExecuteJavaScript("window.addEventListener('click', function(event){alert(event.pageX + \":\" + (event.pageY))}, false)", "", 1);


it is possible:
-to call c++ callback from js code ?
-return the value of a variable event.pageX from js to c++ variable ?


2.Or such an example:

Code: Select all
void OnContextCreated(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefV8Context> context)  override
      {
           CefRefPtr<CefV8Value> object = context->GetGlobal();
           CefRefPtr<CefV8Value> int_ = CefV8Value::CreateInt(555);
           object->SetValue("my_int", int_, V8_PROPERTY_ATTRIBUTE_NONE);

           (*frame).ExecuteJavaScript("my_int = 777; alert(my_int,false)", "", 1);

                std::cout <<"my_int:" << (*int_).GetIntValue() << std::endl;  //What needs to be done so that the "int_" has the value 777 changed in the js code?
           }




I am reading this description: https://bitbucket.org/chromiumembedded/cef/wiki/JavaScriptIntegration.md
But I still can't figure out if it's possible to do this or not.
Staxcelrom
Expert
 
Posts: 206
Joined: Wed Jan 26, 2022 8:20 am

Re: Callback C++ in Javascript

Postby Staxcelrom » Fri Jul 15, 2022 8:41 am

No one can tell? :(
Staxcelrom
Expert
 
Posts: 206
Joined: Wed Jan 26, 2022 8:20 am

Re: Callback C++ in Javascript

Postby bill » Sat Aug 06, 2022 3:14 pm

The framework I used was something like
injecting the function name into the loaded page and then checking if that function name is being called.

#define FN_CALL_NAME "MyFunction"

In cef_render_process_handler.c in
on_context_created(
struct _cef_render_process_handler_t* self,
struct _cef_browser_t* browser, struct _cef_frame_t* frame,
struct _cef_v8context_t* context)
{
cef_string_t name = {0};
cef_v8value_t *func = NULL, *global = NULL;
cef_v8handler_t *handler = NULL;
context->enter(context);
global = context->get_global(context);
handler = get_v8handler();
cef_string_set(MAKE_WIDE(FN_CALL_NAME), strlen(FN_CALL_NAME), &name, 0);
handler->base.add_ref((cef_base_t*)handler);
func = cef_v8value_create_function(&name, handler);
global->set_value_bykey(global, &name, func, V8_PROPERTY_ATTRIBUTE_NONE);
cef_string_clear(&name);
context->exit(context);
..

In cef_v8handler.c in
execute(struct _cef_v8handler_t* self,
const cef_string_t* name, struct _cef_v8value_t* object,
size_t argumentsCount, struct _cef_v8value_t* const* arguments,
struct _cef_v8value_t** retval, cef_string_t* exception)
...
cef_string_utf8_t out = {0};
cef_string_to_utf8(name->str, name->length, &out);
if (strcmp(out.str, FN_CALL_NAME) == 0)
extract the V8 arguments and coerce them into C arguments
do your stuff
bill
Newbie
 
Posts: 4
Joined: Wed Aug 03, 2022 12:56 pm


Return to Support Forum

Who is online

Users browsing this forum: No registered users and 35 guests