JavaScript messaging with Java

Having problems with building or using the JCEF Java binding? Ask your questions here.

JavaScript messaging with Java

Postby pavone11 » Tue Jan 07, 2020 8:15 pm

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!
pavone11
Newbie
 
Posts: 3
Joined: Tue Jan 07, 2020 8:12 pm

Re: JavaScript messaging with Java

Postby magreenblatt » Wed Jan 08, 2020 5:14 am

JCEF does not support synchronous execution of JavaScript code from the Java side.
magreenblatt
Site Admin
 
Posts: 12383
Joined: Fri May 29, 2009 6:57 pm

Re: JavaScript messaging with Java

Postby pavone11 » Wed Jan 08, 2020 5:56 am

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.
pavone11
Newbie
 
Posts: 3
Joined: Tue Jan 07, 2020 8:12 pm

Re: JavaScript messaging with Java

Postby magreenblatt » Wed Jan 08, 2020 6:23 am

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?
magreenblatt
Site Admin
 
Posts: 12383
Joined: Fri May 29, 2009 6:57 pm

Re: JavaScript messaging with Java

Postby pavone11 » Wed Jan 08, 2020 6:51 am

I need to comunicate in this way: Java -> JS with return value -> Java get that return value.
Anyway, synced XHR are deprecated as well...
pavone11
Newbie
 
Posts: 3
Joined: Tue Jan 07, 2020 8:12 pm

Re: JavaScript messaging with Java

Postby Phylanx » Wed Jan 29, 2020 4:15 am

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).
Phylanx
Expert
 
Posts: 201
Joined: Thu Aug 11, 2016 8:17 am

Re: JavaScript messaging with Java

Postby JohnnyTheHun » Mon Feb 03, 2020 10:41 am

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.
JohnnyTheHun
Techie
 
Posts: 42
Joined: Tue Apr 10, 2018 12:24 pm

Re: JavaScript messaging with Java

Postby Phylanx » Tue Feb 04, 2020 7:13 am

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.
Phylanx
Expert
 
Posts: 201
Joined: Thu Aug 11, 2016 8:17 am

Re: JavaScript messaging with Java

Postby Phylanx » Wed Mar 25, 2020 11:09 am

I implemented something like that in my own jcef fork, see commit

https://bitbucket.org/Phylanx/java-cef- ... ae1949cce5
Phylanx
Expert
 
Posts: 201
Joined: Thu Aug 11, 2016 8:17 am

Re: JavaScript messaging with Java

Postby JohnnyTheHun » Tue Apr 28, 2020 8:47 am

Thank you for getting back to this, I'll take a look
JohnnyTheHun
Techie
 
Posts: 42
Joined: Tue Apr 10, 2018 12:24 pm

Next

Return to JCEF Forum

Who is online

Users browsing this forum: No registered users and 16 guests