()
| 670 | } |
| 671 | |
| 672 | async 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 | } |
no test coverage detected
searching dependent graphs…