I'm trying to get the return value of a JavaScript function I've written. I can do it using a very complicated method involving listeners, but I'm wanting to use Eclipse's Browser.evaluate method.
As a toy problem, I have the following code in a Java file:
Object result = this.browser.evaluate("new String(\"Hello World\")");
where this.browser is created in a class extending org.eclipse.ui.part.EditorPart
in the overridden function createPartControl
:
public void createPartControl(Composite parent) {
// Create the browser instance and have it
this.browser = new Browser(parent, SWT.NONE);
this.browser.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
SomeEditor.this.getSite().getPage().activate(SomeEditor.this);
}
});
new ErrorReporter(this.browser);
this.browser.setUrl(Activator.getResourceURL("html/java-shim.html").toString());
...more stuff snipped...
}
However, instead of result
being a Java String with the contents "Hello World", it's null
. Am I using this method incorrectly, or is it broken?
You have to return a value in JavaScript:
Object result = browser.evaluate("return 'Hello World'");
works for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With