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.
(在JxBrowser 6.1中,对公共API进行了一些更改。这些更改可能需要更改您的应用程序的源代码。本简短指南介绍了如何将使用JxBrowser 6.0.2 API编写的应用程序源代码更改为JxBrowser 6.1 API。)
Frames IDs(框架编号)
6.0.2(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 allows getting IDs of frames located inside another frame. (在6.0.2中,Browser.getFramesIds()方法返回当前加载的网页上所有框架的ID。它不允许获取位于另一个框架内的框架的ID。)
Set<Long> framesIds = browser.getFramesIds();
6.1(6.1)
Now, the Browser.getFramesIds() method returns IDs of the main frame only. To get IDs of frames located inside another frame the Browser.getFramesIds(long frameId) method has been introduced.(现在,Browser.getFramesIds()方法仅返回主框架的ID。为了获取位于另一个框架内的框架的ID,引入了Browser.getFramesIds(long frameId)方法。)
List<Long> framesIds = browser.getFramesIds(); for (Long framesId : framesIds) { List<Long> subFramesIds = browser.getFramesIds(framesId); }
Working with BrowserFunction
(使用BrowserFunction)
6.0.2(6.0.2)
To register global JavaScript function the Browser.registerFunction(String functionName, BrowserFunction function) method is used. Every time when web page is loaded, JxBrowser creates a function with the functionName and attach it to the window JavaScript global object.(要注册全局JavaScript函数,请使用Browser.registerFunction(String functionName,BrowserFunction function)方法。每次加载网页时,JxBrowser都会使用functionName创建一个函数并将其附加到窗口JavaScript全局对象。)
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.(该功能将被注入每个加载的网页的JavaScript中,直到您通过Browser.unregisterFunction(String functionName)方法取消注册为止。)
6.1(6.1)
In 6.1 the BrowserFunction API has become a part of JSObject class. Now, to register global JavaScript function you must use the following code: (在6.1版中,BrowserFunction API已成为JSObject类的一部分。现在,要注册全局JavaScript函数,您必须使用以下代码:)
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 web page, you must inject the function into JavaScript. The function will be removed automatically when another web page is loaded or existing one reloaded.
(每次加载/重新加载网页时,必须将函数注入JavaScript。当加载另一个网页或重新加载现有网页时,该功能将自动删除。)
Accessing Upload Data(访问上传数据)
6.0.2(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(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(JavaScript Java Bridge API)
6.0.2(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(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.(在6.1版中,JavaScript Java Bridge API已更新。现在,您无需将Java原语类型包装到JSValue中即可将它们传递给JavaScript端。您可以直接使用Java基本类型。它们将由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!"); } }