New version is available You are viewing the documentation for JxBrowser 6 which is not supported since December 2019. Go to the current documentation.
List icon 目录

In JxBrowser 6.1 several changes to public API have been introduced. These changes may require changes to your application’s source code. This short guide shows how to change your application’s source code written with JxBrowser 6.0.2 API to JxBrowser 6.1 API.

Frames IDs

6.0.2

In 6.0.2 the Browser.getFramesIds() method returns IDs of all frames on the currently loaded web page. It doesn’t allow getting IDs of the frames located inside another frame.

Set<Long> framesIds = browser.getFramesIds();

6.1

Now, the Browser.getFramesIds() method returns IDs of the main frame only. To get IDs of the frames located inside another frame the Browser.getFramesIds(long frameId) method has been introduced.

List<Long> framesIds = browser.getFramesIds();
for (Long framesId : framesIds) {
    List<Long> subFramesIds = browser.getFramesIds(framesId);
}

Working with BrowserFunction

6.0.2

To register global JavaScript function the Browser.registerFunction(String functionName, BrowserFunction function) method is used. Every time when a web page is loaded, JxBrowser creates a function with the functionName and attach it to the window JavaScript global object.

browser.registerFunction("save", new BrowserFunction() {
    public JSValue invoke(JSValue... args) {
        return JSValue.createNull();
    }
});

The function will be injected into JavaScript of each loaded web page until you un-register it via the Browser.unregisterFunction(String functionName) method.

6.1

In 6.1 the BrowserFunction API has become a part of JSObject class. Now, to register global JavaScript function you should use the following code:

browser.addScriptContextListener(new ScriptContextAdapter() {
    @Override
    public void onScriptContextCreated(ScriptContextEvent event) {
        Browser browser = event.getBrowser();
        JSValue window = browser.executeJavaScriptAndReturnValue("window");
        window.asObject().setProperty("save", new JSFunctionCallback() {
            @Override
            public Object invoke(Object... args) {
                return null;
            }
        });
    }
});

Every time when you load/reload a web page, you should inject the function into JavaScript. The function will be removed automatically when another web page is loaded or the existing one is reloaded.

Accessing Upload Data

6.0.2

public void onBeforeURLRequest(BeforeURLRequestParams params) {
    if ("POST".equals(params.getMethod())) {
        PostData post = params.getPostData();
        PostDataContentType contentType = post.getContentType();
        if (contentType == PostDataContentType.FORM_URL_ENCODED) {
            FormData data = (FormData) post;
            data.setPair("key1", "value1", "value2");
            data.setPair("key2", "value2");
        } else if (contentType == PostDataContentType.MULTIPART_FORM_DATA) {
            MultipartFormData data = (MultipartFormData) post;
            data.setPair("key1", "value1", "value2");
            data.setPair("key2", "value2");
            data.setFilePair("file3", "C:\\Test.zip");
        } else if (contentType == PostDataContentType.PLAIN_TEXT) {
            RawData data = (RawData) post;
            data.setData("raw data");
        }
        params.setPostData(post);
    }
}

6.1

public void onBeforeURLRequest(BeforeURLRequestParams params) {
    if ("POST".equals(params.getMethod())) {
        UploadData uploadData = params.getUploadData();
        UploadDataType dataType = uploadData.getType();
        if (dataType == UploadDataType.FORM_URL_ENCODED) {
            FormData data = (FormData) uploadData;
            data.setPair("key1", "value1", "value2");
            data.setPair("key2", "value2");
        } else if (dataType == UploadDataType.MULTIPART_FORM_DATA) {
            MultipartFormData data = (MultipartFormData) uploadData;
            data.setPair("key1", "value1", "value2");
            data.setPair("key2", "value2");
            data.setFilePair("file3", "C:\\Test.zip");
        } else if (dataType == UploadDataType.PLAIN_TEXT) {
            TextData data = (TextData) uploadData;
            data.setText("My data");
        } else if (dataType == UploadDataType.BYTES) {
            BytesData data = (BytesData) uploadData;
            data.setData("My data".getBytes());
        }
        // Apply modified upload data that will be sent to a web server.
        params.setUploadData(uploadData);
    }
}

JavaScript Java Bridge API

6.0.2

JSValue value = browser.executeJavaScriptAndReturnValue("document");
if (value.isObject() && value instanceof JSObject) {
    JSObject document = (JSObject) value;
    boolean success = document.set("title", JSValue.create("New Document Title"));

    JSValue method = document.get("write");
    if (method.isFunction() && method instanceof JSFunction) {
        JSFunction documentWrite = (JSFunction) method;
        documentWrite.invoke(document, JSValue.create("Hello World!"));
    }
}

6.1

In 6.1 version JavaScript Java Bridge API has been updated. Now, you don’t need to wrap Java primitive types into JSValue to pass them to JavaScript side. You can use Java primitive types directly. They will be wrapped automatically by JxBrowser.

JSValue document = browser.executeJavaScriptAndReturnValue("document");
if (document.isObject()) {
    boolean success = document.asObject().setProperty("title", "New Title");

    JSValue write = document.asObject().getProperty("write");
    if (write.isFunction()) {
        write.asFunction().invoke(document.asObject(), "Hello World!");
    }
}
Go Top