How to add a JavaScript function in WebView and call it later from HTML upon submitting reCAPTCHA

前端 未结 2 1327
萌比男神i
萌比男神i 2021-02-05 22:58

I am adding a JavaScript function in WebView like this (Kotlin):

val webView = findViewById(R.id.webview) as WebView
webView.getSettings().setJavaScriptEnabled(t         


        
2条回答
  •  渐次进展
    2021-02-05 23:09

    Try injecting the script like this,

    function addCode(code){
    var addedScript= document.createElement('script');
    addedScript.text= code;
    document.body.appendChild(addedScript);}
    

    now call the function like,

    val codeToExec = "function captchaResponse (token){" +
                        "android.reCaptchaCallbackInAndroid(token);" +
                        "}";
    

    now exec loadurl like,

    webview.loadUrl("javascript:(function addCode(code){
    var addedScript= document.createElement('script');
    addedScript.text= code;
    document.body.appendChild(addedScript);})(codeToExec));
    

提交回复
热议问题