Best way to listen to JavaScript function

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

Best way to listen to JavaScript function

Postby michael10 » Fri May 20, 2016 8:35 am

Hi.
We are loading an swt app in cefBrowser. The app will send via JS the status (e.g. initializeCompleted) when all swt stuff is loaded.
So we have to listen to the JS function
Code: Select all
function sendCommand(method, args)
to get this info.
It seems the best way to handle callback could be :
Code: Select all
function sendCommand(method, args) {
  // Results in a call to the OnQuery method in binding_test.cpp
  window.cefQuery({
    request: method,
    onSuccess: function(response) {
      document.getElementById('result').value = 'Response: '+response;
    },
    onFailure: function(error_code, error_message) {}
  });
}


and adding this snipped by manipulation response with
Code: Select all
ResourceHandler.readResponse


Is that right or is there a better way?
michael10
Newbie
 
Posts: 8
Joined: Fri May 13, 2016 2:34 am

Re: Best way to listen to JavaScript function

Postby magreenblatt » Fri May 20, 2016 10:46 am

You posted in the JCEF forum so I'll assume that you're using the JCEF project. In that case you can use the existing CefMessageRouterHandler Java class. See the detailed sample app for example usage.
magreenblatt
Site Admin
 
Posts: 12384
Joined: Fri May 29, 2009 6:57 pm

Re: Best way to listen to JavaScript function

Postby michael10 » Mon May 30, 2016 4:54 am

Hi.
I already use CefMessageRouterHandler....
The problem is, I have to generate a HTML with a JavaScript specific function name "responseCommand" to handle response from embed object.
So I call the generated HTML with
Code: Select all
cefBrowser.loadString(genHTML, dummyUrl)

The genHTML:
Code: Select all
<html><head><title>Test</title>

<script language="JavaScript" type="text/javascript">
function responseCommand(method, args) {
window.cefQuery({
request: method + ';' + args,
onFailure: function(error_code, error_message) {}
});
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body style="background-color: rgb(38,38,38); height: 100%; width: 100%; overflow: hidden; margin: 0">
 <iframe src="http://localhost:8080/integrationLogin.html" width="100%" height="100%" name="plugin" id="plugin"></iframe>

</body></html>


But the "responseCommand" function isn't called.
I am not sure if it's a goot way to handle such problem with "cefBrowser.loadString"? I read that there could be problems with complicated html code
Or is it better to use cefBrowser.loadRequest(CefRequest).
michael10
Newbie
 
Posts: 8
Joined: Fri May 13, 2016 2:34 am

Re: Best way to listen to JavaScript function

Postby michael10 » Tue Aug 09, 2016 7:42 am

The cefBrowser.executeJavaScript() works fine in all may cases .
The problems were:
- JS was executed on wrong URL
- browser not finished with loading
- redirection. I have to listen on CefDisplayHandler.onAddressChange() and execute my JS if necessary
michael10
Newbie
 
Posts: 8
Joined: Fri May 13, 2016 2:34 am


Return to JCEF Forum

Who is online

Users browsing this forum: No registered users and 18 guests