How to use CefV8Context?

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.

How to use CefV8Context?

Postby eren » Mon Jul 07, 2014 9:55 am

So i'm using CEF3 and i need to do the following things:
1) wait until some HTML page is fully loaded;
2) then execute my own JavaScript function;
3) then read the result of this function into CefString.

I tried to use CefV8Context in ClientHandler::OnLoadEnd function.
Code: Select all
void ClientHandler::OnLoadEnd(CefRefPtr<CefBrowser> browser,
                       CefRefPtr<CefFrame> frame,
                       int httpStatusCode)
{
   if ((httpStatusCode != 0) && frame->IsMain())
   {
      CefRefPtr<CefV8Context> v8Context = frame->GetV8Context();
      CefRefPtr<CefV8Value> globalObj = v8Context->GetGlobal();
      
      CefRefPtr<CefV8Handler> handler = …;
      CefRefPtr<CefV8Value> func = CefV8Value::CreateFunction("myfunc", handler);

      CefV8ValueList args;
      CefRefPtr<CefV8Exception> exception;
      CefRefPtr<CefV8Value> retVal = evalFunc->ExecuteFunctionWithContext(v8Context, globalObj, args);
      CefString retValStr = retVal->GetStringValue();
   }
}

But application crashes on
CefRefPtr<CefV8Value> globalObj = v8Context->GetGlobal();
string.

What am i doing wrong?
And can this be made via CefRegisterExtension? Because I don't actually want to rewrite the native JavaScript code into CEF native implementation.
CEF v3.1650.1567_windows32
eren
Techie
 
Posts: 25
Joined: Mon Feb 03, 2014 8:11 am

Re: How to use CefV8Context?

Postby magreenblatt » Mon Jul 07, 2014 11:27 am

V8 can only be used in the renderer process. ClientHandler::OnLoadEnd is probably called in the browser process. See https://code.google.com/p/chromiumembed ... h_Contexts.
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: How to use CefV8Context?

Postby eren » Tue Jul 08, 2014 2:07 am

So how do i achieve this:
1) wait until some HTML page is fully loaded;
2) then execute my own JavaScript function;
3) then read the result of this function into CefString.
?
Do i need to call CefPostTask(TID_RENDERER, task) from ClientHandler::OnLoadEnd function? Or is there a better ways?
CEF v3.1650.1567_windows32
eren
Techie
 
Posts: 25
Joined: Mon Feb 03, 2014 8:11 am

Re: How to use CefV8Context?

Postby magreenblatt » Tue Jul 08, 2014 10:08 am

1) wait until some HTML page is fully loaded;

Use document.onload via JS if you control the HTML contents or CefLoadHandler::OnLoadingStateChange via native code. CefLoadHandler is also supported in the renderer process with newer CEF versions via CefRenderProcessHandler::GetLoadHandler.
2) then execute my own JavaScript function;

Use any of the techniques described on the JavaScriptIntegration wiki page.
3) then read the result of this function into CefString.

What process do you need the result in? If the browser process then send a message from the renderer process after executing your JS function as described in https://code.google.com/p/chromiumembed ... e_Messages.
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: How to use CefV8Context?

Postby eren » Thu Jul 10, 2014 8:13 am

Thank you, magreenblatt! This is exaclty what i was looking for.
CEF v3.1650.1567_windows32
eren
Techie
 
Posts: 25
Joined: Mon Feb 03, 2014 8:11 am


Return to Support Forum

Who is online

Users browsing this forum: No registered users and 52 guests