How to talk with Javascript function from SWT

There is a javascript xxx_return () function in my HTML file that will return a string value. Is there a way that I can use this value from the Java level?

I use the SWT shell to display this html. Does SWT provide any function to get the return values ​​of a script function?

edit:

My code looks something like this: package test.html.simulation;

import org.eclipse.swt.SWT; import org.eclipse.swt.SWTException; import org.eclipse.swt.browser.Browser; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class BrowserExample{ public static void main(String[] args) { Display display = new Display(); final Shell shell = new Shell(display); String html=""; Object ob=null; shell.setText("Browser Example"); shell.setSize(500, 350); final Browser browser = new Browser(shell, SWT.NONE); browser.setBounds(5, 75, 600, 400); browser.setUrl("http://localhost/test/tryxml.html"); shell.open(); //System.out.println(browser.getUrl()); //try { html=(String)browser.evaluate("returnHTML();"); }/*catch(SWTException e) { System.out.println(e.getMessage()); }*/ System.out.println(html); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } 

This code gives me a SWT exception like Object:

 Exception in thread "main" org.eclipse.swt.SWTException: Object expected at org.eclipse.swt.browser.WebBrowser$EvaluateFunction.function(Unknown Source) at org.eclipse.swt.browser.WebSite.Invoke(Unknown Source) at org.eclipse.swt.browser.WebSite$7.method6(Unknown Source) at org.eclipse.swt.internal.ole.win32.COMObject.callback6(Unknown Source) at org.eclipse.swt.internal.ole.win32.COM.VtblCall(Native Method) at org.eclipse.swt.internal.ole.win32.IDispatch.Invoke(Unknown Source) at org.eclipse.swt.ole.win32.OleAutomation.invoke(Unknown Source) at org.eclipse.swt.ole.win32.OleAutomation.invoke(Unknown Source) at org.eclipse.swt.browser.IE.execute(Unknown Source) at org.eclipse.swt.browser.WebBrowser.evaluate(Unknown Source) at org.eclipse.swt.browser.Browser.evaluate(Unknown Source) at test.html.simulation.BrowserExample.main(BrowserExample.java:29) 

In java script, I wrote a function in a script tag, for example:

 <script> function returnHTML() { var str=document.body.innerHTML; //alert(str); return str; } </script> 

Can anyone find a mistake in this ?. I do not understand where she gets into the error.

Thanks.

+7
source share
3 answers

Use the SWT Browser object. Then you can simply use String result = (String)Browser.evaluate("xxx_return();") .

+4
source

I found it, an exception was thrown since Browser.evaluate () was called before the page was loaded into the shell. I added a ProgressListener to find out about the completion and tried to call him.

 browser.addProgressListener(new ProgressListener() { public void changed(ProgressEvent event) { } public void completed(ProgressEvent event) {String htm; htm=(String)browser.evaluate("return returnHTML()"); System.out.println(htm); } }); 

Thanks everyone

+4
source

In addition to the above solutions, add "return" before the expression. Also, depending on what you are evaluating, the completed event can be ignored. The following expression just works.

 browser.evaluate("return 4 + 5;") 

Of course, if you evaluate javascript from a page loaded into the browser, the rating should be called after the event has ended, otherwise javascript may not have been loaded.

0
source

All Articles