[SOLVED] Need help with CefV8Context::Eval()

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.

Re: Need help with CefV8Context::Eval()

Postby Czarek » Mon Jul 11, 2016 3:39 am

I think you can accomplish the same using ExecuteJavascript to inject custom js on pages in CefLoadHandler and javascript bindings to communicate with C++ (see wiki pages for tutorial).

EDIT: to inject custom js from the Renderer process call CefV8Context::GetFrame::ExecuteJavascript.
Maintainer of the CEF Python, PHP Desktop and CEF C API projects. My LinkedIn.
User avatar
Czarek
Virtuoso
 
Posts: 1927
Joined: Sun Nov 06, 2011 2:12 am

Re: Need help with CefV8Context::Eval()

Postby amaitland » Mon Jul 11, 2016 6:48 am

Interesting that CSP only appears to be limiting Eval. I would have ExecuteJavascript to behave the same, and yet it doesn't. Thank Czarek!
Maintainer of the CefSharp project.
amaitland
Virtuoso
 
Posts: 1292
Joined: Wed Jan 14, 2015 2:35 am

Re: Need help with CefV8Context::Eval()

Postby Czarek » Mon Jul 11, 2016 7:29 am

Try also CefV8Context::GetGlobal::GetValue("pageYOffset").GetIntValue - along with HasValue and other checks.
Maintainer of the CEF Python, PHP Desktop and CEF C API projects. My LinkedIn.
User avatar
Czarek
Virtuoso
 
Posts: 1927
Joined: Sun Nov 06, 2011 2:12 am

Re: Need help with CefV8Context::Eval()

Postby fasecero » Mon Jul 11, 2016 5:41 pm

amaitland wrote:Interesting that CSP only appears to be limiting Eval. I would have ExecuteJavascript to behave the same, and yet it doesn't. Thank Czarek!


I thought exactly the same... so I even didn't try. Test below works fine on pages with CSP:

Code: Select all
FocusedFrame->ExecuteJavaScript("alert(window.pageYOffset);", FocusedFrame->GetURL(), 0);


I'll have to find a way to replace each Eval() with ExecuteJavaScript()

Code: Select all
v8Context->Eval("document.body.clientHeight", ret, excep);
v8Context->Eval("window.getSelection().toString()", ret, excep);
v8Context->Eval("document.activeElement.tagName", ret, excep);
....


Czarek wrote:I think you can accomplish the same using ExecuteJavascript to inject custom js on pages in CefLoadHandler and javascript bindings to communicate with C++ (see wiki pages for tutorial).

EDIT: to inject custom js from the Renderer process call CefV8Context::GetFrame::ExecuteJavascript.

Czarek wrote:Try also CefV8Context::GetGlobal::GetValue("pageYOffset").GetIntValue - along with HasValue and other checks.


will study how this works in detail and then try to implement a solution. I just want to thank you very much for all the help you gave me.
fasecero
Mentor
 
Posts: 60
Joined: Mon May 12, 2014 2:53 pm

Re: Need help with CefV8Context::Eval()

Postby fasecero » Tue Jul 12, 2016 3:12 pm

Problem solved, one last question, do I have to somehow delete the attached values to a frame's window object?

Code: Select all
class MyApp : public CefApp,
              public CefBrowserProcessHandler,
              public CefRenderProcessHandler {

  virtual void OnContextCreated(CefRefPtr<CefBrowser> browser,
                                CefRefPtr<CefFrame> frame,
                                CefRefPtr<CefV8Context> context) OVERRIDE {

     if (frame->IsMain()) {
        CefRefPtr<CefV8Value> object = context->GetGlobal();

        CefRefPtr<CefV8Value> V8Value = CefV8Value::CreateDouble(0);
        object->SetValue("fs_pageYOffset", V8Value, V8_PROPERTY_ATTRIBUTE_NONE);

        // do I have to explicitly delete V8Value, inside OnContextReleased maybe?
     }
  }
}
fasecero
Mentor
 
Posts: 60
Joined: Mon May 12, 2014 2:53 pm

Re: Need help with CefV8Context::Eval()

Postby magreenblatt » Tue Jul 12, 2016 3:20 pm

fasecero wrote:do I have to explicitly delete V8Value, inside OnContextReleased maybe?

No. The values are reference counted and will be deleted when no longer referenced.
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: [SOLVED] Need help with CefV8Context::Eval()

Postby fasecero » Tue Jul 12, 2016 3:35 pm

Thank you again and best regards
fasecero
Mentor
 
Posts: 60
Joined: Mon May 12, 2014 2:53 pm

Previous

Return to Support Forum

Who is online

Users browsing this forum: No registered users and 102 guests