This commit is contained in:
kunfei
2023-07-16 21:04:58 +08:00
parent b2bf49f2a4
commit 649e481d72
4 changed files with 46 additions and 34 deletions
@@ -0,0 +1,19 @@
package io.legado.app.help
import com.script.SimpleBindings
import com.script.rhino.RhinoScriptEngine
import io.legado.app.data.entities.BaseSource
object JsEngine : JsExtensions {
override fun getSource(): BaseSource? {
return null
}
fun eval(js: String): Any? {
val bindings = SimpleBindings()
bindings["java"] = this
return RhinoScriptEngine.eval(js, bindings)
}
}
@@ -4,10 +4,15 @@ import android.content.Context
import android.content.Intent import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.view.View import android.view.View
import androidx.lifecycle.lifecycleScope
import androidx.preference.Preference import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceFragmentCompat
import io.legado.app.R import io.legado.app.R
import io.legado.app.help.JsEngine
import io.legado.app.help.coroutine.Coroutine
import io.legado.app.utils.* import io.legado.app.utils.*
import org.intellij.lang.annotations.Language
import splitties.init.appCtx
class DonateFragment : PreferenceFragmentCompat() { class DonateFragment : PreferenceFragmentCompat() {
@@ -38,6 +43,7 @@ class DonateFragment : PreferenceFragmentCompat() {
"qqSkRwm" -> requireContext().openUrl(qqSkRwmUrl) "qqSkRwm" -> requireContext().openUrl(qqSkRwmUrl)
"zfbHbSsm" -> getZfbHb(requireContext()) "zfbHbSsm" -> getZfbHb(requireContext())
"gzGzh" -> requireContext().sendToClip("开源阅读") "gzGzh" -> requireContext().sendToClip("开源阅读")
"ktt" -> openKtt()
} }
return super.onPreferenceTreeClick(preference) return super.onPreferenceTreeClick(preference)
} }
@@ -58,4 +64,19 @@ class DonateFragment : PreferenceFragmentCompat() {
} }
} }
private fun openKtt() {
Coroutine.async(lifecycleScope) {
@Language("js")
val js = """
java.webViewGetOverrideUrl(null, "https://ktt.pinduoduo.com/t/tlQQsofbQM", null, "weixin:.*")
""".trimIndent()
JsEngine.eval(js).toString()
}.timeout(10000)
.onSuccess {
activity?.openUrl(it)
}.onError {
appCtx.toastOnUi(it.localizedMessage)
}
}
} }
@@ -1,34 +0,0 @@
package io.legado.app.utils
import com.script.SimpleBindings
import com.script.rhino.RhinoScriptEngine
import io.legado.app.constant.AppPattern.EXP_PATTERN
object JsUtils {
fun evalJs(js: String, bindingsFun: ((SimpleBindings) -> Unit)? = null): String {
val bindings = SimpleBindings()
bindingsFun?.invoke(bindings)
if (js.contains("{{") && js.contains("}}")) {
val sb = StringBuffer()
val expMatcher = EXP_PATTERN.matcher(js)
while (expMatcher.find()) {
val result = expMatcher.group(1)?.let { js1 ->
RhinoScriptEngine.eval(js1, bindings)
} ?: ""
if (result is String) {
expMatcher.appendReplacement(sb, result)
} else if (result is Double && result % 1.0 == 0.0) {
expMatcher.appendReplacement(sb, String.format("%.0f", result))
} else {
expMatcher.appendReplacement(sb, result.toString())
}
}
expMatcher.appendTail(sb)
return sb.toString()
}
return RhinoScriptEngine.eval(js, bindings).toString()
}
}
+6
View File
@@ -15,6 +15,12 @@
android:title="@string/follow_official_account" android:title="@string/follow_official_account"
app:iconSpaceReserved="false" /> app:iconSpaceReserved="false" />
<io.legado.app.lib.prefs.Preference
android:key="ktt"
android:summary="快团团购物支持"
android:title="我的店铺"
app:iconSpaceReserved="false" />
<io.legado.app.lib.prefs.Preference <io.legado.app.lib.prefs.Preference
android:key="wxZsm" android:key="wxZsm"
android:summary="@string/click_to_open" android:summary="@string/click_to_open"