替换规则支持js,可以用js判断匹配到的内容决定替换为什么,匹配到的内容变量为result,替换为@js:开头则自动采用js判断替换内容
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
package io.legado.app.utils
|
package io.legado.app.utils
|
||||||
|
|
||||||
import androidx.core.os.postDelayed
|
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.exception.RegexTimeoutException
|
||||||
import io.legado.app.help.CrashHandler
|
import io.legado.app.help.CrashHandler
|
||||||
import io.legado.app.help.coroutine.Coroutine
|
import io.legado.app.help.coroutine.Coroutine
|
||||||
@@ -20,8 +22,23 @@ suspend fun CharSequence.replace(regex: Regex, replacement: String, timeout: Lon
|
|||||||
return suspendCancellableCoroutine { block ->
|
return suspendCancellableCoroutine { block ->
|
||||||
val coroutine = Coroutine.async {
|
val coroutine = Coroutine.async {
|
||||||
try {
|
try {
|
||||||
val result = regex.replace(charSequence, replacement)
|
if (replacement.startsWith("@js:")) {
|
||||||
block.resume(result)
|
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) {
|
} catch (e: Exception) {
|
||||||
block.resumeWithException(e)
|
block.resumeWithException(e)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user