Skip to content

Commit e2605e2

Browse files
committed
更新[BoxJs]: 代码编辑器: Surge HTTP-API 环境下执行的代码能显示响应内容
1 parent 40d4a04 commit e2605e2

3 files changed

Lines changed: 100 additions & 30 deletions

File tree

‎box/chavy.boxjs.html‎

Lines changed: 78 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -555,9 +555,35 @@
555555
<v-subheader>
556556
<h2>脚本编辑器</h2>
557557
<v-spacer></v-spacer>
558-
<v-btn icon @click="exeScript">
558+
<v-btn icon @click="exeScript" :loading="isLoading">
559559
<v-icon color="primary">mdi-play-circle</v-icon>
560560
</v-btn>
561+
<v-bottom-sheet v-model="ui.exeScriptSheet.show" scrollable>
562+
<v-card>
563+
<v-card-title>
564+
执行结果
565+
<v-spacer></v-spacer>
566+
<v-btn icon @click="ui.exeScriptSheet.show = false">
567+
<v-icon>mdi-close</v-icon>
568+
</v-btn>
569+
</v-card-title>
570+
<v-divider></v-divider>
571+
<v-card-text style="height: 80%;">
572+
<div class="mt-4">
573+
<p
574+
class="text-pre-wrap"
575+
v-if="ui.exeScriptSheet.resp && ui.exeScriptSheet.resp.exception"
576+
v-text="ui.exeScriptSheet.resp.exception"
577+
></p>
578+
<p
579+
class="text-pre-wrap"
580+
v-else-if="ui.exeScriptSheet.resp && ui.exeScriptSheet.resp.output"
581+
v-text="ui.exeScriptSheet.resp.output"
582+
></p>
583+
</div>
584+
</v-card-text>
585+
</v-card>
586+
</v-bottom-sheet>
561587
</v-subheader>
562588
</v-card>
563589
<div class="pa-0" id="container" style="width: inherit; height: inherit;"></div>
@@ -1166,6 +1192,7 @@ <h2 :class="version === ver.version ? 'primary--text' : undefined">v{{ver.versio
11661192
searchDialog: { show: false },
11671193
versionsheet: { show: false },
11681194
updatesheet: { show: false },
1195+
exeScriptSheet: { show: false, resp: null },
11691196
naviDrawer: { show: false },
11701197
modSessionDialog: { show: false },
11711198
editProfileDialog: { show: false },
@@ -1506,32 +1533,53 @@ <h2 :class="version === ver.version ? 'primary--text' : undefined">v{{ver.versio
15061533
if (newval === '/coding') {
15071534
require.config({ paths: { vs: 'https://cdn.jsdelivr.net/npm/monaco-editor@0.20.0/min/vs' } })
15081535
require(['vs/editor/editor.main'], () => {
1536+
const envjs_demo = [
1537+
'/** ',
1538+
' * 注意: ',
1539+
' * 在这里你可以使用完整的 EnvJs 环境',
1540+
' * ',
1541+
' * 同时: ',
1542+
' * 你`必须`手动调用 $done()',
1543+
' * ',
1544+
' * 因为: ',
1545+
' * BoxJs 不为主动执行的脚本调用 $done()',
1546+
' * 而把 $done 的时机完全交由脚本控制',
1547+
' * ',
1548+
' * 最后: ',
1549+
' * 这段脚本是可以直接运行的!',
1550+
' */ ',
1551+
'const host = $.getdata("boxjs_host")',
1552+
'$.json = {',
1553+
' msg: "这段 json 是返回给浏览器的!"',
1554+
'}',
1555+
'$.msg($.name, host)',
1556+
'$.done()',
1557+
'// $done() 或 $.done() 都可以'
1558+
]
1559+
const surgejs_demo = [
1560+
'/** ',
1561+
' * 注意: ',
1562+
' * 你正在使用 Surge HTTP-API 环境',
1563+
' * ',
1564+
' * 在这里:你不可以使用 EnvJs',
1565+
' * 请确保:你的脚本能被 Surge 独立运行',
1566+
' * ',
1567+
' * 最后: ',
1568+
' * 这段脚本是可以直接运行的!',
1569+
' */ ',
1570+
'const host = $persistentStore.read("boxjs_host")',
1571+
'const msgs = [""]',
1572+
'',
1573+
'msgs.push("这是日志的内容")',
1574+
'msgs.push("BoxJs host: " + host)',
1575+
'',
1576+
'console.log(msgs.join("\\n"))',
1577+
'$done()'
1578+
]
15091579
this.ui.editor = monaco.editor.create(document.getElementById('container'), {
15101580
fontSize: 12,
15111581
tabSize: 2,
1512-
value: [
1513-
'/** ',
1514-
' * 注意: ',
1515-
' * 在这里你可以使用完整的 EnvJs 环境',
1516-
' * ',
1517-
' * 同时: ',
1518-
' * 你`必须`手动调用 $done()',
1519-
' * ',
1520-
' * 因为: ',
1521-
' * BoxJs 不为主动执行的脚本调用 $done()',
1522-
' * 而把 $done 的时机完全交由脚本控制',
1523-
' * ',
1524-
' * 最后: ',
1525-
' * 这段脚本是可以直接运行的!',
1526-
' */ ',
1527-
'const host = $.getdata("boxjs_host")',
1528-
'$.json = {',
1529-
' msg: "这段 json 是返回给浏览器的!"',
1530-
'}',
1531-
'$.msg($.name, host)',
1532-
'$.done()',
1533-
'// $done() 或 $.done() 都可以'
1534-
].join('\n'),
1582+
value: this.box.usercfgs.httpapi ? surgejs_demo.join('\n') : envjs_demo.join('\n'),
15351583
language: 'javascript',
15361584
minimap: { enabled: false },
15371585
theme: this.isDarkMode ? 'vs-dark' : 'vs'
@@ -1792,7 +1840,12 @@ <h2 :class="version === ver.version ? 'primary--text' : undefined">v{{ver.versio
17921840
exeScript() {
17931841
const script = this.ui.editor.getValue()
17941842
const opts = { script }
1795-
axios.post('/api/runScript', opts)
1843+
axios.post('/api/runScript', opts).then((resp) => {
1844+
if (this.box.usercfgs.httpapi) {
1845+
this.ui.exeScriptSheet.resp = JSON.parse(resp.data)
1846+
this.ui.exeScriptSheet.show = true
1847+
}
1848+
})
17961849
},
17971850
// 保存用户偏好
17981851
saveUserCfgs() {

‎box/chavy.boxjs.js‎

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

3-
$.version = '0.7.28'
3+
$.version = '0.7.29'
44
$.versionType = 'beta'
55

66
/**
@@ -37,7 +37,6 @@ $.html = $.name // `页面`类请求的响应体
3737

3838
// 页面源码地址
3939
$.web = `https://cdn.jsdelivr.net/gh/chavyleung/scripts@${$.version}/box/chavy.boxjs.html?_=${new Date().getTime()}`
40-
4140
// 版本说明地址 (Release Note)
4241
$.ver = 'https://gitee.com/chavyleung/scripts/raw/master/box/release/box.release.tf.json'
4342

@@ -524,9 +523,14 @@ async function apiRunScript() {
524523
}
525524
} else {
526525
// 对于手动执行的脚本, 把 $done 的时机交给脚本自主控制
527-
$.isSkipDone = true
528-
$request = undefined
529-
eval(opts.script)
526+
if ($.isSurge() && ishttpapi) {
527+
const runOpts = { timeout: opts.timeout }
528+
await $.runScript(opts.script, runOpts).then((resp) => ($.json = resp))
529+
} else {
530+
$.isSkipDone = true
531+
$request = undefined
532+
eval(opts.script)
533+
}
530534
}
531535
}
532536

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
{
22
"releases": [
3+
{
4+
"version": "0.7.29",
5+
"tags": ["beta"],
6+
"author": "@chavyleung",
7+
"icon": "https://avatars3.githubusercontent.com/u/29748519",
8+
"repo": "https://github.com/chavyleung",
9+
"notes": [
10+
{
11+
"name": "代码编辑器",
12+
"descs": ["Surge HTTP-API 环境下执行的代码能显示响应内容"]
13+
}
14+
]
15+
},
316
{
417
"version": "0.7.28",
518
"tags": ["beta"],

0 commit comments

Comments
 (0)