Adding functions to the DOM

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.

Adding functions to the DOM

Postby sirheroics » Thu Oct 26, 2023 8:45 pm

Hello!

My application that uses CEF needs to inject different javascript functions into the DOM depending on the page that the user is navigating to (functions that are only applicable to that page). In addition, there are some functions that need to always exist regardless of the page (like low-level logging utilities).

For the latter, we're registering some functions in `OnWebKitInitialized` using CefRegisterExtension but of course this is only done once per browser regardless of the page that's loaded.

For the former, what's the optimal way to do this? I tried to use `OnBeforeBrowse` to call ExecuteJavaScript which seems to work but only the first time for some reason. Any redirect or post and the injection doesn't seem to work.
sirheroics
Techie
 
Posts: 33
Joined: Mon Mar 22, 2021 6:32 am

Re: Adding functions to the DOM

Postby magreenblatt » Thu Oct 26, 2023 9:57 pm

magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: Adding functions to the DOM

Postby sirheroics » Fri Oct 27, 2023 6:12 am



Hi Matt, yes I'm sorry I did a poor job of explaining myself.

OnContextCreated runs in the Render process right? I was trying to figure out a way to make these DOM changes in my application's process if possible. It's going to be code outside of our CEF implementation that needs to be involved in the decisions.

I'm using code similar to the message_router to handle cefQuery calls and this part works fine. What I'm trying to do is inject a javascript wrapper into the page rather than have static HTML like the example.

Code: Select all
function sendMessage() {
  // Results in a call to the OnQuery method in client_impl.cc.
  window.cefQuery({
    request: 'MessageRouterTest:' + document.getElementById("message").value,
    onSuccess: function(response) {
      document.getElementById('result').value = 'Response: '+response;
    },
    onFailure: function(error_code, error_message) {}
  });
}


So I'm trying to have my application's C++ generate the code above and stick it into the page's that need it. I'm currently using ExecuteJavascript for this and it's not working properly after redirects etc.
sirheroics
Techie
 
Posts: 33
Joined: Mon Mar 22, 2021 6:32 am

Re: Adding functions to the DOM

Postby sirheroics » Mon Oct 30, 2023 7:40 am

I think I've got it sorted out. OnBeforeBrowse is apparently too early to call this from. I moved it to OnLoadEnd and it seems to work consistently now.
sirheroics
Techie
 
Posts: 33
Joined: Mon Mar 22, 2021 6:32 am


Return to Support Forum

Who is online

Users browsing this forum: Google [Bot] and 196 guests