Tuesday, 31 July 2018

How to add javascript function in webview and call it later from html

I am adding a javascript function in webview like this (Kotlin)

val webView = findViewById(R.id.webview) as WebView
webView.getSettings().setJavaScriptEnabled(true)
webView.addJavascriptInterface(this, "android")
webView.getSettings().setBuiltInZoomControls(false)
webView.loadUrl(url)

webView.webViewClient = object : WebViewClient() {
        override fun onPageFinished(view: WebView, url: String) {
            super.onPageFinished(view, url)
            webView.loadUrl("javascript:(function captchaResponse (token){" +
                            "      android.reCaptchaCallbackInAndroid(token);" +
                            "    })()")
        }
    }

Function works fine but the problem is that it runs immediately when I add it in webview. I only want to include it as Javascript function and it should be called only from html (When user will fill the recaptcha). How can I do that?



from How to add javascript function in webview and call it later from html

No comments:

Post a Comment