Note: Advice in this article will only work for JxBrowser 6. See the corresponding article for JxBrowser 7 here.(注意:本文中的建议仅适用于JxBrowser6,JxBrowser7相应文章请点击这里。)


JxBrowser provides functionality that allows injecting Java object into JavaScript code. By default, all public fields and methods of the injected Java object is accessible from JavaScript, so JavaScript code can access them. To tell JavaScript code what public fields/methods of the injected Java object can be accessible from JavaScript you can use @JSAccessible annotation. If you need to let JavaScript code to access only specific public fields/methods, then you must mark them with the @JSAccessible annotation. For example:(JxBrowser提供了允许将Java对象注入JavaScript代码的功能。默认情况下,可以从JavaScript访问注入的Java对象的所有公共字段和方法。要告诉JavaScript代码可以访问注入的Java对象的哪些公共字段/方法,可以使用@JSAccessible批注。如果您需要让JavaScript代码仅访问特定的公共字段/方法,则必须使用@JSAccessible批注对其进行标记。例如:)

public class JavaObject {
    @JSAccessible
    public String accessibleField;
    public String nonAccessibleField;
    
    public void doAction() {
    }
    
    @JSAccessible
    public void doAccessibleAction() {
    }
}

If you inject the JavaObject into JavaScript code, then JavaScript will be able to access only the accessibleField public field and the doAccessibleAction() public method. If JavaScript tries to access other public fields or methods, an exception in JavaScript code will be thrown.(如果将JavaObject注入JavaScript代码中,则JavaScript将只能访问accessibilityField公共字段和doAccessibleAction()公共方法。如果JavaScript尝试访问其他公共字段或方法,则将引发JavaScript代码中的异常。)


Complete example:(完整的例子:)

import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.JSAccessible;
import com.teamdev.jxbrowser.chromium.JSValue;
import com.teamdev.jxbrowser.chromium.swing.BrowserView;

import javax.swing.*;
import java.awt.*;

/**
 * The sample demonstrates how to inject Java object into JavaScript code and
 * tell JavaScript what public fields and methods JavaScript can access via the
 * JSAccessible annotation. See comments in the example.
 */
public class JavaScriptJavaJSAccessibleSample {
    public static void main(String[] args) {
        Browser browser = new Browser();
        BrowserView view = new BrowserView(browser);

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(view, BorderLayout.CENTER);
        frame.setSize(700, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        JSValue window = browser.executeJavaScriptAndReturnValue("window");

        // Inject Java object into JavaScript and associate it with
        // the 'window.javaObject' property.
        JavaObject javaObject = new JavaObject();
        window.asObject().setProperty("javaObject", javaObject);

        // You can access public field marked with the JSAccessible annotation
        // of the injected Java object and modify their values.
        browser.executeJavaScriptAndReturnValue(
                "window.javaObject.accessibleField = 'Hello from JavaScript!';");

        // Access to public field that isn't marked with the JSAccessible
        // annotation will be denied and cause an error in JavaScript.
        browser.executeJavaScriptAndReturnValue(
                "window.javaObject.nonAccessibleField = 'Hello from JavaScript again!';");

        // Print a new value of the accessibleField.
        System.out.println("accessibleField = " + javaObject.accessibleField);
        System.out.println("nonAccessibleField = " + javaObject.nonAccessibleField);
    }

    /**
     * The class has two public fields. The 'accessibleField' public field marked
     * with the JSAccessible annotation. The 'nonAccessibleField' public field
     * isn't. If at least one public field of method is marked with the
     * JSAccessible annotation, it means that only public fields and methods with
     * this annotation can be accessed from JavaScript. Access to other public
     * fields or methods will cause an error in JavaScript. If class or its
     * superclass doesn't have public fields or methods marked with the
     * JSAccessible annotation, then it means that all public fields and methods
     * can be accessed from JavaScript.
     */
    public static class JavaObject {
        @JSAccessible
        public String accessibleField;
        public String nonAccessibleField;
    }
}