Skip to content

Commit bb8bcc6

Browse files
committed
更新[BoxJs TF]: 运行脚本可显示执行结果
1 parent 4634083 commit bb8bcc6

3 files changed

Lines changed: 77 additions & 51 deletions

File tree

‎box/chavy.boxjs.html‎

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@
593593
<v-subheader>
594594
<h2>脚本编辑器</h2>
595595
<v-spacer></v-spacer>
596-
<v-btn icon @click="exeScript" :loading="isLoading">
596+
<v-btn icon @click="runTxtScript" :loading="isLoading">
597597
<v-icon color="primary">mdi-play-circle</v-icon>
598598
</v-btn>
599599
</v-subheader>
@@ -605,7 +605,7 @@ <h2>脚本编辑器</h2>
605605
<v-subheader>
606606
<h2 :style="appTitleStyle">{{curapp.name}}</h2>
607607
<v-spacer></v-spacer>
608-
<v-btn v-if="curapp.script" icon :loading="isLoading" @click="runScript(curapp.script, curapp.script_timeout)">
608+
<v-btn v-if="curapp.script" icon :loading="isLoading" @click="runRemoteScript(curapp.script, curapp.script_timeout)">
609609
<v-icon color="primary">mdi-play-circle</v-icon>
610610
</v-btn>
611611
</v-subheader>
@@ -631,7 +631,7 @@ <h2 :style="appTitleStyle">{{curapp.name}}</h2>
631631
<v-list-item-title>
632632
{{scriptIdx + 1}}. {{script.name}}
633633
</v-list-item-title>
634-
<v-btn icon :loading="isLoading" @click.stop="runScript(script.script, script.script_timeout)">
634+
<v-btn icon :loading="isLoading" @click.stop="runRemoteScript(script.script, script.script_timeout)">
635635
<v-icon>mdi-play-circle</v-icon>
636636
</v-btn>
637637
</v-list-item>
@@ -1147,25 +1147,25 @@ <h2>{{curbak.name}}</h2>
11471147
<v-btn dark v-if="!box.usercfgs.isHideRefresh" fab small color="orange" @click="reload()">
11481148
<v-icon>mdi-refresh</v-icon>
11491149
</v-btn>
1150-
<v-btn dark v-if="!box.usercfgs.isHideCoding" fab small @click="path = '/#/coding'">
1150+
<v-btn dark v-if="!box.usercfgs.isHideCoding" fab small @click="switchView('coding')">
11511151
<v-icon>mdi-code-tags</v-icon>
11521152
</v-btn>
11531153
<v-btn dark v-if="!box.usercfgs.isHidedSearch" fab small color="green" @click="ui.searchDialog.show = true">
11541154
<v-icon>mdi-magnify</v-icon>
11551155
</v-btn>
11561156
</v-speed-dial>
11571157
</v-fab-transition>
1158-
<v-bottom-sheet v-model="ui.versionsheet.show" scrollable>
1158+
<v-bottom-sheet v-model="ui.versionsheet.show" scrollable fullscreen>
11591159
<v-card v-if="box.versions">
1160-
<v-subheader>
1160+
<v-subheader v-touch="{ down: () => ui.versionsheet.show = false }">
11611161
<v-btn icon small @click="open('https://chavyleung.gitbook.io/boxjs/base/upgrade')">
11621162
<v-icon>mdi-help-circle</v-icon>
11631163
</v-btn>
11641164
<v-spacer></v-spacer>
11651165
<v-btn text small v-if="hasNewVersion">新版本</v-btn>
11661166
<v-spacer></v-spacer>
11671167
<v-btn icon small @click="ui.versionsheet.show = false">
1168-
<v-icon>mdi-close</v-icon>
1168+
<v-icon>mdi-chevron-double-down</v-icon>
11691169
</v-btn>
11701170
</v-subheader>
11711171
<v-divider></v-divider>
@@ -1182,21 +1182,21 @@ <h2 :class="version === ver.version ? 'primary--text' : undefined">v{{ver.versio
11821182
</v-card-text>
11831183
</v-card>
11841184
</v-bottom-sheet>
1185-
<v-bottom-sheet v-model="ui.exeScriptSheet.show" scrollable>
1185+
<v-bottom-sheet v-model="ui.exeScriptSheet.show" scrollable fullscreen>
11861186
<v-card>
1187-
<v-card-title>
1187+
<v-card-title v-touch="{ down: () => ui.exeScriptSheet.show = false }">
11881188
执行结果
11891189
<v-spacer></v-spacer>
11901190
<v-btn icon @click="ui.exeScriptSheet.show = false">
1191-
<v-icon>mdi-close</v-icon>
1191+
<v-icon>mdi-chevron-double-down</v-icon>
11921192
</v-btn>
11931193
</v-card-title>
11941194
<v-divider></v-divider>
11951195
<v-card-text style="height: 80%;">
11961196
<div class="mt-4" v-if="ui.exeScriptSheet.resp">
11971197
<p class="text-pre-wrap" v-if="ui.exeScriptSheet.resp.exception" v-text="ui.exeScriptSheet.resp.exception"></p>
11981198
<p class="text-pre-wrap" v-else-if="ui.exeScriptSheet.resp.output" v-text="ui.exeScriptSheet.resp.output"></p>
1199-
<p v-else v-text="ui.exeScriptSheet.resp"></p>
1199+
<p v-else v-text="JSON.stringify(ui.exeScriptSheet.resp)"></p>
12001200
</div>
12011201
</v-card-text>
12021202
</v-card>
@@ -1645,9 +1645,7 @@ <h2 :class="version === ver.version ? 'primary--text' : undefined">v{{ver.versio
16451645
' * 这段脚本是可以直接运行的!',
16461646
' */ ',
16471647
'const host = $.getdata("boxjs_host")',
1648-
'$.json = {',
1649-
' msg: "这段 json 是返回给浏览器的!"',
1650-
'}',
1648+
'console.log("输出的内容是返回给浏览器的!")',
16511649
'$.msg($.name, host)',
16521650
'$.done()',
16531651
'// $done() 或 $.done() 都可以'
@@ -1675,7 +1673,7 @@ <h2 :class="version === ver.version ? 'primary--text' : undefined">v{{ver.versio
16751673
this.ui.editor = monaco.editor.create(document.getElementById('container'), {
16761674
fontSize: 12,
16771675
tabSize: 2,
1678-
value: this.box.usercfgs.httpapi ? surgejs_demo.join('\n') : envjs_demo.join('\n'),
1676+
value: this.env === 'Surge' && this.box.usercfgs.httpapi ? surgejs_demo.join('\n') : envjs_demo.join('\n'),
16791677
language: 'javascript',
16801678
minimap: { enabled: false },
16811679
theme: this.isDarkMode ? 'vs-dark' : 'vs'
@@ -1988,24 +1986,20 @@ <h2 :class="version === ver.version ? 'primary--text' : undefined">v{{ver.versio
19881986
app.isLoaded = true
19891987
},
19901988
// 运行远程脚本
1991-
runScript(url, timeout) {
1989+
runRemoteScript(url, timeout) {
19921990
const opts = { url, timeout, isRemote: true }
1993-
axios.post('/api/runScript', opts).then((resp) => {
1994-
if (this.box.usercfgs.httpapi) {
1995-
this.ui.exeScriptSheet.resp = JSON.parse(resp.data)
1996-
this.ui.exeScriptSheet.show = true
1997-
}
1998-
})
1991+
this.runScript(opts)
19991992
},
20001993
// 运行文本脚本
2001-
exeScript() {
1994+
runTxtScript() {
20021995
const script = this.ui.editor.getValue()
20031996
const opts = { script }
1997+
this.runScript(opts)
1998+
},
1999+
runScript(opts) {
20042000
axios.post('/api/runScript', opts).then((resp) => {
2005-
if (this.box.usercfgs.httpapi) {
2006-
this.ui.exeScriptSheet.resp = JSON.parse(resp.data)
2007-
this.ui.exeScriptSheet.show = true
2008-
}
2001+
this.ui.exeScriptSheet.resp = resp.data
2002+
this.ui.exeScriptSheet.show = true
20092003
})
20102004
},
20112005
// 保存用户偏好

‎box/chavy.boxjs.js‎

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
const $ = new Env('BoxJs')
22

3-
$.version = '0.7.43'
3+
// 为 eval 准备的上下文环境
4+
const $eval_env = {}
5+
6+
$.version = '0.7.44'
47
$.versionType = 'beta'
58

69
/**
@@ -537,29 +540,47 @@ async function apiRunScript() {
537540
const opts = $.toObj($request.body)
538541
const httpapi = $.getdata('@chavy_boxjs_userCfgs.httpapi')
539542
const ishttpapi = /.*?@.*?:[0-9]+/.test(httpapi)
543+
let script_text = null
540544
if (opts.isRemote) {
541-
if ($.isSurge() && ishttpapi) {
542-
const runOpts = { timeout: opts.timeout }
543-
let script_text = null
544-
await $.getScript(opts.url).then((script) => (script_text = script))
545-
await $.runScript(script_text, runOpts).then((resp) => ($.json = resp))
546-
} else {
547-
$.getScript(opts.url).then((script) => {
548-
// 避免被执行脚本误认为是 rewrite 环境
549-
// 所以需要 `$request = undefined`
550-
$request = undefined
551-
eval(script)
552-
})
553-
}
545+
await $.getScript(opts.url).then((script) => (script_text = script))
554546
} else {
555-
// 对于手动执行的脚本, 把 $done 的时机交给脚本自主控制
556-
if ($.isSurge() && ishttpapi) {
557-
const runOpts = { timeout: opts.timeout }
558-
await $.runScript(opts.script, runOpts).then((resp) => ($.json = resp))
559-
} else {
560-
$.isSkipDone = true
547+
script_text = opts.script
548+
}
549+
if ($.isSurge() && ishttpapi) {
550+
const runOpts = { timeout: opts.timeout }
551+
await $.runScript(script_text, runOpts).then((resp) => ($.json = JSON.parse(resp)))
552+
} else {
553+
await new Promise((resolve) => {
554+
$eval_env.resolve = resolve
555+
// 避免被执行脚本误认为是 rewrite 环境
556+
// 所以需要 `$request = undefined`
557+
$eval_env.request = $request
561558
$request = undefined
562-
eval(opts.script)
559+
// 重写 console.log, 把日志记录到 $.cached_logs
560+
$.cached_logs = []
561+
console.cloned_log = console.log
562+
console.log = (l) => {
563+
console.cloned_log(l)
564+
$.cached_logs.push(l)
565+
}
566+
// 重写脚本内的 $done, 调用 $done() 即是调用 $eval_env.resolve()
567+
script_text = script_text.replace(/\$done/g, '$eval_env.resolve')
568+
script_text = script_text.replace(/\$\.done/g, '$eval_env.resolve')
569+
try {
570+
eval(script_text)
571+
} catch (e) {
572+
$.cached_logs.push(e)
573+
resolve()
574+
}
575+
})
576+
// 还原 console.log
577+
console.log = console.cloned_log
578+
// 还原 $request
579+
$request = $eval_env.request
580+
// 返回数据
581+
$.json = {
582+
result: '',
583+
output: $.cached_logs.join('\n')
563584
}
564585
}
565586
}
@@ -626,9 +647,6 @@ function upgradeUserData() {
626647
* ===================================
627648
*/
628649
function doneBox() {
629-
if ($.isSkipDone === true) {
630-
return
631-
}
632650
// 记录当前使用哪个域名访问
633651
$.setdata(getHost($request.url), $.KEY_boxjs_host)
634652
if ($.isOptions) doneOptions()

‎box/release/box.release.tf.json‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
{
22
"releases": [
3+
{
4+
"version": "0.7.44",
5+
"tags": ["beta"],
6+
"author": "@chavyleung",
7+
"icon": "https://avatars3.githubusercontent.com/u/29748519",
8+
"repo": "https://github.com/chavyleung",
9+
"msg": "更新[BoxJs TF]: 运行脚本可显示执行结果",
10+
"notes": [
11+
{
12+
"name": "新增",
13+
"descs": ["Surge、QuanX、Loon 手动运行脚本时都可以显示执行结果"]
14+
}
15+
]
16+
},
317
{
418
"version": "0.7.43",
519
"tags": ["beta"],

0 commit comments

Comments
 (0)