I want to execute some javascript code only with GeckSession. As far as I have explored the API, there doesn't seem to be a direct way to do this. So I have written a small web page which executes the javascript code on load and then I use a prompt to tell the app about the output. This approach was working until I tried to execute the code in a background thread. I started getting warnings regarding main thread in Logcat. Now I want to execute this piece of code hundreds of times so the performance is very slow if I just use the main thread. Is there any work around to fullfil my use case?
Here's the relevant piece of code I currently have:
suspend fun read(html: String) = suspendCoroutine { continuation ->
CoroutineScope(Dispatchers.Main).launch {
val file = prepareFile(html)
val settings = GeckoSessionSettings.Builder().build()
val session = GeckoSession(settings)
session.open(runtime)
session.promptDelegate = object : GeckoSession.PromptDelegate {
override fun onTextPrompt(
session: GeckoSession,
prompt: GeckoSession.PromptDelegate.TextPrompt
): GeckoResult<GeckoSession.PromptDelegate.PromptResponse?>? {
try {
when (prompt.message) {
"readability-result" -> {
val html = (prompt.defaultValue ?: "").trim()
if (html.isNotEmpty()) {
continuation.resume(html)
} else {
continuation.resume(null)
}
}
"readability-error" -> {
continuation.resume(null)
}
}
prompt.dismiss()
} catch (e: Exception) {
e.printStackTrace()
continuation.resume(null)
} finally {
session.close()
safeDelete(file)
}
return super.onTextPrompt(session, prompt)
}
}
session.loadUri("file://${file.absolutePath}")
}
}
I want to execute some javascript code only with
GeckSession. As far as I have explored the API, there doesn't seem to be a direct way to do this. So I have written a small web page which executes the javascript code on load and then I use a prompt to tell the app about the output. This approach was working until I tried to execute the code in a background thread. I started getting warnings regarding main thread in Logcat. Now I want to execute this piece of code hundreds of times so the performance is very slow if I just use the main thread. Is there any work around to fullfil my use case?Here's the relevant piece of code I currently have: