diff --git a/app/src/main/java/com/paulcoding/hviewer/js/JS.kt b/app/src/main/java/com/paulcoding/hviewer/js/JS.kt index 2434c59..88f2a01 100644 --- a/app/src/main/java/com/paulcoding/hviewer/js/JS.kt +++ b/app/src/main/java/com/paulcoding/hviewer/js/JS.kt @@ -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() @@ -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() } diff --git a/app/src/main/java/com/paulcoding/hviewer/js/JsFunctions.kt b/app/src/main/java/com/paulcoding/hviewer/js/JsFunctions.kt index 016bd04..bb42f1d 100644 --- a/app/src/main/java/com/paulcoding/hviewer/js/JsFunctions.kt +++ b/app/src/main/java/com/paulcoding/hviewer/js/JsFunctions.kt @@ -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 @@ -17,7 +22,9 @@ val fetchFunction = object : BaseFunction() { thisObj: Scriptable?, args: Array ): 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() @@ -31,6 +38,28 @@ val fetchFunction = object : BaseFunction() { } } +val xhrFunction = object : BaseFunction() { + override fun call( + cx: Context?, + scope: Scriptable?, + thisObj: Scriptable?, + args: Array + ): 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?,