How to talk to a Javascript function from SWT
Asked Answered
R

3

9

My HTML file has a javascript function xxx_return(), which will return a string value. Is there any way i can take this value from Java layer?.

I am using SWT shell to display this html. Does SWT carry any feature to get the return values of a script function?

edit:

My code is something like below: 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 an SWT Exception like Object expected:

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 the java script i have written a function in the script tag like:

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

Can anyone find the error in this?. I don't understand where it hits the error.

Thanks.

Ranunculaceous answered 17/10, 2011 at 10:23 Comment(0)
O
5

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

Ornament answered 17/10, 2011 at 14:8 Comment(3)
I have edited my question. I did as u said, but it gives me an exception. Can you please find the error?ThanksRanunculaceous
There's a bug regarding Browser#evaluate and null return values with IE9: bugs.eclipse.org/bugs/show_bug.cgi?id=344597. To work around this, you can do Browser.evaluate("foo() || '';") Can you simplify this to a Javascript function that just returns a constant string? Does that succeed?Ornament
I could retrieve the String from javascript, by calling evaluate as Browser.evaluate("return foo()");Ranunculaceous
R
5

I found it, the exception occurred since the Browser.evaluate() was getting called before the page was loaded in the shell. I added a ProgressListener to know the completion, and tried calling it worked.

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 All

Ranunculaceous answered 18/10, 2011 at 5:17 Comment(0)
I
2

In addition the above solutions, add "return" in front of the expression. Also depending on what you are evaluating, completed event can be ignored. Following expression just works.

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

Of course if you're evaluating javascript from the page loaded in the browser, evaluate must be called after completed event, otherwise the javascript may not have been loaded.

Illboding answered 6/8, 2015 at 10:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.