dsh-muv-table 0.2.8 → 0.2.9

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.
Files changed (2) hide show
  1. package/lib/client.js +44 -4
  2. package/package.json +1 -1
package/lib/client.js CHANGED
@@ -167,6 +167,37 @@ window.__ModuleLoader__.load({
167
167
  // panel polls, and the user can switch presets at any time. An earlier
168
168
  // revision cached the first answer and never re-checked, which pinned the
169
169
  // panel to whatever preset happened to be active when it opened.
170
+ // 当前 DSH 会话 id —— 这才是「我在哪个会话」的权威依据。
171
+ //
172
+ // 起因:用户报告「不管点哪个会话,面板永远显示同一张卡」。查下来服务端没问题
173
+ // (按 presetId 请求,每个预设都返回各自的卡),是客户端一直在发同一个预设。
174
+ // 根因是过度依赖酒馆的 DOM:`#tavern-session-preset-label` 的 dataset.presetId
175
+ // 在切换会话时不一定跟着更新(酒馆自己在 bundle.js:1655 就是
176
+ // `savedPid || lbl.dataset.presetId` —— savedPid 为空时会**保留旧值**),
177
+ // 而 localStorage 那份更是切换预设时才写。
178
+ // 会话 id 则不会撒谎:服务端按它查 session-bindings.json,直接得到该会话绑的预设。
179
+ const currentSessionId = () => {
180
+ try {
181
+ const svc = window.__DSH_TAVERN_SESSIONS__
182
+ if (svc && svc.list && typeof svc.list.getSnapshot === 'function') {
183
+ const snap = svc.list.getSnapshot()
184
+ const cur = snap && snap.current
185
+ if (cur && /^(session-)?[a-f0-9-]{20,}$/i.test(String(cur))) {
186
+ return 'session-' + String(cur).replace(/^session-/, '')
187
+ }
188
+ }
189
+ } catch (_) {}
190
+ try {
191
+ const m = location.href.match(/session[/=:-]([a-f0-9-]{20,})/i)
192
+ if (m && m[1]) return 'session-' + m[1].replace(/^session-/, '')
193
+ } catch (_) {}
194
+ try {
195
+ const cached = document.documentElement.getAttribute('data-dsh-current-session')
196
+ if (cached) return cached
197
+ } catch (_) {}
198
+ return ''
199
+ }
200
+
170
201
  const readActivePresetFromDom = () => {
171
202
  try {
172
203
  for (const id of ['tavern-session-preset-label', 'tavern-session-preset-btn']) {
@@ -181,12 +212,16 @@ window.__ModuleLoader__.load({
181
212
  try { return localStorage.getItem('dsh-tavern-active-preset') || '' } catch (_) { return '' }
182
213
  }
183
214
 
215
+ // 本次请求要带的定位参数。会话 id 优先——它随会话切换而变,是唯一不会「粘住」的依据。
216
+ let lastSessionId = ''
184
217
  const resolveActivePreset = async () => {
185
- // Tavern's DOM is authoritative and updates the moment the user switches.
218
+ lastSessionId = currentSessionId()
219
+ // 有会话 id 就不必猜:服务端会按 session-bindings.json 查出该会话绑的预设。
220
+ if (lastSessionId) return lastPresetId
221
+ // 拿不到会话 id 时才退回酒馆 DOM;DOM 元素在酒馆 UI 之外不存在。
186
222
  const fromDom = readActivePresetFromDom()
187
223
  if (fromDom) { lastPresetId = fromDom; return lastPresetId }
188
- // The DOM nodes are absent outside the tavern UI; ask the server, which
189
- // resolves the preset from the most recently active session.
224
+ // 最后才让服务端按「最近写入的会话」猜。
190
225
  try {
191
226
  const r = await fetch('/api/muv-table/active-preset')
192
227
  const d = await r.json()
@@ -198,7 +233,12 @@ window.__ModuleLoader__.load({
198
233
  const fetchTavernCard = async () => {
199
234
  await resolveActivePreset()
200
235
  let url = '/api/muv-table/tavern-card'
201
- if (lastPresetId) url += '?presetId=' + encodeURIComponent(lastPresetId)
236
+ // 只带一个定位参数,且**会话 id 优先**:服务端的判定顺序是
237
+ // `if (presetId) … else if (sessionId) …`,两个都传时 presetId 会盖掉会话——
238
+ // 而 presetId 恰恰是可能「粘住」的那个。会话 id 由服务端查 session-bindings.json,
239
+ // 切换会话必然跟着变,这正是「不管点哪个会话都显示同一张卡」的解药。
240
+ if (lastSessionId) url += '?sessionId=' + encodeURIComponent(lastSessionId)
241
+ else if (lastPresetId) url += '?presetId=' + encodeURIComponent(lastPresetId)
202
242
  const r = await fetch(url)
203
243
  return r.json()
204
244
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-muv-table",
3
- "version": "0.2.8",
3
+ "version": "0.2.9",
4
4
  "description": "MUV 变量表格编辑器 —— 树形变量面板、宏测试浮窗(random/pick/roll)、骰子快捷栏、pick 缓存管理;支持直接读取 SillyTavern PNG 角色卡(含正则脚本、世界书、变量表)。dsh-muv-engine 的伴生插件",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",