Search code examples
c++chromiumchromium-embedded

How to get JS Object in Chromium Embedded Framework (CEF)?


Is it possible to get an existing object reference using CEF API?

For example I run a script using ExecuteJavaScript()

function foo()
{
    var self = this;
    self.value="some value";
}
var fooObj = new foo;

This script creates a new variable fooObj. It is possible to get a reference to this variable later in the C++ code and to modify its value?


Solution

  • You should be able to get it by doing something like the following (untested):

        auto context = AppGetBrowser()->GetMainFrame()->GetV8Context();
        CefRefPtr<CefV8Value> p = context->GetGlobal()->GetValue(CefString("fooObj"));
    

    You may need to Enter/Exit the context depending on where you're calling it from in C++. Furthermore you may need to actually reference your object explicitly as 'window.fooObj' in which case you'll have to get the value for 'window' and then get 'fooObj' off that.

    (edit - accidentally posted too early) (edit 2 - more)