This guide describes how to access JavaScript on a loaded web page, execute JavaScript code, inject Java objects to call Java from JavaScript, etc.

Executing JavaScript

JxBrowser allows accessing and executing JavaScript code on a loaded web page.

To access JavaScript make sure that web page is loaded completely and JavaScript is enabled.

To execute JavaScript code please use the Frame.executeJavaScript(String javaScript) method. This method blocks the current thread execution and waits until the given code is executed. The method returns a java.lang.Object that represents the result of the execution. The method returns null if the result of the execution is null or undefined.

The following example executes the JavaScript code that returns a title of the document:

String title = frame.executeJavaScript("document.title");

You can execute any JavaScript code:

double number = frame.executeJavaScript("123");
boolean bool = frame.executeJavaScript("true");
String string = frame.executeJavaScript("'Hello'");
JsObject window = frame.executeJavaScript("window");
Element body = frame.executeJavaScript("document.body");

Type Conversion

JavaScript and Java work with different primitive types. JxBrowser implements an automatic type conversion from JavaScript to Java types and vice versa.

JavaScript to Java

The following rules are used to convert JavaScript into Java types:

  • JavaScript numbers are converted to java.lang.Double
  • JavaScript string to java.lang.String
  • JavaScript boolean to java.lang.Boolean
  • JavaScript null and undefined to null
  • JavaScript objects get wrapped as JsObject
  • JavaScript DOM Node objects get wrapped as both JsObject and EventTarget.

In the example above we know that the document.title is a string, so we set the return value to java.lang.String.

Java to JavaScript

The following rules are used to convert Java into JavaScript types:

  • java.lang.Double is converted to JavaScript Number
  • java.lang.String to JavaScript string
  • java.lang.Boolean to JavaScript boolean
  • Java null to JavaScript null
  • JsObject to an appropriate JavaScript object
  • EventTarget to an appropriate JavaScript DOM Node object
  • java.lang.Object will be wrapped into a JavaScript object.

DOM Wrappers

By the rules of the automatic type conversion JavaScript DOM objects get wrapped as both JsObject and EventTarget. It allows you to work with the JavaScript DOM objects through JxBrowser DOM API.

In the following example we return the document that represents the JavaScript DOM object. In this case the return value can be set to JsObject or Document:

Document document = frame.executeJavaScript("document");
JsObject document = frame.executeJavaScript("document");

Working with JsObject

To work with JavaScript objects from Java code please use the JsObject class. It allows working with the object properties and calling its functions.

Properties

To get property names of a JavaScript object, including properties from the prototype objects, please use the getPropertyNames() method:

Collection<String> propertyNames = jsObject.getPropertyNames();

To check whether JavaScript object has a specified property, please use the hasProperty(String) method:

boolean has = jsObject.hasProperty("<property-name>");

To get value of the JavaScript object property by its name please use getProperty(String). For example:

JsObject document = frame.executeJavaScript("document");
document.getProperty("title").ifPresent(title -> {});

The return value represents java.lang.Object that can be set to the required type. See the Type Conversion.

You can remove a property using the following approach:

boolean success = jsObject.removeProperty("<property-name>");

Functions

To call a function with the required name and arguments please use the call(String methodName, Object... args) method. The following example demonstrates how to call the document.getElementById() JavaScript function:

JsObject element = document.call("getElementById", "elementId");

which is equivalent to the following code in JavaScript:

var element = document.getElementById("demo");

The method throws JsException if an error occurs during the function execution.

Calling Java from JavaScript

When you pass a java.lang.Object as a property value or an argument when calling JavaScript function, the Java object will be automatically converted/wrapped into a JavaScript object.

It allows injecting Java objects into JavaScript and invoking its public methods from the JavaScript code.

For security reasons only the public methods annotated with @JsAccessible can be accessed from JavaScript. Java object’s fields are not accessible. If JavaScript calls the method that does not exist in the injected Java object or the method is not public and has not been annotated, a JavaScript exception with an appropriate error message will be thrown. For example, if the method does not exist, the error message will look like “Uncaught No such method”.

The following example demonstrates how to pass a Java object as a property value and call its public method from JavaScript:

public class JavaObject {
    @JsAccessible
    public String sayHelloTo(String firstName) {
        return "Hello " + firstName + "!";
    }
}
...
JsObject window = frame.executeJavaScript("window");
window.setProperty("java", new JavaObject());

Then you can refer to the object and call its method from the JavaScript code:

console.log(window.java.sayHelloTo("John"));

The Console output should look like:

Hello John!

How it Works

When JavaScript calls a public method of the registered Java object, JavaScript parameters are automatically converted to the corresponding Java objects/primitive values by JxBrowser.

If JxBrowser cannot convert the passed JavaScript arguments or find a method with an appropriate signature, a JavaScript error will be thrown.

If the registered Java object has several methods with the same name and number of parameters, but different parameter types, JavaScript will invoke the first method declared in the Java class.

Console Messages

JxBrowser allows receiving all output messages sent to the Console via the console.log() JavaScript function. You can listen to the messages with the following levels:

  • DEBUG
  • LOG
  • WARNING
  • ERROR

To get a notification when the Console gets a message please use the ConsoleMessageReceived event. For example:

browser.on(ConsoleMessageReceived.class, event -> {
    Level level = event.getLevel();
    String message = event.getMessage();
});
Go Top