Issue
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.
Solution
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.
Answered By - MayNotBe
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.