dsh-whale-widget 0.3.3 → 0.3.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.
@@ -671,6 +671,11 @@ var css = [
671
671
  ].join('\n')
672
672
 
673
673
  var styleEl = document.createElement('style')
674
+ // PR #114:不带 data-plugin 的 <style> 会被 DSH 客户端模块系统 claimStyles 认领到
675
+ // 「当前正在物化的那个插件」名下,之后该插件热重载/失效时 removeOwnedStyles 会把它
676
+ // 一起删掉。样式一没,挂件 20 多个 dshwv-* 节点就从 position:fixed 掉回文档流堆在
677
+ // 页面底部(页面被撑到几千像素高)。打上自己的名字后就不会被任何人认领/删除。
678
+ styleEl.setAttribute('data-plugin', 'dsh-whale-widget')
674
679
  styleEl.textContent = css
675
680
  document.head.appendChild(styleEl)
676
681
 
@@ -1916,6 +1921,9 @@ function usageAlertBudgetEditor(key, onSave) {
1916
1921
  // 提醒编辑期间:窗口内可能弹出的各类全屏遮罩(确认/裁剪/音频/快照/用量)统一置顶,杜绝层级错位
1917
1922
  var remindZStyle = document.createElement('style')
1918
1923
  remindZStyle.id = 'dshw-remind-overlay-z'
1924
+ // 同上(PR #114):本文本表同样必须自带 data-plugin,否则会被别的客户端插件
1925
+ // 热重载时顺带删掉,提醒编辑期的遮罩层级就失效了。
1926
+ remindZStyle.setAttribute('data-plugin', 'dsh-whale-widget')
1919
1927
  // 提醒编辑期间:窗口内可能弹出的全屏遮罩(确认/裁剪/音频/快照)统一置顶,杜绝层级错位。
1920
1928
  // 注意:不要把 .dshwv-usage-mask 放进来——「模型子菜单/模型设置」用的是这个类(29000),
1921
1929
  // 一提权就会反盖到提醒编辑器(30000)上面。
@@ -12470,10 +12478,28 @@ function setScrollGapPx(v) {
12470
12478
  saveConfig()
12471
12479
  settle()
12472
12480
  }
12481
+ // issue #91 缺陷2:触屏设备上没有任何进菜单的路径 —— ☰ 按钮默认 opacity:0,只由
12482
+ // pointermove 命中鲸鱼时才加 dshwv-menu-btn-visible,而触摸端没有 hover;长按唤出又只在
12483
+ // 「隐藏菜单按钮」开启时挂计时(默认关闭)。这里判定「主输入是否为无 hover 的触摸」:
12484
+ // 只用 (hover: none),或 (pointer: coarse) + 有触点。触屏笔记本(主输入是鼠标)不受影响。
12485
+ function dshwvTouchUI() {
12486
+ try {
12487
+ if (typeof window !== 'undefined' && window.matchMedia) {
12488
+ if (window.matchMedia('(hover: none)').matches) return true
12489
+ if (window.matchMedia('(pointer: coarse)').matches && (navigator.maxTouchPoints || 0) > 0) return true
12490
+ }
12491
+ if (typeof window !== 'undefined' && typeof navigator !== 'undefined') {
12492
+ return (navigator.maxTouchPoints || 0) > 0 && ('ontouchstart' in window)
12493
+ }
12494
+ } catch (err) {}
12495
+ return false
12496
+ }
12473
12497
  function applyMenuBtnHideUI() {
12474
12498
  try {
12475
12499
  menuBtn.classList.toggle('dshwv-menu-btn-hidden', menuBtnHide)
12476
12500
  if (menuBtnHide) menuBtn.classList.remove('dshwv-menu-btn-visible')
12501
+ // 触屏:没有 hover 可以显形 → 常显(仍受上面的 hidden 开关控制)
12502
+ else if (dshwvTouchUI()) menuBtn.classList.add('dshwv-menu-btn-visible')
12477
12503
  } catch (err) {}
12478
12504
  }
12479
12505
  function setMenuBtnHide(v) {
@@ -14454,8 +14480,10 @@ function onDocTouchStart(e) {
14454
14480
  touchDrag = { id: t.identifier }
14455
14481
  touchStartPt = { x: t.clientX, y: t.clientY }
14456
14482
  touchNowPt = { x: t.clientX, y: t.clientY }
14457
- // 长按唤出菜单:仅「隐藏菜单按钮」开启且菜单未打开时挂计时(位移超标即取消)
14458
- if (menuBtnHide && !menuOpen && (Date.now() - menuClosedAt > 600)) {
14483
+ // 长按唤出菜单:原本只在「隐藏菜单按钮」开启且菜单未打开时挂计时(位移超标即取消)
14484
+ // 触屏上默认配置(按钮永远不显形)就完全没有进菜单的路径 hover 的设备一律允许长按
14485
+ // 唤出(issue #91 缺陷2);桌面端行为不变。
14486
+ if ((menuBtnHide || dshwvTouchUI()) && !menuOpen && (Date.now() - menuClosedAt > 600)) {
14459
14487
  cancelTouchLongPress()
14460
14488
  touchLongPressTimer = setTimeout(fireTouchLongPressMenu, TOUCH_LONG_PRESS_MS)
14461
14489
  }
@@ -14511,9 +14539,14 @@ function onDocPointerMoveCursor(e) {
14511
14539
  }
14512
14540
  var over = isWhaleHit(e)
14513
14541
  setWidgetCursor(over ? 'grab' : '')
14514
- if (!menuBtnHide) menuBtn.classList.toggle('dshwv-menu-btn-visible', over || menuOpen)
14542
+ // 触屏上必须把 dshwvTouchUI() 也当作"该显示":拖动鲸鱼时 pointermove 的 over false,
14543
+ // 否则这一次 toggle 会把常显状态撤掉(issue #91 缺陷2 修完又被自己抹掉)。
14544
+ if (!menuBtnHide) menuBtn.classList.toggle('dshwv-menu-btn-visible', over || menuOpen || dshwvTouchUI())
14515
14545
  }
14516
14546
  document.addEventListener('pointermove', onDocPointerMoveCursor, true)
14547
+ // 启动即应用一次菜单按钮可见性:触屏上 ☰ 常显(issue #91 缺陷2)。
14548
+ // 配置读回来之后还会再应用一次,这里是配置请求失败时的兜底。
14549
+ try { applyMenuBtnHideUI() } catch (err) {}
14517
14550
 
14518
14551
  function endDrag(e, clickAllowed) {
14519
14552
  if (!drag || !drag.active) return
@@ -14693,8 +14726,10 @@ fetch(SIZE_URL, { cache: 'no-store' })
14693
14726
  if (d && typeof d.menuBtnHide === 'boolean') {
14694
14727
  menuBtnHide = d.menuBtnHide
14695
14728
  if (menuHideToggle) menuHideToggle.checked = menuBtnHide
14696
- applyMenuBtnHideUI()
14697
14729
  }
14730
+ // 无论服务端带没带这个键都要应用一次:触屏上 ☰ 需要常显(issue #91 缺陷2),
14731
+ // 而旧写法只在键存在时才调用,空配置下按钮永远是透明的。
14732
+ applyMenuBtnHideUI()
14698
14733
  // 相对边框恢复(localStorage 锚点):窗口变化后保持离边距离。
14699
14734
  // 仅认 v:2 净距离格式;旧格式(含避让距离)废弃,挂件保持默认右下角吸附。
14700
14735
  // issue #102:这里原与 applyAnchorPos() 各写了一份恢复逻辑(两份都只做下限夹紧),
package/lib/index.js CHANGED
@@ -929,7 +929,7 @@ export default {
929
929
  const days = new Set([today, ...Object.keys(led.history || {}), ...accountingDays(led)])
930
930
  for (const e of events) if (/^\d{4}-\d{2}-\d{2}$/.test(e.day)) days.add(e.day)
931
931
  return {
932
- ok: true, version: '0.3.3', today: todayData, days7,
932
+ ok: true, version: '0.3.4', today: todayData, days7,
933
933
  total7: total7ByCurrency[todayData.currency] || 0, total7Currency: todayData.currency, total7ByCurrency,
934
934
  all: {
935
935
  days: Array.from(days).filter(d => /^\d{4}-\d{2}-\d{2}$/.test(d)).sort().reverse().map(forDay),
@@ -1273,9 +1273,19 @@ export default {
1273
1273
  // 后台预热:codexSummary 首次要同步读全部 rollout 日志(可能几十 MB),
1274
1274
  // 放在请求路径里会阻塞事件循环。这里在插件启动后预热一次,并每 5 分钟刷新一次缓存,
1275
1275
  // 请求进来时通常直接命中 codexSummaryCached 的 5 秒记忆。
1276
+ let codexPrewarmT = null, codexPrewarmI = null
1276
1277
  try {
1277
- setTimeout(function () { try { codexSummaryCached() } catch (err) {} }, 1500)
1278
- setInterval(function () { try { codexSummaryCached() } catch (err) {} }, 5 * 60 * 1000)
1278
+ codexPrewarmT = setTimeout(function () { try { codexSummaryCached() } catch (err) {} }, 1500)
1279
+ codexPrewarmI = setInterval(function () { try { codexSummaryCached() } catch (err) {} }, 5 * 60 * 1000)
1280
+ // issue #109:这两个定时器原先不存句柄、也不进 disposers,插件树 dispose 之后
1281
+ // 事件循环里仍有一个被引用的 interval → 用户退出 dsh 时进程不结束(只能 Ctrl+C)。
1282
+ // 存句柄并纳入 disposers,卸载时一并清掉;unref 作为兜底(正常运行时由 HTTP 服务维持事件循环)。
1283
+ try { if (codexPrewarmT && codexPrewarmT.unref) codexPrewarmT.unref() } catch (err) {}
1284
+ try { if (codexPrewarmI && codexPrewarmI.unref) codexPrewarmI.unref() } catch (err) {}
1285
+ disposers.push(function () {
1286
+ if (codexPrewarmT !== null) { clearTimeout(codexPrewarmT); codexPrewarmT = null }
1287
+ if (codexPrewarmI !== null) { clearInterval(codexPrewarmI); codexPrewarmI = null }
1288
+ })
1279
1289
  } catch (err) {}
1280
1290
  // ===== 自定义 API 模型注册表(v655)=====
1281
1291
  // 文件:$DSH_HOME/.dshw-api.json → { version, models:[…], usage:{ [id]:{day,dayStart,lastBalance,delta,eventCost} } }
@@ -1361,12 +1371,34 @@ export default {
1361
1371
  }
1362
1372
  return out
1363
1373
  }
1374
+ // PR #104(收窄版):只放行 http/https,并挡掉云厂商元数据与 link-local 地址。
1375
+ // 关键取舍:**不拦回环与内网** —— 我们内置的 ollama 模板就推荐用户填
1376
+ // http://127.0.0.1:11434/v1(本地 Ollama / LM Studio),自建网关(New API 之类,
1377
+ // 见 issue #53)也跑在本机;拦掉它们会把正当用法一起打死。接口地址由用户自己在面板/
1378
+ // 注册表里填写,且所有 /dsh-whale/* 路由都在受信任栅栏之后 —— 真正要挡的是
1379
+ // 「被诱导去读云元数据把凭据带出来」,而不是「访问本机服务」。
1380
+ // decimal / octal / hex 形式的 IPv4 已由 WHATWG URL 规范化成点分十进制,这里只按规范化结果判定。
1381
+ function assertSafeApiUrl(u) {
1382
+ let parsed
1383
+ try { parsed = new URL(u) } catch { throw new Error('无效的接口地址') }
1384
+ if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') throw new Error('不支持的协议: ' + parsed.protocol)
1385
+ const host = parsed.hostname.toLowerCase().replace(/^\[|\]$/g, '')
1386
+ if (host === 'metadata.google.internal' || host === 'metadata.goog' || host === '100.100.100.200') throw new Error('禁止访问云元数据地址')
1387
+ const m = host.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/)
1388
+ if (m) {
1389
+ const [a, b] = [Number(m[1]), Number(m[2])]
1390
+ if (a === 169 && b === 254) throw new Error('禁止访问 link-local 地址')
1391
+ if (a === 0) throw new Error('禁止访问该地址')
1392
+ }
1393
+ if (/^fe[89ab][0-9a-f]:/i.test(host)) throw new Error('禁止访问 link-local 地址')
1394
+ }
1364
1395
  async function apiFetchJson(url, auth, key) {
1365
1396
  const headers = {}
1366
1397
  const a = String(auth == null ? 'Bearer {key}' : auth)
1367
1398
  if (a) headers.Authorization = a.replace('{key}', key)
1368
1399
  // v724:URL 里也支持 {key}(如 Gemini 的 ?key=… 形式;auth 传空串时不带 Authorization 头)
1369
1400
  const u = String(url).replace('{key}', key)
1401
+ assertSafeApiUrl(u)
1370
1402
  const res = await fetch(u, { headers, signal: AbortSignal.timeout(15000) })
1371
1403
  if (!res.ok) throw new Error('HTTP ' + res.status)
1372
1404
  return await res.json()
@@ -1973,7 +2005,7 @@ export default {
1973
2005
  const led = readUsageLedger()
1974
2006
  const summary = daySummary(led, todayKey())
1975
2007
  return {
1976
- ...visible, version: '0.3.3', isPeak: isPeakTime(Math.floor(Date.now() / 1000)),
2008
+ ...visible, version: '0.3.4', isPeak: isPeakTime(Math.floor(Date.now() / 1000)),
1977
2009
  todayUsage: summary.amount, todayUsageCurrency: summary.currency,
1978
2010
  usageSource: summary.source, usageLabel: summary.label, usageMode: 'ledger',
1979
2011
  accounting: balanceSummary(led, todayKey()),
@@ -2604,7 +2636,7 @@ export default {
2604
2636
  const r = apiDeleteModel(parsed.id)
2605
2637
  const list = await apiModelsPayload()
2606
2638
  res.writeHead(r.ok ? 200 : 400, JSON_HEADERS)
2607
- res.end(JSON.stringify(Object.assign({}, r, { models: list.models })))
2639
+ res.end(JSON.stringify({ ...r, models: list.models }))
2608
2640
  return
2609
2641
  }
2610
2642
  if (action === 'probe') {
@@ -2640,10 +2672,10 @@ export default {
2640
2672
  }
2641
2673
  // 注意:keyValue 在提交体上(不在 model 里),必须合并进去,
2642
2674
  // 否则前端面板里填的 API key 会被丢弃(密钥永远存不进去)
2643
- const r = await apiSaveModel(Object.assign({}, parsed.model || parsed, { keyValue: parsed.keyValue }))
2675
+ const r = await apiSaveModel({ ...(parsed.model || parsed), keyValue: parsed.keyValue })
2644
2676
  const list2 = await apiModelsPayload()
2645
2677
  res.writeHead(r.ok ? 200 : 400, JSON_HEADERS)
2646
- res.end(JSON.stringify(Object.assign({}, r, { models: list2.models })))
2678
+ res.end(JSON.stringify({ ...r, models: list2.models }))
2647
2679
  return
2648
2680
  }
2649
2681
  const payload = await apiModelsPayload()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-whale-widget",
3
- "version": "0.3.3",
3
+ "version": "0.3.4",
4
4
  "description": "DSH Web 界面右下角的 DeepSeek 余额小鲸鱼挂件:余额/今日已用/峰谷定价、自定义泡泡点击序列(文本/余额/今日/峰谷/图片/随机语句与并列加权选择)、逐行样式与字体、悬浮快捷编辑、音效与每轮消耗、自定义角色/动图/音效、吸附与翻转自定义",
5
5
  "keywords": [
6
6
  "dsh",