Page 1 of 2

JavaScript messaging with Java

PostPosted: Tue Jan 07, 2020 8:15 pm
by pavone11
Hi at all! I'm struggling with my Java application.
My problem is simple.
I have the "MessageHandler" with the "onQuery" method.
I absolutely need to execute javascript code and get its result. For example, "document.getElementById('mypassword').value" in a SYNCHRONOUSLY way.
The problem is that I cannot manage to synchronize Javascript side and Java side, because:
- "browser.getCefBrowser().executeJavaScript" and "onQuery" happens to be executed all on the same thread!!!
How can I manually synchronize them?
Please, don't suggest off-topic things like "redesign your application". This is absolutely NOT possible!
Thanks in advance!

Re: JavaScript messaging with Java

PostPosted: Wed Jan 08, 2020 5:14 am
by magreenblatt
JCEF does not support synchronous execution of JavaScript code from the Java side.

Re: JavaScript messaging with Java

PostPosted: Wed Jan 08, 2020 5:56 am
by pavone11
Thanks for your answer, but it's not a valid one. JxBrowser does it!
How doesn't JCEF do it?
The problem lies that every command is executed on the same single thread, and synchronization is not possible.
NOT because of a CEF limitation, but it's a JCEF design thing (note the J), where all the commands are serialized to the same single thread.

Re: JavaScript messaging with Java

PostPosted: Wed Jan 08, 2020 6:23 am
by magreenblatt
Yes, it is a JCEF design decision. Java code only runs in the main process. Maybe you can accomplish what you need using synchronous XMLHttpRequest requests?

Re: JavaScript messaging with Java

PostPosted: Wed Jan 08, 2020 6:51 am
by pavone11
I need to comunicate in this way: Java -> JS with return value -> Java get that return value.
Anyway, synced XHR are deprecated as well...

Re: JavaScript messaging with Java

PostPosted: Wed Jan 29, 2020 4:15 am
by Phylanx
We needed something that way too.
We implemented it on our own way by a JavaScript Wrapper calling a callback and the Queue waiting for that callback of the JavaScript execution thread.

Meaning in detail:

Thread1:
browser registers a self built notification object for a callback.
browser.executeJavaScript to get the desired result. Result is published via a cefcallback call
thread1 waits for the notification object to get the notification

Thread2 (JCEFs JavaScript execution thread):
Executes the JavaScript
Executes the Callback
MessageHandler.onQuery gets the callback with the return value and notifies the notification object with it.
Finishes the execution.

Thread1:
Returns from the notification.wait and gets the return value.

And just for clearification: Thread1 should be some kind of extra Thread, e.g. an ExecutorService, definitely not the AWT EventQueue (otherwise the UI would be blocked).

Re: JavaScript messaging with Java

PostPosted: Mon Feb 03, 2020 10:41 am
by JohnnyTheHun
I think this could be something a lot of people - myself included - would like to use.
Can you include some working code in the test java sources? E.g. so that the detailed example program would have this function. E.g. a javascript call returning a getElemendById(...).value
I will also need to look in this later and will create some example.

Re: JavaScript messaging with Java

PostPosted: Tue Feb 04, 2020 7:13 am
by Phylanx
I'm sorry, I currently don't have the time to patch the detailed MainFrame to implement such a behavior.
I just don't have a JCEF environment installed right now because my laptop crashed.

If I have the time and think of it, I might add a source file in here.

Re: JavaScript messaging with Java

PostPosted: Wed Mar 25, 2020 11:09 am
by Phylanx
I implemented something like that in my own jcef fork, see commit

https://bitbucket.org/Phylanx/java-cef- ... ae1949cce5

Re: JavaScript messaging with Java

PostPosted: Tue Apr 28, 2020 8:47 am
by JohnnyTheHun
Thank you for getting back to this, I'll take a look