通过WebView访问各种HTML Navigator属性

发布于 2025-02-05 07:00:32 字数 802 浏览 3 评论 0原文

我的任务是“本地”实施3DS付款验证流。当然,我们将重定向用户,显示特定的HTML内容,拨打各种电话等。根据服务提供商的说法。 他们用于初始化3DS流程的API请求信息,例如:

browserip字符串客户端的IP。它可以是IPv4或IPv6。

根据IETF BCP47,Navigator_language字符串语言。得到这个 navigator.language html属性。

navigator_javaenabled字符串从中获取此值 navigator.javaenabled html属性。

navigator_jsenabled字符串'true'如果在 客户的浏览器。 'false'否则。

screen_colordepth字符串从screen.colordepth html获取此值 属性。

screen_height字符串从screen.height html属性获取此值。

screen_width字符串从screen.width html属性获取此值。

timezoneOffset字符串通过运行'new来获取此值 date()。getTimeZoneOffset();'在客户端的浏览器中。

用户词字符串必须包含HTTP用户代理标头值。

browseraccept字符串它必须包含HTTP接受标头值。

我知道我可能可以获取用户的IP,启用JS的IP,屏幕尺寸& Web视图设置中的用户代理字符串&设备的配置属性,但是如何访问所有其他字段?我找不到附加在Web视图或其设置上的导航对象。是否有一种本地方法来检索所有这些细节?

I'm tasked with implementing a 3DS payment verification flow “natively”. We will of course be redirecting users, showing specific HTML content, making various calls, etc. according to the service provider.
Their API for initializing the 3DS process requests information such as :

BrowserIP string The IP of the client. It can be IPv4 or IPv6.

Navigator_language string Language according to IETF BCP47. Get this
value from navigator.language HTML property.

Navigator_javaEnabled string Get this value from
navigator.javaEnabled HTML property.

Navigator_jsEnabled string 'true' if javascript is enabled in
client's browser. 'false' otherwise.

Screen_colorDepth string Get this value from screen.colorDepth HTML
property.

Screen_height string Get this value from screen.height HTML property.

Screen_width string Get this value from screen.width HTML property.

TimezoneOffset string Get this value by running 'new
Date().getTimezoneOffset();' in the client's browser.

UserAgent string It must contain the HTTP user-agent header value.

BrowserAccept string It must contain the HTTP accept header value.

I know that I can probably get the user's IP, JS-enabled, screen dimensions & user-agent string from the web view's settings & the device's configuration properties, but how would I access all these other fields? I couldn't find a navigator object attached to the web view or its settings. Is there a native way for retrieving all these details?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

围归者 2025-02-12 07:00:32

好吧,经过很多比赛,我弄清楚了!首先,我使用androidView composable:在其中创建了一个普通的WebView:

@SuppressLint("SetJavaScriptEnabled")
@Composable
fun Payments3DSWebViewScreen(
    navController: NavController,
    accountId: String?,
    cardInfo: CardInfo,
    paymentsViewModel: PaymentsViewModel = get()
) {
    val context = LocalContext.current
    var webView by remember { mutableStateOf<WebView?>(null) }
    Column(modifier = Modifier.fillMaxSize()) {
        AndroidView(
            factory = {
                WebView(context).apply {
                    settings.javaScriptEnabled = true
                    addJavascriptInterface(PaymentsJSInterface(), "ANDROID")
                    webChromeClient = object : WebChromeClient() {
                        override fun onJsAlert(
                            view: WebView?,
                            url: String?,
                            message: String?,
                            result: JsResult?
                        ): Boolean {
                            return super.onJsAlert(view, url, message, result)
                        }
                    }
                    webView = this
                    webView?.loadUrl("javascript: ANDROID.getNavigatorInfo(navigator.language || navigator.userLanguage, navigator.javaEnabled());")
                }
            },
            modifier = Modifier
                .fillMaxSize()
                .background(Color.White)
        )
    }
    BackHandler {
        if (webView != null) {
            if (webView!!.canGoBack()) {
                webView?.goBack()
            } else {
                navController.popBackStack()
            }
        } else {
            navController.popBackStack()
        }
    }
}

在其中,我使用了web Chromeclient实例,而不是常规webViewclient,因为我阅读了它使其提供的方法更容易(即OnjSalert())。接下来,我通过Web视图设置启用了JavaScript,最后添加了一个自定义JS接口,该界面将完成所有提取工作。

这是PaymentsJsInterface类:

class PaymentsJSInterface {

    @JavascriptInterface
    fun getNavigatorInfo(lang: String, javaEnabled: Boolean) {
        Log.d("PAYMENTJS", "$lang | $javaEnabled")
    }
}

因此,通过使用loadurl()方法在WebView中运行自定义JS脚本来实现提取,并指定URL是JS脚本(因此javascript:</代码>在开始)。完成此操作后,您可以访问JavaScriptInterface方法(通过@JavaScriptInterface通过“ Android”(在我的情况下)(在我的情况下)'命名空间'。由于JS代码在WebView中运行,您现在可以访问各种HTML属性例如navigator一个。

Alright after a lot of playing around I figured it out! First and foremost I created a normal WebView using the AndroidView composable:

@SuppressLint("SetJavaScriptEnabled")
@Composable
fun Payments3DSWebViewScreen(
    navController: NavController,
    accountId: String?,
    cardInfo: CardInfo,
    paymentsViewModel: PaymentsViewModel = get()
) {
    val context = LocalContext.current
    var webView by remember { mutableStateOf<WebView?>(null) }
    Column(modifier = Modifier.fillMaxSize()) {
        AndroidView(
            factory = {
                WebView(context).apply {
                    settings.javaScriptEnabled = true
                    addJavascriptInterface(PaymentsJSInterface(), "ANDROID")
                    webChromeClient = object : WebChromeClient() {
                        override fun onJsAlert(
                            view: WebView?,
                            url: String?,
                            message: String?,
                            result: JsResult?
                        ): Boolean {
                            return super.onJsAlert(view, url, message, result)
                        }
                    }
                    webView = this
                    webView?.loadUrl("javascript: ANDROID.getNavigatorInfo(navigator.language || navigator.userLanguage, navigator.javaEnabled());")
                }
            },
            modifier = Modifier
                .fillMaxSize()
                .background(Color.White)
        )
    }
    BackHandler {
        if (webView != null) {
            if (webView!!.canGoBack()) {
                webView?.goBack()
            } else {
                navController.popBackStack()
            }
        } else {
            navController.popBackStack()
        }
    }
}

Within it, I used a WebChromeClient instance instead of a regular WebViewClient since I read that it makes such things easier with its provided methods (i.e. onJsAlert()). Next up, I enabled javascript through the web view settings and finally added a custom JS Interface that will do all of the extraction work.

Here's the PaymentsJSInterface class:

class PaymentsJSInterface {

    @JavascriptInterface
    fun getNavigatorInfo(lang: String, javaEnabled: Boolean) {
        Log.d("PAYMENTJS", "$lang | $javaEnabled")
    }
}

So the extraction is achieved by running a custom JS script inside the webview using the loadUrl() method and specifying that the URL is a JS script (hence the javascript: at the beginning). Once you do that, you can access the JavascriptInterface methods (that are annotated with @JavascriptInterface through the "ANDROID" (in my case) 'namespace'. Since the JS code is running within a webView, you now have access to various HTML attributes such as the Navigator one. With that, I could extract the necessary info by calling my JSInterface's method with the necessary parameters!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文