From 2c83c151702611ab1c168cec16a449eafb0259f2 Mon Sep 17 00:00:00 2001 From: yoimiya-kokomi <592981798@qq.com> Date: Sun, 18 Sep 2022 12:05:37 +0800 Subject: [PATCH] =?UTF-8?q?V3=E6=88=B3=E4=B8=80=E6=88=B3=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E7=A7=81=E8=81=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/App.js | 2 +- components/common-lib/render.js | 3 ++ tools/web.js | 74 +++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 tools/web.js diff --git a/components/App.js b/components/App.js index 473cf89c..f9a67df0 100644 --- a/components/App.js +++ b/components/App.js @@ -26,7 +26,7 @@ class App { super({ name: `喵喵:${cfg.name || cfg.id}`, dsc: cfg.desc || cfg.name || '喵喵插件', - event: event === 'poke' ? 'notice.group.poke' : 'message', + event: event === 'poke' ? 'notice.*.poke' : 'message', priority: 50, rule: rules }) diff --git a/components/common-lib/render.js b/components/common-lib/render.js index 70284de8..1ca726b0 100644 --- a/components/common-lib/render.js +++ b/components/common-lib/render.js @@ -23,6 +23,9 @@ export default async function (path, params, cfg) { _tpl_path: process.cwd() + '/plugins/miao-plugin/resources/common/tpl/', defaultLayout: layoutPath + 'default.html', elemLayout: layoutPath + 'elem.html', + pageGotoParams: { + waitUntil: 'networkidle0' + }, sys: { scale: Cfg.scale(cfg.scale || 1), copyright: `Created By Yunzai-Bot${Version.yunzai} & Miao-Plugin${Version.version}` diff --git a/tools/web.js b/tools/web.js new file mode 100644 index 00000000..072d8813 --- /dev/null +++ b/tools/web.js @@ -0,0 +1,74 @@ +import express from 'express' +import template from 'express-art-template' +import fs from 'fs' +import lodash from 'lodash' + +/* +* npm run app web-debug开启Bot后 +* 可另外通过 npm run web 开启浏览器调试 +* 访问 http://localhost:8000/ 即可看到对应页面 +* 页面内的资源需使用 {{_res_path}}来作为resources目录的根目录 +* 可编辑模板与页面查看效果 +* todo: 预览页面的热更 +* +* */ + +let app = express() + +let _path = process.cwd() + +app.engine('html', template) +app.set('views', _path + '/resources/') +app.set('view engine', 'art') +app.use(express.static(_path + '/resources')) +app.use('/plugins', express.static('plugins')) + +app.get('/', function (req, res) { + let fileList = fs.readdirSync(_path + '/data/ViewData/') || [] + console.log(fileList) + let html = [ + '在npm run web-dev模式下触发截图消息后,可在下方选择页面进行调试', + '如果页面内资源路径不正确请使用{{_res_path}}作为根路径,对应之前的../../../../', + '可直接修改模板html或css刷新查看效果' + ] + let li = {} + for (let idx in fileList) { + let ret = /(.+)\.json$/.exec(fileList[idx]) + if (ret && ret[1]) { + let data = JSON.parse(fs.readFileSync(_path + '/data/ViewData/' + ret[1] + '.json', 'utf8')) + let text = [(data._app || 'genshin'), ret[1]] + if (data._plugin) { + text.unshift(data._plugin) + } + li[text.join('')] = (`
  • ${text.join(' / ')}
  • `) + } + } + res.send(html.join('
    ') + '') +}) + +app.get('/:type', function (req, res) { + let page = req.params.type + if (page == 'favicon.ico') { + return res.send('') + } + let data = JSON.parse(fs.readFileSync(_path + '/data/ViewData/' + page + '.json', 'utf8')) + data = data || {} + data._res_path = '' + data._sys_res_path = data._res_path + + let app = data._app || 'genshin' + if (data._plugin) { + console.log(data._plugin) + data._res_path = `/plugins/${data._plugin}/resources/` + } + let tplPath = `${app}/${page}/${page}.html` + if (data._plugin) { + tplPath = `../plugins/${data._plugin}/resources/${app}/${page}.html` + } else if (data._no_type_path) { + tplPath = `${app}/${page}.html` + } + res.render(tplPath, data) +}) + +app.listen(8000) +console.log('页面服务已启动,触发消息图片后访问 http://localhost:8000/ 调试页面')