Interface InjectJsCallback
- All Superinterfaces:
BrowserCallback
,BrowserSyncCallback<InjectJsCallback.Params,
,InjectJsCallback.Response> Callback
,SyncCallback<InjectJsCallback.Params,
InjectJsCallback.Response>
You can inject a custom JavaScript code using the Frame.executeJavaScript(String)
method. This callback is intended to give you an opportunity to inject a Java object into
JavaScript code or inject a custom JavaScript code for further execution before any scripts are
executed in the frame. For example:
browser.set(InjectJsCallback.class, params -> {
JsObject window = params.frame().executeJavaScript("window");
if (window != null) {
window.putProperty("java", new JavaObject());
}
return InjectJsCallback.Response.proceed();
});
The JavaObject class may look like this:
public class JavaObject {
@JsAccessible
public String sayHelloTo(String firstName) {
return "Hello " + firstName + "!";
}
}
When the property is set, you can call methods of the injected Java object from JavaScript:
window.java.sayHelloTo('John');
Return the InjectJsCallback.Response.proceed()
response from the callback to continue loading the
frame.
If the callback throws an exception, the InjectJsCallback.proceed()
method
will be invoked.
This callback may be invoked several times for the same frame.
Important: you should avoid executing a JavaScript code that modifies the DOM tree of the web page being loaded. You must not use JxBrowser DOM API to remove the frame for which this callback is invoked, otherwise the render process will crash.
Important: the engine will be blocked until you return control from the callback.
-
Nested Class Summary
Modifier and TypeInterfaceDescriptionstatic interface
The parameters of theInjectJsCallback
.static interface
A response of theInjectJsCallback
. -
Method Summary
Methods inherited from interface com.teamdev.jxbrowser.callback.SyncCallback
on