对话框
本指南展示了如何处理 JavaScript、打开/保存文件、颜色、客户端证书和其他对话框。
您可以使用本指南中描述的 JxBrowser API 以编程方式处理对话框。
有不同类型的对话框。 当应显示特定类型的对话框时,将调用相应的回调。 如果未注册回调,对话框将自动关闭,就像用户单击对话框中的 Cancel 按钮一样。
在 BrowserView
实例化期间,给定的 Browser
实例将配置为每个对话框的默认回调实现。 默认实现使用相应的 UI 工具包功能显示对话框。
JavaScript 对话框
Alert
当应显示 JavaScript 警报对话框时,将调用 AlertCallback
回调。 它发生在调用 window.alert()
JavaScript 函数时。
在此回调中,您可以获得对话框参数,例如, 标题、消息和“OK”操作的本地化文本。 当对话框关闭时,必须调用 tell.ok()
。 例如:
browser.set(AlertCallback.class, (params, tell) -> {
// Dialog title.
String title = params.title();
// Dialog message.
String message = params.message();
// The localized text of the "OK" callback action.
String okActionText = params.okActionText();
// Create and display the dialog if necessary.
...
// The dialog has been closed.
tell.ok();
});
browser.set(AlertCallback::class.java, AlertCallback { params, tell ->
// Dialog title.
val title = params.title()
// Dialog message.
val message = params.message()
// The localized text of the "OK" callback action.
val okActionText = params.okActionText()
// Create and display the dialog if necessary.
...
// The dialog has been closed.
tell.ok()
})
JavaScript 的执行将被阻止,直到警报对话框关闭。
确认
当应显示 JavaScript 确认对话框时,将调用 ConfirmCallback
回调。 它发生在调用 window.confirm()
JavaScript 函数时。
在此回调中,您可以获取对话框参数,例如, 标题、消息、 “Yes”和“No”操作的本地化文本。 当对话框关闭时,必须调用 tell.ok()
或 tell.cancel()
。 例如:
browser.set(ConfirmCallback.class, (params, tell) -> {
// Dialog title.
String title = params.title();
// Dialog message.
String message = params.message();
// The localized text of the "OK" action.
String okActionText = params.okActionText();
// The localized text of the "Cancel" action.
String cancelActionText = params.cancelActionText();
...
// The "OK" action has been selected.
tell.ok();
});
browser.set(ConfirmCallback::class.java, ConfirmCallback { params, tell ->
// Dialog title.
val title = params.title()
// Dialog message.
val message = params.message()
// The localized text of the "OK" action.
val okActionText = params.okActionText()
// The localized text of the "Cancel" action.
val cancelActionText = params.cancelActionText()
...
// The "OK" action has been selected.
tell.ok()
})
JavaScript 执行将被阻止,直到确认对话框关闭。
提示
当应显示 JavaScript 提示对话框时,将调用 PromptCallback
回调。 它发生在调用 window.prompt()
JavaScript 函数时。
在此回调中,您可以获得对话框参数,例如,标题、消息、文本、“OK”和“Cancel”操作的本地化文本。 当对话框关闭时,必须调用 tell.ok(String)
或 tell.cancel()
。 例如:
browser.set(PromptCallback.class, (params, tell) -> {
// Dialog title.
String title = params.title();
// Dialog message.
String message = params.message();
// The localized text of the "OK" action.
String okActionText = params.okActionText();
// The localized text of the "Cancel" action.
String cancelActionText = params.cancelActionText();
...
// A text has been entered and the "OK" action has been selected.
tell.ok("Entered text");
});
browser.set(PromptCallback::class.java, PromptCallback { params, tell ->
// Dialog title.
val title = params.title()
// Dialog message.
val message = params.message()
// The localized text of the "OK" action.
val okActionText = params.okActionText()
// The localized text of the "Cancel" action.
val cancelActionText = params.cancelActionText()
...
// A text has been entered and the "OK" action has been selected.
tell.ok("Entered text")
})
JavaScript 执行将被阻止,直到提示对话框关闭。
卸载前
onbeforeunload
事件在网页即将被卸载时触发。 此事件允许您在确认对话框中显示一条消息,以通知用户他们是想停留还是离开当前页面。
当应显示确认对话框时,将调用 BeforeUnloadCallback
回调。 在此回调中,您可以获得对话框参数,例如,标题、消息、“Stay”和“Leave”操作的本地化文本。当对话框关闭时,必须调用 tell.stay()
或tell.leave()
。 例如:
browser.set(BeforeUnloadCallback.class, (params, tell) -> {
// Dialog title.
String title = params.title();
// Dialog message.
String message = params.message();
// The localized text of the "Stay" action.
String stayActionText = params.stayActionText();
// The localized text of the "Leave" action.
String leaveActionText = params.leaveActionText();
...
// The "Stay" action has been selected.
tell.stay();
});
browser.set(BeforeUnloadCallback::class.java, BeforeUnloadCallback { params, tell ->
// Dialog title.
val title = params.title()
// Dialog message.
val message = params.message()
// The localized text of the "Stay" action.
val stayActionText = params.stayActionText()
// The localized text of the "Leave" action.
val leaveActionText = params.leaveActionText()
...
// The "Stay" action has been selected.
tell.stay()
})
通过 Browser.close()
关闭 Browser
实例时不会调用此回调。
如果您想在关闭 Browser
时显示对话框,请使用以下方法关闭 Browser
:
browser.close(CloseOptions.newBuilder().fireBeforeUnload().build());
browser.close(CloseOptions.newBuilder().fireBeforeUnload().build())
选择颜色
当应选择一种颜色时,将调用 SelectColorCallback
回调。 当用户单击具有以下 color 类型的 input 元素时,就会发生这种情况:
<input type="color" value="#ff0000">
在此回调中,您可以获得默认颜色等对话框参数。 当对话框关闭时,必须调用 tell.select(Color)
或 tell.cancel()
。 例如:
browser.set(SelectColorCallback.class, (params, tell) -> {
// The default color.
Color defaultColor = params.defaultColor();
// The selected color.
Color selectedColor = Color.newBuilder()
.red(1.0f)
.green(1.0f)
.blue(1.0f)
.build();
...
// This color has been selected.
tell.select(selectedColor);
});
browser.set(SelectColorCallback::class.java, SelectColorCallback { params, tell ->
// The default color.
val defaultColor = params.defaultColor()
// The selected color.
val selectedColor = Color.newBuilder()
.red(1.0f)
.green(1.0f)
.blue(1.0f)
.build()
...
// This color has been selected.
tell.select(selectedColor)
})
打开文件
当网页希望用户从其设备存储中选择文件时,将调用OpenFileCallback
回调。 当用户单击具有 file
类型的 input
元素时,就会发生这种情况:
<input type="file" accept="image/png, image/jpeg">
在此回调中,您可以获得对话框参数,例如默认文件名、可接受的文件扩展名以及可接受的文件扩展名的说明。 当对话框关闭时,必须调用 tell.open(Path)
或 tell.cancel()
。 例如:
browser.set(OpenFileCallback.class, (params, tell) -> {
// The default file name.
String defaultFileName = params.defaultFileName();
// Acceptable extensions.
List<String> acceptableExtensions = params.acceptableExtensions();
...
// This file should be opened.
tell.open(Paths.get("<path-to-selected-file>"));
});
browser.set(OpenFileCallback::class.java, OpenFileCallback { params, tell ->
// The default file name.
val defaultFileName = params.defaultFileName()
// Acceptable extensions.
val acceptableExtensions: List<String> = params.acceptableExtensions()
...
// This file should be opened.
tell.open(Path("<path-to-selected-file>"))
})
打开文件
当网页希望用户从其设备存储中选择多个文件时,将调用OpenFilesCallback
回调。 当用户单击具有 file
类型和 multiple
属性的输入元素时,就会发生这种情况:
<input type="file" accept="image/png, image/jpeg" multiple>
在此回调中,您可以获得对话框参数,例如默认文件名、可接受的文件扩展名以及可接受的文件扩展名的说明。 当对话框关闭时,必须调用tell.open(Path...)
或 tell.cancel()
。 例如:
browser.set(OpenFilesCallback.class, (params, tell) -> {
// Acceptable extensions.
List<String> acceptableExtensions = params.acceptableExtensions();
...
Path file1 = Paths.get("<path-to-selected-file1>");
Path file2 = Paths.get("<path-to-selected-file1>");
// These files should be opened.
tell.open(file1, file2);
});
browser.set(OpenFilesCallback::class.java, OpenFilesCallback { params, tell ->
// Acceptable extensions.
val acceptableExtensions: List<String> = params.acceptableExtensions()
...
val file1 = Path("<path-to-selected-file1>")
val file2 = Path("<path-to-selected-file1>")
// These files should be opened.
tell.open(file1, file2)
})
打开文件夹
当 Chromium 希望用户从他们的设备存储中选择一个文件夹时,将调用 OpenFolderCallback
回调。
如有必要,您可以创建并显示一个对话框,用户可以在其中选择文件夹。 当对话框关闭时,必须调用 tell.open(Path)
或 tell.cancel()
。 例如:
browser.set(OpenFolderCallback.class, (params, tell) -> {
...
// This folder should be opened.
tell.open(Paths.get("<path-to-folder>"));
});
browser.set(OpenFolderCallback::class.java, OpenFolderCallback { params, tell ->
...
// This folder should be opened.
tell.open(Path("<path-to-folder>"))
})
另存为 PDF
通过“打印预览”对话框将网页另存为 PDF 文档时,将调用SaveAsPdfCallback
回调。
在此回调中,您可以获得默认文件名等对话框参数。 当对话框关闭时,必须调用 tell.save(Path)
或 tell.cancel()
。 例如:
browser.set(SaveAsPdfCallback.class, (params, tell) -> {
// The suggested file name.
String suggestedFileName = params.suggestedFileName();
...
// This file should be saved.
tell.save(Paths.get("<path-to-file>"));
});
browser.set(SaveAsPdfCallback::class.java, SaveAsPdfCallback { params, tell ->
// The suggested file name.
val suggestedFileName = params.suggestedFileName()
...
// This file should be saved.
tell.save(Path("<path-to-file>"))
})
选择客户端证书
当 Chromium 希望用户从可用证书列表中选择客户端 SSL 证书时,将调用 SelectClientCertificateCallback
回调。
在此回调中,您可以获得对话框参数,例如对话框标题、消息、“Select”和“Cancel”操作的本地化文本、可用证书列表。
当对话框关闭时,必须调用 tell.select(int)
或 tell.cancel()
。 例如:
browser.set(SelectClientCertificateCallback.class, (params, tell) -> {
// The dialog title.
String title = params.getTitle();
// The dialog message.
String message = params.getMessage();
// The localized text of the "Select" action.
String selectActionText = params.getSelectActionText();
// The localized text of the "Cancel" action.
String cancelActionText = params.getCancelActionText();
// Available SSL certificates.
List<Certificate> certificates = params.getCertificatesList();
// The index of the last certificate in the list.
int certificateIndex = certificates.size() - 1;
...
// The last certificate in the list has been selected.
tell.select(certificateIndex);
});
browser.set(SelectClientCertificateCallback::class.java,
SelectClientCertificateCallback { params, tell ->
// The dialog title.
val title = params.getTitle()
// The dialog message.
val message = params.getMessage()
// The localized text of the "Select" action.
val selectActionText = params.getSelectActionText()
// The localized text of the "Cancel" action.
val cancelActionText = params.getCancelActionText()
// Available SSL certificates.
val certificates = params.getCertificatesList()
// The index of the last certificate in the list.
val certificateIndex = certificates.size - 1
...
// The last certificate in the list has been selected.
tell.select(certificateIndex)
}
)
打开外部应用程序
当网页想要在关联的外部应用程序中打开链接时,将调用OpenExternalAppCallback
回调。
在此回调中,您可以获得本地化对话框标题、消息、“Open”和“Cancel”操作的文本。 您可以向最终用户显示一个对话框,以决定是否可以打开链接或以编程方式打开外部应用程序,如下例所示:
browser.set(OpenExternalAppCallback.class, (params, tell) -> tell.open());
browser.set(OpenExternalAppCallback::class.java,
OpenExternalAppCallback { params, tell -> tell.open() }
)
默认情况下,打开外部应用程序的请求将被取消。
如果您创建一个 BrowserView 控件并将其嵌入到您的 JavaFX、Swing、SWT 应用程序中,将注册回调的默认 UI 工具包特定实现。 最终用户将看到以下对话框: