@fish-under-sea/magpie-dragonapi 1.0.3 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/service.mjs CHANGED
@@ -49,6 +49,16 @@ const PKG_VERSION = (() => {
49
49
  try { return JSON.parse(readFileSync(join(here, 'package.json'), 'utf8')).version || '0.0.0' } catch { return '0.0.0' }
50
50
  })()
51
51
 
52
+ /** UI 文件的最新修改时间:页面用它做热重载(本地改样式/脚本后自动刷新)。 */
53
+ const UI_FILES = ['ui/index.html', 'ui/app.js', 'ui/style.css']
54
+ function uiStamp() {
55
+ let newest = 0
56
+ for (const rel of UI_FILES) {
57
+ try { newest = Math.max(newest, Math.round(statSync(join(here, rel)).mtimeMs)) } catch { /* 文件缺失就跳过 */ }
58
+ }
59
+ return newest
60
+ }
61
+
52
62
  /** MIME 表只覆盖本插件真正会发的类型。 */
53
63
  const MIME = {
54
64
  '.html': 'text/html; charset=utf-8',
@@ -262,7 +272,7 @@ export async function startService({ port = 3517, dir = '.', site, catalog, stat
262
272
  if (path === '/api/ping' && req.method === 'GET') {
263
273
  // 版本从 package.json 读,别硬编码:硬编码的版本号会在升级后说谎,
264
274
  // 排查时容易把「旧代码还在跑」误判成「新版没修」。
265
- json(res, 200, { ok: true, version: PKG_VERSION, port })
275
+ json(res, 200, { ok: true, version: PKG_VERSION, ui: uiStamp(), port })
266
276
  return
267
277
  }
268
278
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fish-under-sea/magpie-dragonapi",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "description": "DragonAPI for magpie: a provider plugin whose models, groups and prices come from the site's public /api/pricing, with balance and per-call usage from an sk- key, plus a local page that lists the market (read-only), shows usage and can mint new keys for a group.",
5
5
  "type": "module",
6
6
  "main": "index.mjs",
package/ui/app.js CHANGED
@@ -3688,7 +3688,16 @@ async function onClearPat(btn) {
3688
3688
  }
3689
3689
 
3690
3690
  function bindPatControls() {
3691
- $('#pat-form').addEventListener('submit', onSavePat);
3691
+ // 不能挂 form 的 submit:magpie 的插件页是 sandbox="allow-scripts"(没有
3692
+ // allow-forms),原生表单提交会被浏览器在触发 submit 事件之前就拦下,
3693
+ // 表现是点「保存」毫无反应、连「已提交」都不出现。所以保存走 click;
3694
+ // 输入框里的回车也自己处理,避免隐式提交。
3695
+ $('#pat-save').addEventListener('click', onSavePat);
3696
+ $('#pat-input').addEventListener('keydown', (ev) => {
3697
+ if (ev.key !== 'Enter' || ev.isComposing) return;
3698
+ ev.preventDefault();
3699
+ void onSavePat(ev);
3700
+ });
3692
3701
  $('#pat-clear').addEventListener('click', (ev) => onClearPat(ev.currentTarget));
3693
3702
  }
3694
3703
 
@@ -4226,6 +4235,34 @@ function bindGlobal() {
4226
4235
  });
4227
4236
  }
4228
4237
 
4238
+ /**
4239
+ * startHotReload:监听服务端版本 / UI 文件时间戳,变化就整页重载。
4240
+ * 本地改 ui/* 后不用手动刷新;插件升级到新版本时也会自动换到新界面。
4241
+ */
4242
+ function startHotReload() {
4243
+ const every = 5000
4244
+ let baseline = null
4245
+ let reloading = false
4246
+ const check = async () => {
4247
+ if (reloading) return
4248
+ try {
4249
+ const res = await fetch(apiUrl('/api/ping'), { cache: 'no-store', credentials: 'omit' })
4250
+ if (!res.ok) return
4251
+ const info = await res.json()
4252
+ if (!info || info.ok !== true) return
4253
+ const stamp = { version: String(info.version || ''), ui: Number(info.ui) || 0 }
4254
+ if (!baseline) { baseline = stamp; return }
4255
+ if (stamp.version !== baseline.version || stamp.ui > baseline.ui) {
4256
+ reloading = true
4257
+ banner('info', '检测到插件更新,正在热重载…', { key: 'hot-reload' })
4258
+ setTimeout(() => location.reload(), 600)
4259
+ }
4260
+ } catch { /* 服务重启中,下一次再试 */ }
4261
+ }
4262
+ check()
4263
+ setInterval(check, every)
4264
+ }
4265
+
4229
4266
  async function boot() {
4230
4267
  initTheme();
4231
4268
  bindGlobal();
@@ -4245,7 +4282,7 @@ async function boot() {
4245
4282
  const wanted = params.get('tab');
4246
4283
  // 密钥已并入账户页;旧的 ?tab=keys 链接仍要能打开(映射到 wallet),
4247
4284
  // 不能因为标签被取消就让人书签失效。
4248
- const valid = ['wallet', 'catalog', 'logs', 'pat'];
4285
+ const valid = ['wallet', 'dashboard', 'catalog', 'logs', 'pat'];
4249
4286
  // 账户页放在首位,也作为默认落地页:打开就能看到余额与钥匙。
4250
4287
  const start = wanted === 'keys' ? 'wallet' : (valid.includes(wanted) ? wanted : 'wallet');
4251
4288
 
@@ -4255,7 +4292,7 @@ async function boot() {
4255
4292
  await Promise.allSettled([loadPing(false), loadState(false), ensureTab(start, false)]);
4256
4293
  }
4257
4294
 
4258
- boot().catch((err) => {
4295
+ boot().then(() => startHotReload()).catch((err) => {
4259
4296
  // 兜底:boot 里任何没接住的异常都不能变成白屏
4260
4297
  banner('error', `页面初始化失败:${describeError(err)}。可以点右上角「刷新」重试。`, { key: 'boot' });
4261
4298
  });
package/ui/index.html CHANGED
@@ -353,7 +353,7 @@
353
353
  placeholder="nap_xxxxxxxxxxxxxxxx">
354
354
  </div>
355
355
  <div class="form-actions">
356
- <button class="btn primary" id="pat-save" type="submit">保存</button>
356
+ <button class="btn primary" id="pat-save" type="button">保存</button>
357
357
  <button class="btn ghost" id="pat-clear" type="button">清除 PAT</button>
358
358
  </div>
359
359
  <p class="hint">PAT 只存在插件本地服务里,页面不保存任何凭据。</p>