feat(sdk-webview): make JS bridge name configurable; default to XWebViewBridge
- Add `jsBridgeName` field to `XWebViewConfig` (default: "XWebViewBridge") - Make `DIALOG_OVERRIDE_JS` a function parameterised by bridge name - Use `config.jsBridgeName` in both `XWebViewView` and `XWebViewActivity` - Bump sdk-webview to 1.0.7 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
这个提交包含在:
父节点
79c43b83af
当前提交
6da62dc600
@ -7,5 +7,5 @@ SDK_CORE_VERSION=1.0.6
|
|||||||
SDK_IM_VERSION=1.0.5
|
SDK_IM_VERSION=1.0.5
|
||||||
SDK_PUSH_VERSION=1.0.5
|
SDK_PUSH_VERSION=1.0.5
|
||||||
SDK_UPDATE_VERSION=1.0.6
|
SDK_UPDATE_VERSION=1.0.6
|
||||||
SDK_WEBVIEW_VERSION=1.0.5
|
SDK_WEBVIEW_VERSION=1.0.7
|
||||||
SDK_LICENSE_VERSION=1.0.6
|
SDK_LICENSE_VERSION=1.0.6
|
||||||
|
|||||||
@ -100,7 +100,7 @@ class XWebViewActivity : ComponentActivity() {
|
|||||||
|
|
||||||
addJavascriptInterface(
|
addJavascriptInterface(
|
||||||
XWebViewJsBridge(mainHandler) { config.onMessage },
|
XWebViewJsBridge(mainHandler) { config.onMessage },
|
||||||
"ReactNativeWebView",
|
config.jsBridgeName,
|
||||||
)
|
)
|
||||||
|
|
||||||
webViewClient = object : WebViewClient() {
|
webViewClient = object : WebViewClient() {
|
||||||
|
|||||||
@ -7,6 +7,7 @@ data class XWebViewConfig(
|
|||||||
val hideStatusBar: Boolean = false,
|
val hideStatusBar: Boolean = false,
|
||||||
val userAgent: String? = null,
|
val userAgent: String? = null,
|
||||||
val injectedJavaScript: String? = null,
|
val injectedJavaScript: String? = null,
|
||||||
|
val jsBridgeName: String = "XWebViewBridge",
|
||||||
val onMessage: ((String) -> Unit)? = null,
|
val onMessage: ((String) -> Unit)? = null,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@ -35,11 +35,11 @@ import java.io.File
|
|||||||
import java.util.Locale
|
import java.util.Locale
|
||||||
|
|
||||||
// JS injected into every page to bridge dialog APIs and download interception.
|
// JS injected into every page to bridge dialog APIs and download interception.
|
||||||
// Uses addJavascriptInterface("ReactNativeWebView") for the JS→Native channel.
|
// Uses addJavascriptInterface(jsBridgeName) for the JS→Native channel.
|
||||||
internal val DIALOG_OVERRIDE_JS = """
|
internal fun buildDialogOverrideJs(bridgeName: String) = """
|
||||||
(function() {
|
(function() {
|
||||||
function post(obj) {
|
function post(obj) {
|
||||||
window.ReactNativeWebView.postMessage(JSON.stringify(obj));
|
window.$bridgeName.postMessage(JSON.stringify(obj));
|
||||||
}
|
}
|
||||||
|
|
||||||
window.alert = function(msg) { post({ __xwv: 'alert', msg: String(msg) }); };
|
window.alert = function(msg) { post({ __xwv: 'alert', msg: String(msg) }); };
|
||||||
@ -108,7 +108,7 @@ true;
|
|||||||
""".trimIndent()
|
""".trimIndent()
|
||||||
|
|
||||||
internal fun buildInjectedJs(config: XWebViewConfig): String =
|
internal fun buildInjectedJs(config: XWebViewConfig): String =
|
||||||
DIALOG_OVERRIDE_JS + "\n" + (config.injectedJavaScript ?: "") + "\ntrue;"
|
buildDialogOverrideJs(config.jsBridgeName) + "\n" + (config.injectedJavaScript ?: "") + "\ntrue;"
|
||||||
|
|
||||||
internal fun shouldLoadInWebView(uri: Uri): Boolean {
|
internal fun shouldLoadInWebView(uri: Uri): Boolean {
|
||||||
val scheme = uri.scheme?.lowercase(Locale.ROOT) ?: return true
|
val scheme = uri.scheme?.lowercase(Locale.ROOT) ?: return true
|
||||||
@ -230,7 +230,7 @@ fun XWebViewView(
|
|||||||
// JS → Native bridge. Must be added before loadUrl.
|
// JS → Native bridge. Must be added before loadUrl.
|
||||||
addJavascriptInterface(
|
addJavascriptInterface(
|
||||||
XWebViewJsBridge(mainHandler) { onMessageRef.value },
|
XWebViewJsBridge(mainHandler) { onMessageRef.value },
|
||||||
"ReactNativeWebView",
|
config.jsBridgeName,
|
||||||
)
|
)
|
||||||
|
|
||||||
webViewClient = object : WebViewClient() {
|
webViewClient = object : WebViewClient() {
|
||||||
|
|||||||
正在加载...
在新工单中引用
屏蔽一个用户