dankito/Readability4J

.parse() returns different textContent when calling multiple times in a row

GrayTsar opened this issue · 0 comments

Example Code.
Screenshot.

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val webView = findViewById<WebView>(R.id.webView)
        webView?.let {
            it.settings.javaScriptEnabled = true
            it.webViewClient = MyWebViewClient()
        }
        webView.loadUrl("https://en.wikipedia.org/wiki/The_New_York_Times")
    }

    class MyWebViewClient(): WebViewClient() {
        override fun onPageFinished(view: WebView?, url: String?) {
            super.onPageFinished(view, url)

            view!!.evaluateJavascript("javascript:encodeURI(document.documentElement.outerHTML);") {
                if(!it.isNullOrEmpty()){
                    val txt = URLDecoder.decode(it, "UTF-8")
                    val read = Readability4J(url!!, URLDecoder.decode(it, "UTF-8"))

                    val t1 = read.parse().textContent
                    val t2 = read.parse().textContent
                    val t3 = read.parse().textContent
                    val t4 = read.parse().textContent
                    val t5 = read.parse().articleContent!!.text()
                    val t6 = read.parse().articleContent!!.text()

                    Log.d("DBG", "loaded")
                }
            }
        }
    }
}