1

I have this html in my assets folder.

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<script type="text/javascript">
        var publicKey = ""

        function setPublicKey(key) {
            publicKey = key;

            var url = '<' + 'script src="https://<my_url>?onload=loadChallenge" async defer>' + '<' + '/script>'
            document.write(url);
        }

        function loadChallenge() {
            //do some stuff using the publicKey
            new Thing( {public_key: publicKey } )
        }
     </script>
<div id="CAPTCHA"></div>
</body>
</html>

I need to load it into a webview, pass and set the publicKey, and then load the script that uses the onLoad param in the url.

So I'm loading my html into the webview and using this WebViewClient:

private val myWebViewClient = object : WebViewClient() {
        override fun onPageFinished(view: WebView?, url: String?) {
            view?.loadUrl("javascript:setPublicKey($publicKey)")
        }
    }

document.write(url) isn't working. document.write("anything") isn't working.

I'm not married to this solution, I just need a solution that lets me load the html, then set the publicKey, and then load that script.

Any help would be greatly appreciated.

4
  • Do you set javascript enabled to true, like this myWebViewClient.settings.javaScriptEnabled = true? Commented Jun 17, 2019 at 23:29
  • yep :( all the flags are set Commented Jun 17, 2019 at 23:51
  • In Java we need to wrap the string param in quotes like that "javascript:setPublic( ' " + publicKey + " ' )" maybe you need to do something like that in Kotlin. Do You successfully loaded the html in webview? Commented Jun 18, 2019 at 0:25
  • No, that's not it, thanks Commented Jun 18, 2019 at 12:18

1 Answer 1

1

I solved it with this script tag:

<script type="text/javascript">
    var publicKey = ""

    function setPublicKey(key) {
        publicKey = key;
        console.log("public key: " + publicKey);
        var url = "<my_url>?onload=loadChallenge"
        var s = document.createElement('script');
        s.setAttribute('src', url);
        s.async = true;
        document.body.appendChild(s);
    }

    function loadChallenge() {
        //do some stuff using the publicKey
        new Thing( {public_key: publicKey } )
    }
 </script>

First I load the html into the webview from the file.

The webview is using this WebViewClient:

private val myWebViewClient = object : WebViewClient() {
    override fun onPageFinished(view: WebView?, url: String?) {
        view?.loadUrl("javascript:setPublicKey(`$publicKey`)")
    }
}

This waits for the html to load and calls setPublicKey() which sets the key and loads the script.

Works like a charm.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.