A blank splash screen appears when JCEF loads the url
As shown below:
this is my code:
class Content(toolWindow: ToolWindow,project:Project) {
private val service = project.service<MyProjectService>()
/**
* 返回创建的JPanel
* @return JPanel
*/
val content: JPanel
/**
* 构造函数
*/
init {
content = JPanel(BorderLayout())
// 判断所处的IDEA环境是否支持JCEF
if (!JBCefApp.isSupported()) {
content.add(JLabel("当前环境不支持JCEF", SwingConstants.CENTER))
}
if(service.jbCefBrowser !=null){
println("================")
}
// 创建 JBCefBrowser
val jbCefBrowser = JBCefBrowser()
service.jbCefBrowser = jbCefBrowser
val query = JBCefJSQuery.create(jbCefBrowser)
query.addHandler { arg: String ->
try {
println("chatui send msg: $arg")
var jsonArg = JSON.parseObject(arg)
val msgJSON = JSONObject()
println(jsonArg)
EventQueue.invokeAndWait {
val type = jsonArg.getString("type")
if(type=="chatbotLogoutOrTimeOut"){
//更新工具窗口的按钮
MyToolWindowUtils.createToolbar(toolWindow,false,false)
//保证工具窗口的处理是线程安全的
SwingUtilities.invokeLater {
toolWindow.getContentManager().removeAllContents(true)
val content = toolWindow.contentManager.factory.createContent(MyToolWindowUtils.createPanelWithLabel("Welcome to login"), "Chat", false)
toolWindow.contentManager.addContent(content)
if(service.jbCefBrowser!=null){
service.jbCefBrowser!!.cefBrowser.close(true)
service.jbCefBrowser!!.dispose()
}
}
val actionValue = jsonArg.getJSONObject("data").getString("action")
if(actionValue=="logout"){
MyNotifier.notifyInformation("Exit successfully")
}else if(actionValue=="tokenOutTime"){
MyNotifier.notifyInformation("token invalid")
}
}
val editor = FileEditorManager.getInstance(project).getSelectedTextEditor();
println(editor)
if(editor!=null) {
val content = editor?.getDocument()?.getText()
val selectedText = (editor.getSelectionModel().getSelectedText()?:"").replaceAll()
val startOffset = editor?.scrollingModel?.verticalScrollOffset
if(content != null && editor?.scrollingModel != null && startOffset != null) {
val endOffset = (startOffset.plus(editor.scrollingModel.visibleArea.getHeight())).toInt()
val visibleContent: String = editor.getDocument().getText(TextRange(startOffset, Math.min(content.length,endOffset)))
msgJSON["visibleText"] = visibleContent.replaceAll()
}
else {
msgJSON["visibleText"] =""
}
}
else {
msgJSON["triggerSource"] = ""//""ext.contextMenu
}
}
if(jsonArg["type"] == "getCodeContextMessage") {
return@addHandler JBCefJSQuery.Response(msgJSON.toJSONString())
}
else {
return@addHandler JBCefJSQuery.Response("unkwon msg,by java.")
}
}
catch (e: Exception) {
var error = "Error: "+e.message+"----"+e.printToString()
println(e.printToString())
return@addHandler JBCefJSQuery.Response("{ error: '${error}'", 0, error)
}
}
jbCefBrowser.jbCefClient.addLoadHandler(object : CefLoadHandler {
override fun onLoadingStateChange(cefBrowser: CefBrowser, isLoading: Boolean, canGoBack: Boolean, canGoForward: Boolean) {
}
override fun onLoadStart(cefBrowser: CefBrowser, frame: CefFrame, transitionType: TransitionType) {}
override fun onLoadEnd(cefBrowser: CefBrowser, frame: CefFrame, httpStatusCode: Int) {
println("onLoadEnd")
val formatter = DateTimeFormatter.ofPattern("yyyy-dd-MM HH:mm:ss")
println(LocalDateTime.now().format(formatter))
cefBrowser.executeJavaScript(
"window.jsJavaInvokeBrige = function(arg) {" +
query.inject(
"arg",
"response => { console.log('jsJavaInvokeBrige 调用成功', response) ;jsJavaInvokeBrigeCallback(response)}",
"(error_code, error_message) => console.log('jsJavaInvokeBrige 调用失败', error_code, error_message)"
) +
"};",
null, 0)
}
override fun onLoadError(cefBrowser: CefBrowser, frame: CefFrame, errorCode: CefLoadHandler.ErrorCode, errorText: String, failedUrl: String) {
println("onLoadError: "+errorCode.toString()+": "+errorText)
}
}, jbCefBrowser.cefBrowser)
// 将 JBCefBrowser 的UI控件设置到Panel中
content.add(jbCefBrowser.component, BorderLayout.CENTER)
//加载URL
val chatUIUrl = AISEConfigurable.fullUrl
if(chatUIUrl.startsWith("#") || chatUIUrl.trim() === "") {
MyNotifier.notifyWarning("Server(ChatUI) Url is Empty,Please Config It.")
println("Chat Url is Empty:"+chatUIUrl)
}
else {
// 获取插件的版本号
val clientVersion = ToolUtils.getPluginVersion()
val link_combination = chatUIUrl
println("Url :"+link_combination)
jbCefBrowser.loadURL(link_combination)
}
}
}
Please sign in to leave a comment.
Hi,
It is hard to analyze the whole code without more information.