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


JxBrowser API provides functionality that allows accessing and executing JavaScript code on the loaded web page.(JxBrowser API提供的功能允许访问和执行已加载网页上的JavaScript代码。)


Note: To access JavaScript make sure that web page is loaded completely and JavaScript support is enabled.(注意:要访问JavaScript,请确保已完全加载网页并启用了JavaScript支持。)


JxBrowser provides two ways for JavaScript code execution:(JxBrowser提供了两种执行JavaScript代码的方式:)


Executing JavaScript(执行JavaScript)

To execute JavaScript code asynchronously, without blocking current thread execution until the code is executed and ignore return value, use the Browser.executeJavaScript(String javaScript) method. This method tells Chromium engine to execute the given JavaScript code asynchronously. The return value of JavaScript code execution is ignored.(若要异步执行JavaScript代码并忽略返回值,而不阻塞当前线程的执行,请使用Browser.executeJavaScript(String javaScript)方法。此方法告诉Chromium引擎异步执行给定的JavaScript代码。 JavaScript代码执行的返回值将被忽略。)


The following code updates document.title property with "My title" value: (以下代码使用“My title”值更新document.title属性:)

browser.executeJavaScript("document.title = 'My title';");

 

Executing JavaScript and return value(执行JavaScript并返回值)

To execute JavaScript code and return value use the Browser.executeJavaScriptAndReturnValue(String javaScript) method. This method blocks current thread execution and waits until code is executed. The result of execution is stored in JSValue object.(要得到JavaScript代码运行后的返回值,请使用Browser.executeJavaScriptAndReturnValue(String javaScript)方法。此方法会阻止当前线程执行,直到JavaScript代码执行完毕。执行结果存储在JSValue对象中。)


The following code updates document.title property with "My title" value and returns JSValue object with string value that represents document.title value:(以下代码使用“My title”值更新document.title属性,并返回具有表示document.title值的字符串值的JSValue对象:)

JSValue title = browser.executeJavaScriptAndReturnValue(
        "document.title = 'My title'; document.title");
System.out.println("title = " + title.getStringValue());


Accessing JavaScript objects(访问JavaScript对象)

You can access JavaScript objects on the loaded web page using the Browser.executeJavaScriptAndReturnValue(String javaScript) method. If return value represents JavaScript object, then JSValue will contain JSObject instance that represents Java wrapper for JavaScript object. JSObject class provides functionality that allows working with JavaScript object properties and calling its functions.(您可以使用Browser.executeJavaScriptAndReturnValue(String javaScript)方法访问已加载网页上的JavaScript对象。如果返回值表示JavaScript对象,则JSValue将包含JSObject实例,该实例表示JavaScript对象的Java包装器。 JSObject类提供了允许使用JavaScript对象属性并调用其函数的功能。)


Getting properties(获取属性)

To access JavaScript object property use the JSObject.getProperty(String name) method. The following code demonstrates how to get value of the document.title property:(要访问JavaScript对象属性,请使用JSObject.getProperty(String name)方法。以下代码演示了如何获取document.title属性的值:)

JSValue document = browser.executeJavaScriptAndReturnValue("document");
JSValue titleValue = document.asObject().getProperty("title");
String title = titleValue.getStringValue();


Setting properties(设定属性)

To modify JavaScript object property with specified value use the JSObject.setProperty(String name, Object value) method. The following code demonstrates how to modify document.title property with "My title" value:(要使用指定值修改JavaScript对象属性,请使用JSObject.setProperty(String name,Object value)方法。以下代码演示了如何使用“My title”值修改document.title属性:)

JSValue document = browser.executeJavaScriptAndReturnValue("document");
document.asObject().setProperty("title", "My title");


Working with functions(调用函数)

JavaScript object property can represent a function (JSFunction). You can invoke JavaScript object function using the following approach:(JavaScript对象的属性可以表示一个函数(JSFunction)。您可以使用以下方法调用JavaScript对象函数:)

JSValue document = browser.executeJavaScriptAndReturnValue("document");
JSValue write = document.asObject().getProperty("write");
write.asFunction().invoke(document.asObject(), "<html><body>Hello</body></html>");

Since JxBrowser 6.9, you can also invoke JavaScript function asynchronously and access the result of the invocation through the Future<JSValue> object:(从JxBrowser 6.9开始,您还可以异步调用JavaScript函数并通过Future 对象访问调用结果:)

JSValue document = browser.executeJavaScriptAndReturnValue("document");
JSValue async = document.asObject().getProperty("asyncFunc");
Future<JSValue> asyncResult = 
        async.asFunction().invokeAsync(document.asObject(), "Hello World Async!");
String result = asyncResult.get().asString().getStringValue();


Working with arrays(处理数组)

JSValue can represent an Array (JSArray). You can access elements of the array using the following approach:(JSValue可以表示一个数组(JSArray)。您可以使用以下方法访问数组的元素:)

JSValue array = browser.executeJavaScriptAndReturnValue("['John', 'Doe', 46];");
JSValue john = array.asArray().get(0);
JSValue doe = array.asArray().get(1);

 

Java to JavaScript types conversion(Java到JavaScript类型转换)

JavaScript and Java work with different primitive types. JxBrowser implements automatic types conversion from Java to JavaScript types. Here's how Java objects will be converted to their JavaScript equivalents by JxBrowser:(JavaScript和Java使用不同的原始类型。 JxBrowser实现了从Java到JavaScript类型的自动类型转换。 JxBrowser将Java对象转换为JavaScript等效物的方式如下:)

 

Java JavaScript
double
float
long
int
short
byte
java.lang.Double
java.lang.Float
java.lang.Long
java.lang.Integer
java.lang.Short
java.lang.Byte
Number
boolean
java.lang.Boolean
Boolean
java.lang.String String
null null
com.teamdev.jxbrowser.chromium.JSObject Object
com.teamdev.jxbrowser.chromium.JSArray Array
com.teamdev.jxbrowser.chromium.JSFunction Function