Skip to content

Commit

Permalink
feat: add xhr function for json
Browse files Browse the repository at this point in the history
  • Loading branch information
paulcoding810 committed Nov 29, 2024
1 parent dea3b51 commit 0566818
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
23 changes: 15 additions & 8 deletions app/src/main/java/com/paulcoding/hviewer/js/JS.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ fun toJsonElement(jsObject: Any?, gson: Gson = Gson()): JsonElement {
}
}

class JS(siteConfig: SiteConfig) {
private val fileName = siteConfig.scriptFile

class JS(siteConfig: SiteConfig? = null) {
var scope: ScriptableObject
val gson = Gson()

Expand All @@ -51,19 +49,28 @@ class JS(siteConfig: SiteConfig) {
init {
val context = prepareContext()
scope = context.initStandardObjects()
ScriptableObject.putProperty(scope, "baseUrl", Context.javaToJS(siteConfig.baseUrl, scope))
ScriptableObject.putProperty(scope, "import", importFunction)
ScriptableObject.putProperty(scope, "fetch", fetchFunction)
ScriptableObject.putProperty(scope, "xhr", xhrFunction)
ScriptableObject.putProperty(scope, "atob", atobFunction)
ScriptableObject.putProperty(scope, "console", NativeObject().apply {
put("log", this, logFunction)
})

try {
val reader = FileReader(File(appContext.scriptsDir, fileName))
context.evaluateReader(scope, reader, fileName, 1, null)
Context.exit()
reader.close()
siteConfig?.let {
val fileName = it.scriptFile

ScriptableObject.putProperty(
scope,
"baseUrl",
Context.javaToJS(it.baseUrl, scope)
)
val reader = FileReader(File(appContext.scriptsDir, fileName))
context.evaluateReader(scope, reader, fileName, 1, null)
Context.exit()
reader.close()
}
} catch (e: Exception) {
e.printStackTrace()
}
Expand Down
31 changes: 30 additions & 1 deletion app/src/main/java/com/paulcoding/hviewer/js/JsFunctions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ package com.paulcoding.hviewer.js
import com.paulcoding.hviewer.MainApp.Companion.appContext
import com.paulcoding.hviewer.helper.log
import com.paulcoding.hviewer.helper.readFile
import com.paulcoding.hviewer.network.ktorClient
import io.ktor.client.call.body
import io.ktor.client.request.get
import kotlinx.coroutines.runBlocking
import org.jsoup.Jsoup
import org.mozilla.javascript.BaseFunction
import org.mozilla.javascript.Context
import org.mozilla.javascript.NativeJSON
import org.mozilla.javascript.Scriptable
import kotlin.io.encoding.Base64
import kotlin.io.encoding.ExperimentalEncodingApi
Expand All @@ -17,7 +22,9 @@ val fetchFunction = object : BaseFunction() {
thisObj: Scriptable?,
args: Array<out Any?>
): Any? {
val url = args.getOrNull(0) as? String ?: throw IllegalArgumentException("URL is required")
val url = args.getOrNull(0) as? String
if (url.isNullOrEmpty())
throw IllegalArgumentException("URL is required")

return runCatching {
Jsoup.connect(url).followRedirects(true).get()
Expand All @@ -31,6 +38,28 @@ val fetchFunction = object : BaseFunction() {
}
}

val xhrFunction = object : BaseFunction() {
override fun call(
cx: Context?,
scope: Scriptable?,
thisObj: Scriptable?,
args: Array<out Any>
): Any {
val url = args.getOrNull(0) as? String
if (url.isNullOrEmpty())
throw IllegalArgumentException("URL is required")

return runBlocking {
ktorClient.use { client ->
val res: String = client.get(url).body()
NativeJSON.parse(
cx, scope, res
) { cx, scope, thisObj, args -> args[1] }
}
}
}
}

val logFunction = object : BaseFunction() {
override fun call(
cx: Context?,
Expand Down

0 comments on commit 0566818

Please sign in to comment.