Send payload to browser via JCEF

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

Send payload to browser via JCEF

Postby JoaoSamoes » Thu Jul 07, 2022 10:19 am

Hello

I am new to your framework and I am creating a IntelliJ plugin which opens a Webview of a website that I created.
For that webview I am using your framework to be able to create a browser in a Tool Window with that website URL.
However I need to send a payload to that window so that the frontend shows the payload on the website accordingly.
The way I do it in VSCode plugin is by using the

Code: Select all
webview.postMessage({ command, payload,});


I already saw the MessageRouterHandler and MessageRouterHandlerEx but I think this is not what I want (from what I understood this is to receive an event from the Javascript), I want it the other way around, I want to send the payload to the loaded frontend using an event.
I would like to know if you can help me with this.

Thank you!
JoaoSamoes
Newbie
 
Posts: 7
Joined: Thu Jul 07, 2022 9:37 am

Re: Send payload to browser via JCEF

Postby magreenblatt » Thu Jul 07, 2022 10:28 am

How large is the payload? You could use CefFrame.executeJavaScript.
magreenblatt
Site Admin
 
Posts: 12382
Joined: Fri May 29, 2009 6:57 pm

Re: Send payload to browser via JCEF

Postby JoaoSamoes » Thu Jul 07, 2022 10:35 am

Hello

Thank you for the quick reply
The payload is a JSON, which is still in Java Class (should I pass it to JSON?). It can be big, but the max size would be like 150Kb

I have tried like these
Code: Select all
browser.getCefBrowser().executeJavaScript("this.postCommand('setModuleResults', "+ payload +")", browser.getCefBrowser().getURL(),0);

browser.getCefBrowser().executeJavaScript("var event = new CustomEvent(\"setCertificationResults\", "+payload+");\n" +"document.dispatchEvent(event);", browser.getCefBrowser().getURL(),0);

browser.getCefBrowser().executeJavaScript("window.postMessage({command:'setCertificationResults', payload : "+ payload +"}, \"*\")", browser.getCefBrowser().getURL(),0);

But none of them worked.
How can I use the CefFrame.executeJavaScript ?
JoaoSamoes
Newbie
 
Posts: 7
Joined: Thu Jul 07, 2022 9:37 am

Re: Send payload to browser via JCEF

Postby magreenblatt » Thu Jul 07, 2022 11:31 am

What you pass to executeJavaScript needs to be valid JavaScript, and the page contents need to be loaded first. Test it in DevTools to verify that it works.
magreenblatt
Site Admin
 
Posts: 12382
Joined: Fri May 29, 2009 6:57 pm

Re: Send payload to browser via JCEF

Postby JoaoSamoes » Thu Jul 07, 2022 5:42 pm

After your reply I made sure that it was a valid Javascript. Made some changes and even compared with the postMessage that I have on my VSCode extension and it was exactly the same.
However, I still can't get the MessageEvent triggered by JCEFBrowser.
I don't know if you can help me with this or if you have any example of that kind, I would appreciate it!
Thank you
JoaoSamoes
Newbie
 
Posts: 7
Joined: Thu Jul 07, 2022 9:37 am

Re: Send payload to browser via JCEF

Postby JoaoSamoes » Fri Jul 08, 2022 7:37 am

Basically what I'd like to know is:

Does the framework have any function that could trigger an Frontend event, a similar function like the window.postMessage?
Is it only able to receive the events from the frontend? Can the JCEF framework send these events? Or do I need to find another solution for this?

Thank you in advance.
JoaoSamoes
Newbie
 
Posts: 7
Joined: Thu Jul 07, 2022 9:37 am

Re: Send payload to browser via JCEF

Postby magreenblatt » Fri Jul 08, 2022 7:58 am

executeJavaScript can execute any JavaScript code. Try something simple like "console.log('test')" and view the results in the DevTools console.
magreenblatt
Site Admin
 
Posts: 12382
Joined: Fri May 29, 2009 6:57 pm

Re: Send payload to browser via JCEF

Postby Phylanx » Mon Jul 11, 2022 1:17 am

We tried something similar.
But 150 kb can be too big for one js call.

Might be better you start with smaller events and if they are bigger, try splitting them and sending them in f.e. 10kb packages.

magreenblatt is right: DevTools is your friend with this!
Phylanx
Expert
 
Posts: 201
Joined: Thu Aug 11, 2016 8:17 am

Re: Send payload to browser via JCEF

Postby JoaoSamoes » Tue Jul 12, 2022 6:02 am

Hello!

I followed what you said of trying to start by the simplest function with a console.log and what I could see was that the executeJavaScript was not running because it assumed that the browser was not fully loaded. I changed a bit the flow of the plugin and now I can see the console.log('test') there.
But the thing is that I still am not able to send the payload to the browser (I am now using a payload that has like 5kb and still nothing).
I saw that you said that you "tried something similar". Can I know how?

Thank you for all the help!
JoaoSamoes
Newbie
 
Posts: 7
Joined: Thu Jul 07, 2022 9:37 am

Re: Send payload to browser via JCEF

Postby Phylanx » Thu Jul 14, 2022 1:43 am

@JoaoSamoes:
I rechecked our code and unfortunatly it was the other way around.
We sent a big payload via cefQuery from JavaScript to the Java Code.

The reason was that another department wanted to print huge PDFs (>100MB) directly via Java Code and not via JCEFs browser code.
the PDF is splitted in parts of a customizable size (i remember something agreed between 1k and 10k) and on java side these parts are accepted and again glued together.
After finishing them the java printing algorithm was started for the given PDF.

For details I would have to ask my colleagues, I just remember that JS or cefQuery had size limitations on what could be passed as payload.
Phylanx
Expert
 
Posts: 201
Joined: Thu Aug 11, 2016 8:17 am


Return to JCEF Forum

Who is online

Users browsing this forum: No registered users and 16 guests