Check if JavaScript function exists?

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.

Check if JavaScript function exists?

Postby me12 » Tue Oct 07, 2014 5:03 am

Is there any way to tell if frame->ExecuteJavascript (or something similar) was successful (i.e. the function is not undefined)?

My usecase is like this:

1. User tries to close the browser/window.
2. SimpleHandler::DoClose is called.
3. I call a onclose() function which triggers a dialog in javascript asking if he actually wants to close. It looks like this: frame->ExecuteJavaScript("onclose();", frame->GetURL(), 0);
4. When the user confirms a XMLHttpRequest is being send from JavaScript to CEF3 which I capture and close the window.

Everything works fine, the only issue occures when the javascript code is not available (i.e. if it is the DevTools window). Then there is no onclose() function to call nor any XMLHttpRequest functionality implemented.

The only two possible solutions I can think of are:

1. Find a way to check if the onclose() function is available (is not undefined) on the browser/JavaScript side. if not, close the window no matter what. (This is my prefered solution, yet ExecuteJavascript does not return anything since it is asynchronous)
2. Find a way to check (in SimpleHandler::DoClose) if the window that is going to be closed is the DevTools window and then just close it and ignore everything else. (This does not sound as such a good solution. Also I have no idea how one would correctly check if the window is the DevTools window.)
me12
Techie
 
Posts: 31
Joined: Thu Jul 10, 2014 12:46 pm

Re: Check if JavaScript function exists?

Postby Czarek » Tue Oct 07, 2014 6:30 am

ExecuteJavascript runs asynchronously. CefV8Context::Eval() runs synchronously and returns result/exception, but it can be executed only in the Renderer process. You can catch exceptions globally in CefRenderProcessHandler::OnUncaughtException, or you can use window.onerror event in javascript.
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: Check if JavaScript function exists?

Postby me12 » Tue Oct 07, 2014 8:04 am

Czarek wrote:CefV8Context::Eval() runs synchronously and returns result/exception, but it can be executed only in the Renderer process.

Sadly I need to do that check in the UI thread. So I guess this means I need to call the function from the UI thread in the render process and then send the result back to the UI thread?

I assume that the UI thread and renderer thread are running in different processes? If so, I guess I cant simply do a CefPostTask(TID_RENDERER, base::Bind(&SimpleHandler::AskAllowShutdown, this, browser)); from the UI thread?

Czarek wrote:You can catch exceptions globally in CefRenderProcessHandler::OnUncaughtException, or you can use window.onerror event in javascript.


That would theoretically solve my problem if I would check for my exact error "..blabla... onclose() undefined..." and then force the window to close if that is the case. But that solution is really ugly. I'd rather not make the whole system depend on some exact error string I recive in OnUnchaughtException.
me12
Techie
 
Posts: 31
Joined: Thu Jul 10, 2014 12:46 pm

Re: Check if JavaScript function exists?

Postby Czarek » Tue Oct 07, 2014 9:56 am

To answer the main question in title on how to check if js function exists:
Code: Select all
if (typeof onclose == "function") onclose();
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: Check if JavaScript function exists?

Postby magreenblatt » Tue Oct 07, 2014 10:16 am

me12 wrote:I assume that the UI thread and renderer thread are running in different processes? If so, I guess I cant simply do a CefPostTask(TID_RENDERER, base::Bind(&SimpleHandler::AskAllowShutdown, this, browser)); from the UI thread?

Correct. See https://code.google.com/p/chromiumembed ... ation_(IPC).
magreenblatt
Site Admin
 
Posts: 12402
Joined: Fri May 29, 2009 6:57 pm

Re: Check if JavaScript function exists?

Postby me12 » Wed Oct 08, 2014 3:46 am

Thank you both! I guess I have to bite the bullet and go with that rather inconvenient/complicated way.
me12
Techie
 
Posts: 31
Joined: Thu Jul 10, 2014 12:46 pm


Return to Support Forum

Who is online

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

cron