Call existing JS function from RenderProcess

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.

Call existing JS function from RenderProcess

Postby SteveMcFarlin » Sun Jan 14, 2024 11:00 am

Hello All,

I have a project that is mimicking a small subset of the Puppeteer API. I am looking for a way to call an existing JS function within a window/frame. Essentialy I am trying to duplicate this https://pptr.dev/api/puppeteer.page.evaluate. I see there is the Eval function on the CefV8Context, but that appears to simply evaluate JS in the string parameter. Please correct me if I am wrong here. Essentially I would like to do the following.

Given this JS function in a frame/page

Code: Select all
function someFunc({
   ...
}) {...}


Call the function from the render process

Code: Select all
context->Eval("someFunc", args, ...);


As an alternative I do see I could modify the page to register the function with the render process per the docs: https://bitbucket.org/chromiumembedded/cef/wiki/JavaScriptIntegration.md#markdown-header-executing-functions.

I am wondering if what I am trying to do is possible with CEF. Thanks for any Help.
SteveMcFarlin
Newbie
 
Posts: 2
Joined: Sun Jan 14, 2024 2:38 am

Re: Call existing JS function from RenderProcess

Postby magreenblatt » Sun Jan 14, 2024 12:35 pm

If you don’t need a return value you can just call ExecuteJavaScript. Otherwise, you’ll need to use something like MessageRouter to send the result back to the main/browser process. CefSharp has a EvaluateScriptAsync method that you can use as a model (implemented using the technologies mentioned above).
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: Call existing JS function from RenderProcess

Postby magreenblatt » Sun Jan 14, 2024 1:01 pm

I've also filed https://github.com/chromiumembedded/cef/issues/3631 (and result callback to ExecuteJavaScript)
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: Call existing JS function from RenderProcess

Postby SteveMcFarlin » Sun Jan 14, 2024 2:38 pm

Thanks. That works out good enough for my purposes. Luckily I do not need any return values currently ;) I'll keep an eye on your issue.
SteveMcFarlin
Newbie
 
Posts: 2
Joined: Sun Jan 14, 2024 2:38 am

Re: Call existing JS function from RenderProcess

Postby KatrinaS » Sun Jan 14, 2024 4:01 pm

magreenblatt wrote:I've also filed https://github.com/chromiumembedded/cef/issues/3631 (and result callback to ExecuteJavaScript)


As you note it's unnecessary given the mechanisms already in place but if a user is only interested in a quick std::string / String etc value then it's still a handy addition to the toolbox :)

Oh,, it would be handy if it could return an Array too :D
KatrinaS
Mentor
 
Posts: 83
Joined: Tue Jul 04, 2023 6:30 pm


Return to Support Forum

Who is online

Users browsing this forum: No registered users and 156 guests