Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

543
Vistas
CEF - how to call callback c++ form JS-code

How can I call a C++ function using JavaScript?

For example, I am executing js code in a browser window using the method CefFrame::ExecuteJavaScript like this:

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

Is it possible to somehow call a C++ function for the place of the JS alert() function?

about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

It can be done in two ways. They are almost equal: you can create several native functions at once with CefRegisterExtension, and you can create a single native function with CefV8Value::CreateFunction. The example bellow is just a sketch, nowhere can test it, small issues are possible, but the idea is clear:

class MyAlertHandler : public CefV8Handler {
 public:
  bool Execute(const CefString& name,
               CefRefPtr<CefV8Value> object,
               const CefV8ValueList& arguments,
               CefRefPtr<CefV8Value>& retval,
               CefString& exception) override {
    if (!arguments.empty()) {
      // arguments[0]->GetStringValue();
    }
    return true;
  }
};

CefRefPtr<CefV8Handler> handler = new MyAlertHandler;

CefRefPtr<CefV8Context> v8_context = frame->GetMainFrame()->GetV8Context();
if (v8_context.get() && v8_context->Enter()) {
  CefRefPtr<CefV8Value> global = v8_context->GetGlobal();
  CefRefPtr<CefV8Value> my_alert = CefV8Value::CreateFunction("my_alert", handler);
  global->SetValue("my_alert", my_alert, V8_PROPERTY_ATTRIBUTE_READONLY);
  v8_context->Exit();
}

Calling Enter() and Exit() on a V8 context can be omitted if CefV8Value::CreateFunction is called from CefRenderProcessHandler functions.

frame->GetMainFrame()->ExecuteJavaScript(R"(
  const elem = document.getElementsByClassName("my_class")[0];
  const rect = elem.getBoundingClientRect();
  my_alert(rect.x + ":" + rect.y);)", "", 1);
about 4 years ago · Santiago Gelvez Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda