@fish-under-sea/magpie-neartoken 0.2.2 → 0.2.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/index.mjs CHANGED
@@ -181,6 +181,33 @@ async function catalog(disabled = new Set()) {
181
181
  * 所以这里不推数据、只落一个文件:`<script src="groups.js?t=…">` 一引就有。
182
182
  * 写不进去(插件目录只读)就跳过,页面退回它自己的说明。
183
183
  */
184
+ /**
185
+ * stampLatest 把 npm 上的最新版本写进 page/latest.js。
186
+ *
187
+ * 页面自己查不了(沙箱禁联网),但插件能:这里顺手问一次 registry,页面
188
+ * 「检查更新」时加载这个脚本就知道新不新。一分钟内不重复问。
189
+ */
190
+ let latestAskedAt = 0
191
+ async function stampLatest() {
192
+ if (Date.now() - latestAskedAt < 60_000) return
193
+ latestAskedAt = Date.now()
194
+ try {
195
+ const res = await fetch('https://registry.npmjs.org/@fish-under-sea%2Fmagpie-neartoken/latest', {
196
+ headers: { accept: 'application/json' },
197
+ })
198
+ if (!res.ok) return
199
+ const body = await res.json()
200
+ const v = typeof body?.version === 'string' ? body.version.trim() : ''
201
+ if (!v) return
202
+ writeFileSync(
203
+ join(here, 'page', 'latest.js'),
204
+ 'self.NEARTOKEN_LATEST = ' + JSON.stringify(v) + '\nself.NEARTOKEN_LATEST_AT = ' + Date.now() + '\n',
205
+ 'utf8',
206
+ )
207
+ } catch {
208
+ /* 问不到就保持原样:页面会说「没查到」 */
209
+ }
210
+ }
184
211
  function writeGroups(groups) {
185
212
  try {
186
213
  const js = 'self.NEARTOKEN_GROUPS = ' + JSON.stringify(groups) + '\nself.NEARTOKEN_GROUPS_AT = ' + Date.now() + '\n'
@@ -272,6 +299,7 @@ export default async function neartoken(input, options) {
272
299
  ],
273
300
  /** 余额:站点钱包端点,按 magpie 的用量卡显示。 */
274
301
  async usage(getAuth) {
302
+ await stampLatest()
275
303
  const auth = await getAuth()
276
304
  const key = auth?.access || auth?.key
277
305
  if (!key) throw new Error('还没有连接 NearToken')
@@ -322,6 +350,7 @@ export default async function neartoken(input, options) {
322
350
  * @returns {Promise<Record<string, object>>}
323
351
  */
324
352
  async models() {
353
+ await stampLatest()
325
354
  return catalog(disabled)
326
355
  },
327
356
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fish-under-sea/magpie-neartoken",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "description": "NearToken for magpie: a provider whose sign-in is the site's own device authorization, models listed per billing group with their multiplier, balance and vouchers on magpie's usage card, plus a page for spending and top-ups.",
5
5
  "type": "module",
6
6
  "main": "index.mjs",
package/page/app.js CHANGED
@@ -155,8 +155,9 @@
155
155
  function tick() {
156
156
  tickCount += 1
157
157
  const counting = view && view.pending
158
- if (counting && tickCount % 2 === 0) load().catch(() => {})
159
- else if (!counting && tickCount % 60 === 0) load().catch(() => {})
158
+ // 等确认时每秒问一次:站点那边一点批准,这边下一拍就能接上
159
+ if (counting) load().catch(() => {})
160
+ else if (tickCount % 60 === 0) load().catch(() => {})
160
161
  if (counting) paintCountdown()
161
162
  }
162
163
 
@@ -253,6 +254,11 @@
253
254
  onclick: () => act('打开确认页', () => call('neartoken:open')),
254
255
  }, '打开确认页'),
255
256
  el('button', { disabled: !!pendingAct, onclick: () => copy(p.verificationUriComplete) }, '复制链接'),
257
+ el('button', {
258
+ class: 'ghost',
259
+ disabled: !!pendingAct,
260
+ onclick: () => { load().catch(() => {}) },
261
+ }, '我已确认,立即刷新'),
256
262
  el('button', {
257
263
  class: 'ghost',
258
264
  disabled: !!pendingAct,
@@ -642,6 +648,77 @@
642
648
  act('断开连接', () => call('neartoken:disconnect'))
643
649
  })
644
650
 
651
+ // ---------- 检查更新 ----------
652
+
653
+ /** cmp 比两段版本号:a 比 b 新返回正数。只认数字段,够用了。 */
654
+ function cmpVersion(a, b) {
655
+ const pa = String(a).split(/[.-]/).map((x) => parseInt(x, 10) || 0)
656
+ const pb = String(b).split(/[.-]/).map((x) => parseInt(x, 10) || 0)
657
+ for (let i = 0; i < Math.max(pa.length, pb.length); i += 1) {
658
+ const d = (pa[i] || 0) - (pb[i] || 0)
659
+ if (d) return d
660
+ }
661
+ return 0
662
+ }
663
+
664
+ /**
665
+ * loadLatest 取 npm 上的最新版本。
666
+ *
667
+ * 页面自己查不了,插件会把结果写成 page/latest.js;这里用 script 引入,
668
+ * 加 ?t= 破缓存,所以点一次「检查更新」就是现问一次。
669
+ */
670
+ function loadLatest() {
671
+ return new Promise((resolve) => {
672
+ const s = document.createElement('script')
673
+ s.src = 'latest.js?t=' + Date.now()
674
+ s.onload = () => resolve(typeof self.NEARTOKEN_LATEST === 'string' ? self.NEARTOKEN_LATEST : '')
675
+ s.onerror = () => resolve('')
676
+ document.head.append(s)
677
+ })
678
+ }
679
+
680
+ let checking = false
681
+ async function checkUpdate() {
682
+ if (checking) return
683
+ checking = true
684
+ const btn = $('updates')
685
+ const note = $('update-note')
686
+ const was = btn.textContent
687
+ btn.disabled = true
688
+ btn.textContent = '检查中…'
689
+ note.hidden = true
690
+ const latest = await loadLatest()
691
+ const mine = self.NEARTOKEN_VERSION || ''
692
+ checking = false
693
+ btn.disabled = false
694
+ btn.textContent = was
695
+ if (!latest) {
696
+ note.className = 'update-note warn'
697
+ note.textContent = '没查到最新版本:插件还没联网拉过一次,稍后再试。'
698
+ note.hidden = false
699
+ return
700
+ }
701
+ if (mine && cmpVersion(latest, mine) > 0) {
702
+ note.className = 'update-note new'
703
+ note.textContent = '有新版本 v' + latest + '(当前 v' + mine + ')'
704
+ const link = el('a', {
705
+ class: 'link',
706
+ href: 'https://github.com/Fish-under-sea/magpie-neartoken/releases',
707
+ target: '_blank',
708
+ rel: 'noreferrer',
709
+ style: 'margin-left:8px',
710
+ }, '去下载 ↗')
711
+ note.append(link)
712
+ note.hidden = false
713
+ return
714
+ }
715
+ note.className = 'update-note'
716
+ note.textContent = '已是最新 v' + (mine || latest)
717
+ note.hidden = false
718
+ }
719
+
720
+ $('updates').addEventListener('click', checkUpdate)
721
+
645
722
  // ---------- 画图 ----------
646
723
 
647
724
  /** stackedChart 画一张按模型堆叠的面积图;模型多于 5 个时余下的合并。 */
package/page/icon.png CHANGED
Binary file
package/page/index.html CHANGED
@@ -14,11 +14,14 @@
14
14
  <h1>NearToken</h1>
15
15
  <p class="sub" id="site">neartoken.cc</p>
16
16
  </div>
17
- <span class="chip ver" id="version" title="插件版本">v—</span>
18
- <a class="ghost link" id="updates" href="https://github.com/Fish-under-sea/magpie-neartoken/releases" target="_blank" rel="noreferrer">检查更新</a>
19
- <span class="chip" id="status"><span class="dot"></span><span id="status-text">正在读取</span></span>
20
- <button class="ghost" id="disconnect" hidden>断开连接</button>
17
+ <div class="actions">
18
+ <span class="chip ver" id="version" title="插件版本">v—</span>
19
+ <button class="ghost" id="updates" type="button">检查更新</button>
20
+ <span class="chip" id="status"><span class="dot"></span><span id="status-text">正在读取</span></span>
21
+ <button class="ghost" id="disconnect" type="button" hidden>断开连接</button>
22
+ </div>
21
23
  </header>
24
+ <p class="update-note" id="update-note" hidden></p>
22
25
  <main id="view" aria-live="polite">
23
26
  <div class="card"><p class="brief">正在读取 NearToken 的状态…</p></div>
24
27
  </main>
package/page/latest.js ADDED
@@ -0,0 +1,2 @@
1
+ // 由插件在拉取目录时覆盖写入;这里只是占位。
2
+ self.NEARTOKEN_LATEST = self.NEARTOKEN_LATEST || ''
package/page/style.css CHANGED
@@ -457,4 +457,19 @@ select {
457
457
  padding: 3px 8px;
458
458
  }
459
459
 
460
- .ghost.link { text-decoration: none; display: inline-flex; align-items: center; }
460
+ .ghost.link { text-decoration: none; display: inline-flex; align-items: center; }
461
+ /* 头部按钮组:版本、检查更新、状态、断开四件一样高,右对齐 */
462
+ .head { align-items: center; gap: 12px; }
463
+
464
+ .actions { display: flex; align-items: center; gap: 8px; margin-left: auto; }
465
+ .actions .chip { height: 28px; display: inline-flex; align-items: center; gap: 6px; padding: 0 10px; }
466
+ .actions .ghost { height: 28px; display: inline-flex; align-items: center; }
467
+ .actions .ghost[disabled] { opacity: 0.6; }
468
+
469
+ .update-note {
470
+ margin: 8px 0 0;
471
+ font-size: 11.5px;
472
+ color: var(--nt-muted);
473
+ }
474
+ .update-note.new { color: var(--nt-accent); }
475
+ .update-note.warn { color: var(--nt-warn, #e0a44a); }
package/page/version.js CHANGED
@@ -1,2 +1,2 @@
1
1
  // 由 scripts/stamp.mjs 生成,别手改:版本号在 package.json。
2
- self.NEARTOKEN_VERSION = "0.2.2"
2
+ self.NEARTOKEN_VERSION = "0.2.4"