mirror of
https://github.com/gedoor/legado.git
synced 2024-08-30 09:23:26 +08:00
替换规则支持js,可以用js判断匹配到的内容决定替换为什么,匹配到的内容变量为result,替换为@js:开头则自动采用js判断替换内容
This commit is contained in:
parent
4934b3656b
commit
1a20e9f2dd
@ -1,6 +1,8 @@
|
||||
package io.legado.app.utils
|
||||
|
||||
import androidx.core.os.postDelayed
|
||||
import com.script.SimpleBindings
|
||||
import io.legado.app.constant.AppConst
|
||||
import io.legado.app.exception.RegexTimeoutException
|
||||
import io.legado.app.help.CrashHandler
|
||||
import io.legado.app.help.coroutine.Coroutine
|
||||
@ -20,8 +22,23 @@ suspend fun CharSequence.replace(regex: Regex, replacement: String, timeout: Lon
|
||||
return suspendCancellableCoroutine { block ->
|
||||
val coroutine = Coroutine.async {
|
||||
try {
|
||||
val result = regex.replace(charSequence, replacement)
|
||||
block.resume(result)
|
||||
if (replacement.startsWith("@js:")) {
|
||||
val js = replacement.substring(4)
|
||||
val pattern = regex.toPattern()
|
||||
val matcher = pattern.matcher(charSequence)
|
||||
val stringBuffer = StringBuffer()
|
||||
while (matcher.find()) {
|
||||
val bindings = SimpleBindings()
|
||||
bindings["result"] = matcher.group(1)
|
||||
val jsResult = AppConst.SCRIPT_ENGINE.eval(js, bindings).toString()
|
||||
matcher.appendReplacement(stringBuffer, jsResult)
|
||||
}
|
||||
matcher.appendTail(stringBuffer)
|
||||
block.resume(stringBuffer.toString())
|
||||
} else {
|
||||
val result = regex.replace(charSequence, replacement)
|
||||
block.resume(result)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
block.resumeWithException(e)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user