Skip to content

Commit b4d4fd3

Browse files
committed
Refine JsToJavaBridge
1 parent 2b8b27e commit b4d4fd3

3 files changed

Lines changed: 15 additions & 15 deletions

File tree

src/main/kotlin/ai/devchat/devchat/handler/ListCommandsRequestHandler.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ import com.alibaba.fastjson.JSONObject
88
class ListCommandsRequestHandler(metadata: JSONObject?, payload: JSONObject?) : BaseActionHandler(metadata, payload) {
99
override val actionName: String = DevChatActions.LIST_COMMANDS_RESPONSE
1010
override fun action() {
11-
send(payload= mapOf("commands" to wrapper.commandList))
11+
send(payload = mapOf("commands" to wrapper.commandList))
1212
}
1313
}

src/main/kotlin/ai/devchat/devchat/handler/LoadHistoryMessagesRequestHandler.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ class LoadHistoryMessagesRequestHandler(metadata: JSONObject?, payload: JSONObje
1313
val pageSize = DevChatSettingsState.instance.maxLogCount
1414
val pageIndex = metadata!!.getInteger("pageIndex") ?: 1
1515
val messages = ActiveConversation.getMessages(pageIndex, pageSize)
16-
send(payload= mapOf("messages" to messages))
16+
send(payload = mapOf("messages" to messages))
1717
}
1818
}

src/main/kotlin/ai/devchat/idea/JSJavaBridge.kt

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import org.cef.handler.CefLoadHandlerAdapter
1616
import org.cef.network.CefRequest
1717

1818
class JSJavaBridge(private val jbCefBrowser: JBCefBrowser) {
19-
private val jsQuery: JBCefJSQuery = JBCefJSQuery.create((jbCefBrowser as JBCefBrowserBase))
19+
val jsQuery = JBCefJSQuery.create(jbCefBrowser as JBCefBrowserBase)
2020

2121
init {
2222
jsQuery.addHandler { arg: String -> callJava(arg) }
@@ -43,19 +43,19 @@ class JSJavaBridge(private val jbCefBrowser: JBCefBrowser) {
4343
}
4444

4545
fun registerToBrowser() {
46+
4647
jbCefBrowser.jbCefClient.addLoadHandler(object : CefLoadHandlerAdapter() {
47-
override fun onLoadStart(browser: CefBrowser, frame: CefFrame, transitionType: CefRequest.TransitionType) {
48-
browser.executeJavaScript(
49-
"window.JSJavaBridge = {"
50-
+ "callJava : function(arg) {"
51-
+ jsQuery.inject(
52-
"arg",
53-
"response => console.log(response)",
54-
"(error_code, error_message) => console.log('callJava Failed', error_code, error_message)"
55-
)
56-
+ "}"
57-
+ "};",
58-
"", 0
48+
override fun onLoadStart(browser: CefBrowser?, frame: CefFrame?, transitionType: CefRequest.TransitionType?) {
49+
jbCefBrowser.cefBrowser.executeJavaScript("""
50+
window.JSJavaBridge = {callJava : function(arg) {
51+
${jsQuery.inject(
52+
"arg",
53+
"response => console.log(response)",
54+
"(error_code, error_message) => console.log('callJava Failed', error_code, error_message)"
55+
)}
56+
}};
57+
""".trimIndent(),
58+
jbCefBrowser.cefBrowser.url, 0
5959
)
6060
}
6161

0 commit comments

Comments
 (0)