MCPcopy Create free account
hub / github.com/Moli-X/Other / apiRunScript

Function apiRunScript

Backups/KeLee/Resource/Script/BoxJS/BoxJS.js:672–729  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

670}
671
672async function apiRunScript() {
673 // 取消勿扰模式
674 $.isMute = false
675 const opts = $.toObj($request.body)
676 const httpapi = $.getdata('@chavy_boxjs_userCfgs.httpapi')
677 const ishttpapi = /.*?@.*?:[0-9]+/.test(httpapi)
678 let script_text = null
679 if (opts.isRemote) {
680 await $.getScript(opts.url).then((script) => (script_text = script))
681 } else {
682 script_text = opts.script
683 }
684 if (
685 $.isSurge() &&
686 !$.isLoon() &&
687 !$.isShadowrocket() &&
688 !$.isStash() &&
689 ishttpapi
690 ) {
691 const runOpts = { timeout: opts.timeout }
692 await $.runScript(script_text, runOpts).then(
693 (resp) => ($.json = JSON.parse(resp))
694 )
695 } else {
696 await new Promise((resolve) => {
697 $eval_env.resolve = resolve
698 // 避免被执行脚本误认为是 rewrite 环境
699 // 所以需要 `$request = undefined`
700 $eval_env.request = $request
701 $request = undefined
702 // 重写 console.log, 把日志记录到 $eval_env.cached_logs
703 $eval_env.cached_logs = []
704 console.cloned_log = console.log
705 console.log = (l) => {
706 console.cloned_log(l)
707 $eval_env.cached_logs.push(l)
708 }
709 // 重写脚本内的 $done, 调用 $done() 即是调用 $eval_env.resolve()
710 script_text = script_text.replace(/\$done/g, '$eval_env.resolve')
711 script_text = script_text.replace(/\$\.done/g, '$eval_env.resolve')
712 try {
713 eval(script_text)
714 } catch (e) {
715 $eval_env.cached_logs.push(e)
716 resolve()
717 }
718 })
719 // 还原 console.log
720 console.log = console.cloned_log
721 // 还原 $request
722 $request = $eval_env.request
723 // 返回数据
724 $.json = {
725 result: '',
726 output: $eval_env.cached_logs.join('\n')
727 }
728 }
729}

Callers 1

handleApiFunction · 0.85

Calls 11

parseMethod · 0.80
toObjMethod · 0.45
getdataMethod · 0.45
getScriptMethod · 0.45
isSurgeMethod · 0.45
isLoonMethod · 0.45
isShadowrocketMethod · 0.45
isStashMethod · 0.45
runScriptMethod · 0.45
pushMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…