Home > Net >  CEF - how to get a variable from JS to С
CEF - how to get a variable from JS to С

Time:07-16

Please help me, I've been suffering for two weeks, I can't understand if it's possible in CEF to get the value of a variable from the JS code to a C variable?

For example:

I am executing js code in a browser window using the method CefFrame::ExecuteJavaScript:

(*frame).GetMainFrame()).ExecuteJavaScript("const elem = document.getElementsByClassName("my_class")[0];const rect = elem.getBoundingClientRect(); alert(rect.x   \":\"   rect.y)", "", 1);

And everything works great, but only "inside" javascript.

Here's the question: is it possible to somehow get the value of a variable to a rect.x to pull out and put in a C variable int rect_x ?

CodePudding user response:

You might have noticed, if you type rect.x in the console in the developer tools, it echoes the variable value. It should have hinted you, how to retrieve variable values.

CefRefPtr<CefV8Context> v8_context = frame->GetMainFrame()->GetV8Context();
if (v8_context.get() && v8_context->Enter()) {
  CefRefPtr<CefV8Value> retval;
  CefRefPtr<CefV8Exception> exception;
  if (v8_context->Eval("rect.x", "", 0, retval, exception)) {
    int rect_x = retval->GetIntValue()
  }
  v8_context->Exit();
}
  • Related