1

I have a very simple activity where I will later try to extract some information from a web page that is shown in a webView. For now I simplified it to a script which just changes the text color and returns the string "res" (see code below).
The problem is that neither the color change nor the return value have any effect. The value that is actually returned is null.
I read that there is a problem with evaluateJavascript on API<19, but I am on a higher API version so that shouldn't be the issue. I also tried to wrap the javascript in a function (since it uses the return statement) which did not help either.
What am I missing? I'll be grateful for any ideas!

class MyActivity : AppCompatActivity() {
    private lateinit var textView : TextView
    private lateinit var webView : WebView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.myActivity)


        webView = findViewById<WebView>(R.id.webView)
        webView.webViewClient = WebViewClient()
        webView.settings.javaScriptEnabled = true
        webView.loadUrl("https://www.google.com")

        btnAddNote = findViewById(R.id.buttonAddNote)
        btnAddNote.setOnClickListener(fun(_) {
            webView.evaluateJavascript(
                """
                document.body.style.color = "blue !important";
                return "res"
                """
                ) { res -> textView.text = res} // res is null
        })
    }
}
  • https://stackoverflow.com/questions/19788294/how-does-evaluatejavascript-work – ADM Aug 07 '22 at 11:55
  • I saw that one and a few similar questions. I copied my code directly from one of the answers to one of the questions which had Kotlin code but still doesn't work. – user6287736 Aug 07 '22 at 12:23

0 Answers0