Skip to content

Commit

Permalink
running javascript on main thread (webview)
Browse files Browse the repository at this point in the history
  • Loading branch information
whoisjeeva committed Mar 26, 2022
1 parent ea7ba5e commit cf7610b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ dependencies {
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.material:material:$compose_version"
implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.0'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.1'
implementation 'androidx.activity:activity-compose:1.4.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
Expand Down
8 changes: 4 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
buildscript {
ext {
compose_version = '1.0.5'
compose_version = '1.1.1'
}
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '7.1.1' apply false
id 'com.android.library' version '7.1.1' apply false
id 'org.jetbrains.kotlin.android' version '1.5.31' apply false
id 'com.android.application' version '7.1.2' apply false
id 'com.android.library' version '7.1.2' apply false
id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
}

task clean(type: Delete) {
Expand Down
15 changes: 10 additions & 5 deletions ghostjs/src/main/java/me/gumify/ghostjs/Ghostjs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package me.gumify.ghostjs
import android.annotation.SuppressLint
import android.content.Context
import android.webkit.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.json.JSONObject

@SuppressLint("SetJavaScriptEnabled")
Expand Down Expand Up @@ -52,7 +55,8 @@ class Ghostjs(private val context: Context) {
private suspend fun evaluator(script: String): String? {
var output: String? = null
var isDone = false
webView.evaluateJavascript("""
CoroutineScope(Dispatchers.Main).launch {
webView.evaluateJavascript("""
(function() {
try {
var __GHOST_DATA = (function() {
Expand All @@ -64,10 +68,11 @@ class Ghostjs(private val context: Context) {
}
})();
""".trimIndent()) {
try {
output = it
} catch (e: Exception) {}
isDone = true
try {
output = it
} catch (e: Exception) {}
isDone = true
}
}
while (!isDone) {
delay(100)
Expand Down

0 comments on commit cf7610b

Please sign in to comment.