kunfei 3 лет назад
Родитель
Сommit
64abc741cc

+ 19 - 0
app/src/main/java/io/legado/app/help/JsEngine.kt

@@ -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)
+    }
+
+}

+ 21 - 0
app/src/main/java/io/legado/app/ui/about/DonateFragment.kt

@@ -4,10 +4,15 @@ import android.content.Context
 import android.content.Intent
 import android.os.Bundle
 import android.view.View
+import androidx.lifecycle.lifecycleScope
 import androidx.preference.Preference
 import androidx.preference.PreferenceFragmentCompat
 import io.legado.app.R
+import io.legado.app.help.JsEngine
+import io.legado.app.help.coroutine.Coroutine
 import io.legado.app.utils.*
+import org.intellij.lang.annotations.Language
+import splitties.init.appCtx
 
 
 class DonateFragment : PreferenceFragmentCompat() {
@@ -38,6 +43,7 @@ class DonateFragment : PreferenceFragmentCompat() {
             "qqSkRwm" -> requireContext().openUrl(qqSkRwmUrl)
             "zfbHbSsm" -> getZfbHb(requireContext())
             "gzGzh" -> requireContext().sendToClip("开源阅读")
+            "ktt" -> openKtt()
         }
         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)
+            }
+    }
+
 }

+ 0 - 34
app/src/main/java/io/legado/app/utils/JsUtils.kt

@@ -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 - 0
app/src/main/res/xml/donate.xml

@@ -15,6 +15,12 @@
             android:title="@string/follow_official_account"
             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
             android:key="wxZsm"
             android:summary="@string/click_to_open"