embedded_browser_jcef.md: Clarify handling responses from Java code

This commit is contained in:
Karol Lewandowski 2025-01-15 16:46:34 +01:00
parent 851d8217c2
commit 3ec51aefc6

View File

@ -1,4 +1,4 @@
<!-- Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. --> <!-- Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. -->
# Embedded Browser (JCEF) # Embedded Browser (JCEF)
<primary-label ref="2020.2"/> <primary-label ref="2020.2"/>
@ -219,7 +219,7 @@ browser.getCefBrowser().executeJavaScript( // 6
2. Add a handler implementing a plugin code to be executed. 2. Add a handler implementing a plugin code to be executed.
Example implementation opens a link in the editor or an external browser depending on whether the link is local or external. Example implementation opens a link in the editor or an external browser depending on whether the link is local or external.
3. Handlers can optionally return a `JBCefJSQuery.Response` object, which holds information about success or error occurred on the plugin code side. 3. Handlers can optionally return a `JBCefJSQuery.Response` object, which holds information about success or error occurred on the plugin code side.
It can be handled in the browser if needed. It can be [handled](#handling-query-response) in the browser if needed.
4. Execute JavaScript, which creates a custom `openLink` function. 4. Execute JavaScript, which creates a custom `openLink` function.
5. Inject JavaScript code responsible for invoking plugin code implemented in step 2. 5. Inject JavaScript code responsible for invoking plugin code implemented in step 2.
The handler added to `openLinkQuery` will be invoked on each `openLink` function call. The handler added to `openLinkQuery` will be invoked on each `openLink` function call.
@ -230,6 +230,20 @@ browser.getCefBrowser().executeJavaScript( // 6
6. Execute JavaScript, which registers a click event listener in the browser. 6. Execute JavaScript, which registers a click event listener in the browser.
Whenever an `a` element is clicked in the browser, the listener will invoke the `openLink` function defined in step 4 with the `href` value of the clicked link. Whenever an `a` element is clicked in the browser, the listener will invoke the `openLink` function defined in step 4 with the `href` value of the clicked link.
#### Handling Query Response
In the example above, there is no need to return response value to the browser from a query handler.
If it is required to handle response:
1. Instead of returning `null` as in 3., return a `JBCefJSQuery.Response` instance, for example, `new JBCefJSQuery.Response("OK")`.
2. Instead of injecting code without callbacks with `JBCefJSQuery.inject("link")` as in 5., use `JBCefJSQuery.inject(queryResult, onSuccessCallback, onFailureCallback)`, for example:
```java
openLinkQuery.inject(
"link",
"function(response) { /* success handler code */ }",
"function(error_code, error_message) { /* error handler code */ }"
);
```
### Loading Resources From Plugin Distribution ### Loading Resources From Plugin Distribution
In cases when a plugin feature implements a web-based UI, the plugin may provide HTML, CSS, and JavaScript files in its [distribution](plugin_content.md) or build them on the fly depending on some configuration. In cases when a plugin feature implements a web-based UI, the plugin may provide HTML, CSS, and JavaScript files in its [distribution](plugin_content.md) or build them on the fly depending on some configuration.