dsh-pick 0.4.0 → 0.4.2
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/bin/dsh-pick.js +9 -1
- package/lib/engine.js +33 -5
- package/package.json +1 -1
- package/plugin/index.js +23 -1
- package/web/panel.html +24 -5
- package/web/panel.js +110 -20
package/bin/dsh-pick.js
CHANGED
|
@@ -11,7 +11,7 @@ import { createServer } from 'node:http'
|
|
|
11
11
|
import { readFile } from 'node:fs/promises'
|
|
12
12
|
import { join, extname } from 'node:path'
|
|
13
13
|
import { fileURLToPath } from 'node:url'
|
|
14
|
-
import { buildCatalog, loadCatalog, searchByNeed, rank, stats, normalize } from '../lib/engine.js'
|
|
14
|
+
import { buildCatalog, loadCatalog, searchByNeed, rank, stats, liveStats, normalize } from '../lib/engine.js'
|
|
15
15
|
|
|
16
16
|
const PKG_ROOT = fileURLToPath(new URL('..', import.meta.url))
|
|
17
17
|
const [, , cmd = 'help', ...rest] = process.argv
|
|
@@ -117,6 +117,14 @@ async function main() {
|
|
|
117
117
|
try {
|
|
118
118
|
let p = decodeURIComponent(new URL(req.url, 'http://x').pathname)
|
|
119
119
|
if (p === '/') p = '/web/panel.html'
|
|
120
|
+
// 「我的监控」的刷新:实时查 npm(和插件里的 /api-dsh-pick/live 一样)
|
|
121
|
+
if (p === '/live') {
|
|
122
|
+
const names = [...new Set((new URL(req.url, 'http://x').searchParams.get('names') || '').split(',')
|
|
123
|
+
.map(s => s.trim()).filter(s => /^(@[a-z0-9._-]+\/)?[a-z0-9._-]+$/i.test(s)))].slice(0, 30)
|
|
124
|
+
res.writeHead(names.length ? 200 : 400, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' })
|
|
125
|
+
res.end(JSON.stringify(names.length ? await liveStats(names) : { error: '没有合法的包名' }))
|
|
126
|
+
return
|
|
127
|
+
}
|
|
120
128
|
const prefix = Object.keys(ROOTS).find(k => p.startsWith(k))
|
|
121
129
|
if (!prefix) { res.writeHead(403).end('forbidden'); return }
|
|
122
130
|
let buf
|
package/lib/engine.js
CHANGED
|
@@ -387,15 +387,43 @@ export async function stats(catalog, names) {
|
|
|
387
387
|
out.push({ source: 'catalog', ...serialize(rec) })
|
|
388
388
|
continue
|
|
389
389
|
}
|
|
390
|
-
const live = await fetchLive(name)
|
|
391
|
-
|
|
390
|
+
const [live, dl] = await Promise.all([fetchLive(name), searchDownloads(name)])
|
|
391
|
+
// 周 / 月下载和目录同一口径(见 liveStats)
|
|
392
|
+
out.push(live ? { source: 'live', ...live, ...(dl ? { weekly: dl.weekly, monthly: dl.monthly } : {}) } : { source: 'not-found', name })
|
|
392
393
|
}
|
|
393
394
|
return out
|
|
394
395
|
}
|
|
395
396
|
|
|
396
|
-
|
|
397
|
-
|
|
397
|
+
/**
|
|
398
|
+
* 实时查几个包(「我的监控」的刷新):统计截止日只探测一次,下载量接口低并发(稍高就 429)。
|
|
399
|
+
* 不带 30 天日序列(太长,界面用不上)
|
|
400
|
+
* @returns {Promise<{at: string, anchor: string, rows: object[]}>}
|
|
401
|
+
*/
|
|
402
|
+
export async function liveStats(names) {
|
|
398
403
|
const anchor = await resolveAnchor()
|
|
404
|
+
const rows = await pool(names, 2, async name => {
|
|
405
|
+
const [r, dl] = await Promise.all([fetchLive(name, anchor), searchDownloads(name)])
|
|
406
|
+
if (!r && !dl) return { name, found: false }
|
|
407
|
+
const { daily, keywords, ...rest } = r || { name, deep: null }
|
|
408
|
+
// 周下载 / 月下载用搜索接口的:和目录同一口径(目录就是从它抓的),而且比下载统计接口新——
|
|
409
|
+
// 下载统计接口常常只到几天前,拿它和目录比会凭空「跌」一截
|
|
410
|
+
return dl
|
|
411
|
+
? { found: true, ...rest, weekly: dl.weekly, monthly: dl.monthly, latest: rest.latest || dl.version }
|
|
412
|
+
: { found: true, ...rest }
|
|
413
|
+
})
|
|
414
|
+
return { at: new Date().toISOString(), anchor, rows }
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/** npm 搜索接口里这个包的周 / 月下载(按包名精确匹配;查不到返回 null) */
|
|
418
|
+
async function searchDownloads(name) {
|
|
419
|
+
const d = await jget(`${SEARCH_API}?text=${encodeURIComponent(name)}&size=5`, 2)
|
|
420
|
+
const o = d && Array.isArray(d.objects) && d.objects.find(x => x.package && x.package.name === name)
|
|
421
|
+
return o && o.downloads ? { weekly: o.downloads.weekly || 0, monthly: o.downloads.monthly || 0, version: o.package.version } : null
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
async function fetchLive(name, knownAnchor) {
|
|
425
|
+
const today = new Date().toISOString().slice(0, 10)
|
|
426
|
+
const anchor = knownAnchor || await resolveAnchor()
|
|
399
427
|
const start = shiftDate(anchor, -29)
|
|
400
428
|
const [range, total, meta] = await Promise.all([
|
|
401
429
|
jget(`${DL_API}/range/${start}:${anchor}/${enc(name)}`),
|
|
@@ -466,4 +494,4 @@ function serialize(rec) {
|
|
|
466
494
|
}
|
|
467
495
|
|
|
468
496
|
|
|
469
|
-
export default { buildCatalog, loadCatalog, reloadCatalog, searchByNeed, rank, stats, CATALOG_PATH, resolveAnchor }
|
|
497
|
+
export default { buildCatalog, loadCatalog, reloadCatalog, searchByNeed, rank, stats, liveStats, CATALOG_PATH, resolveAnchor }
|
package/package.json
CHANGED
package/plugin/index.js
CHANGED
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
// POST /api-dsh-pick/update 更新到 npm 最新版(交给 DSH 的插件管理器)
|
|
15
15
|
// POST /api-dsh-pick/uninstall 从 DSH 卸载本插件
|
|
16
16
|
// GET /api-dsh-pick/installed DSH 里已装的插件名(面板的「已安装」标记)
|
|
17
|
+
// GET /api-dsh-pick/live?names=a,b 实时查 npm(「我的监控」的刷新,最多 30 个)
|
|
17
18
|
// POST /api-dsh-pick/install 安装目录里的某个插件 { name }
|
|
18
19
|
//
|
|
19
20
|
// 另外注册 4 个 AI 工具(plugin/tools.js):对话里的 AI 能自己查目录、比较插件。
|
|
@@ -24,7 +25,7 @@ import { readFile } from 'node:fs/promises'
|
|
|
24
25
|
import { fileURLToPath } from 'node:url'
|
|
25
26
|
import { join } from 'node:path'
|
|
26
27
|
import { registerTools } from './tools.js'
|
|
27
|
-
import { loadCatalog } from '../lib/engine.js'
|
|
28
|
+
import { loadCatalog, liveStats } from '../lib/engine.js'
|
|
28
29
|
import { refreshCatalog, remoteState, CHECK_EVERY_MS } from '../lib/remote.js'
|
|
29
30
|
|
|
30
31
|
/** 本包根目录:插件就是完整的 dsh-pick 包 */
|
|
@@ -148,6 +149,12 @@ function installedNames(pluginManager) {
|
|
|
148
149
|
|
|
149
150
|
const NAME_RE = /^(@[a-z0-9._-]+\/)?[a-z0-9._-]+$/i
|
|
150
151
|
|
|
152
|
+
/** ?names=a,b,c → 合法、去重的包名,最多 30 个(监控列表本身也最多 30 个) */
|
|
153
|
+
export function parseNames(url) {
|
|
154
|
+
const raw = new URL(url || '/', 'http://x').searchParams.get('names') || ''
|
|
155
|
+
return [...new Set(raw.split(',').map(s => s.trim()).filter(s => NAME_RE.test(s)))].slice(0, 30)
|
|
156
|
+
}
|
|
157
|
+
|
|
151
158
|
function sendJson(res, status, body) {
|
|
152
159
|
res.writeHead(status, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' })
|
|
153
160
|
res.end(JSON.stringify(body))
|
|
@@ -340,6 +347,21 @@ export function apply(ctx) {
|
|
|
340
347
|
},
|
|
341
348
|
}))
|
|
342
349
|
|
|
350
|
+
// 实时数据:「我的监控」点刷新时,直接去 npm 查这几个包(只读公开数据)
|
|
351
|
+
track(ws.register({
|
|
352
|
+
kind: 'exact',
|
|
353
|
+
path: '/api-dsh-pick/live',
|
|
354
|
+
handler: async (req, res) => {
|
|
355
|
+
const names = parseNames(req.url)
|
|
356
|
+
if (!names.length) return sendJson(res, 400, { error: '没有合法的包名' })
|
|
357
|
+
try {
|
|
358
|
+
sendJson(res, 200, await liveStats(names))
|
|
359
|
+
} catch (e) {
|
|
360
|
+
sendJson(res, 502, { error: `查 npm 失败:${e.message}` })
|
|
361
|
+
}
|
|
362
|
+
},
|
|
363
|
+
}))
|
|
364
|
+
|
|
343
365
|
// 已装的插件(面板卡片上的「已安装」)
|
|
344
366
|
track(ws.register({
|
|
345
367
|
kind: 'exact',
|
package/web/panel.html
CHANGED
|
@@ -107,6 +107,13 @@ h1{margin:0;font-size:19px;font-weight:650;letter-spacing:-.2px}
|
|
|
107
107
|
.ai:hover{border-color:color-mix(in srgb,var(--ai) 62%,transparent);
|
|
108
108
|
background:linear-gradient(135deg,color-mix(in srgb,var(--accent) 22%,transparent),color-mix(in srgb,var(--ai) 28%,transparent))}
|
|
109
109
|
.section h2 .ai{margin-left:auto;font-weight:650}
|
|
110
|
+
.section h2 .h2-acts{margin-left:auto;display:inline-flex;gap:8px;align-items:center}
|
|
111
|
+
.section h2 .h2-acts .ai{margin-left:0}
|
|
112
|
+
.btn.sm{font-size:11.5px;padding:0 10px;height:24px;border-radius:7px}
|
|
113
|
+
.mon .live{font-size:10px;color:var(--ok-text);background:var(--ok-bg);border-radius:5px;padding:1px 5px;white-space:nowrap}
|
|
114
|
+
.mon .delta{font-size:10.5px;margin-left:3px}
|
|
115
|
+
.mon .delta.up{color:var(--ok)}.mon .delta.down{color:var(--bad)}
|
|
116
|
+
#watchAsOf b{color:var(--t2);font-weight:600}
|
|
110
117
|
.inst{appearance:none;font-family:inherit;font-size:10.5px;font-weight:600;border-radius:6px;padding:2px 9px;cursor:pointer;
|
|
111
118
|
border:1px solid var(--accent);background:var(--panel);color:var(--accent);white-space:nowrap}
|
|
112
119
|
.inst:hover{background:var(--accent-bg)}
|
|
@@ -192,7 +199,15 @@ table.rank th{text-align:left;font-size:11px;color:var(--t3);font-weight:600;pad
|
|
|
192
199
|
table.rank td{padding:9px 10px;border-bottom:1px solid var(--soft);color:var(--t2);font-variant-numeric:tabular-nums}
|
|
193
200
|
table.rank tbody tr:hover td{background:var(--row-hover)}
|
|
194
201
|
table.rank td.nm{color:var(--text);font-weight:650;font-variant-numeric:normal}
|
|
195
|
-
td.r,th.r{text-align:right}
|
|
202
|
+
td.r,th.r{text-align:right;white-space:nowrap}
|
|
203
|
+
/* 「更新」一格:最近发版 + 小字节奏 */
|
|
204
|
+
table.rank td.mt{line-height:1.35}
|
|
205
|
+
table.rank td.mt .sub{font-size:10.5px;color:var(--t3)}
|
|
206
|
+
table.rank td.mt .stale{color:var(--warn-text);font-weight:600}
|
|
207
|
+
/* 只有图标的外链(排行榜) */
|
|
208
|
+
table.rank td.lks{white-space:nowrap;width:1%}
|
|
209
|
+
.lk.ic{padding:0;width:24px;height:22px;justify-content:center;gap:0}
|
|
210
|
+
.lk.ic + .lk.ic{margin-left:4px}
|
|
196
211
|
td.up{color:var(--ok);font-weight:600}
|
|
197
212
|
td.down{color:var(--bad);font-weight:600}
|
|
198
213
|
.catpill{font-size:10.5px;background:var(--soft);color:var(--t2);border-radius:20px;padding:2px 8px;white-space:nowrap}
|
|
@@ -332,6 +347,10 @@ td.down{color:var(--bad);font-weight:600}
|
|
|
332
347
|
</div>
|
|
333
348
|
</div>
|
|
334
349
|
|
|
350
|
+
</div>
|
|
351
|
+
|
|
352
|
+
<!-- ============ Tab 2:排行榜 ============ -->
|
|
353
|
+
<div id="tab-rank" class="hidden">
|
|
335
354
|
<div class="section">
|
|
336
355
|
<h2>排行榜</h2>
|
|
337
356
|
<p class="cap">换一种方式发现:按去噪后的指标看整个生态。</p>
|
|
@@ -347,7 +366,7 @@ td.down{color:var(--bad);font-weight:600}
|
|
|
347
366
|
<thead><tr>
|
|
348
367
|
<th style="width:44px">#</th><th>插件</th><th>分类</th>
|
|
349
368
|
<th class="r">基线</th><th class="r">周下载</th><th class="r">环比</th>
|
|
350
|
-
<th class="r"
|
|
369
|
+
<th class="r" title="最近一次发版 · 平均几天发一个版本">更新</th><th></th>
|
|
351
370
|
</tr></thead>
|
|
352
371
|
<tbody id="rankBody"></tbody>
|
|
353
372
|
</table>
|
|
@@ -355,11 +374,11 @@ td.down{color:var(--bad);font-weight:600}
|
|
|
355
374
|
</div>
|
|
356
375
|
</div>
|
|
357
376
|
|
|
358
|
-
<!-- ============ Tab
|
|
377
|
+
<!-- ============ Tab 3:监控 ============ -->
|
|
359
378
|
<div id="tab-watch" class="hidden">
|
|
360
379
|
<div class="section">
|
|
361
|
-
<h2>我的监控<button class="ai hidden" id="aiWatch" title="把监控列表发到对话里,让 AI 看看谁在涨、谁停更了">+AI 看看近况</button></h2>
|
|
362
|
-
<p class="cap"
|
|
380
|
+
<h2>我的监控<span class="h2-acts"><button class="btn ghost sm" id="btnWatchRefresh" title="去 npm 实时查这些插件的最新数据">↻ 刷新</button><button class="ai hidden" id="aiWatch" title="把监控列表发到对话里,让 AI 看看谁在涨、谁停更了">+AI 看看近况</button></span></h2>
|
|
381
|
+
<p class="cap"><span id="watchAsOf"></span>监控列表存在浏览器本地,不上传。点「刷新」去 npm 查最新:版本号、发版时间是即时的;下载量 npm 大约一天更新一次,日常基线和环比按 npm 下载统计算(滞后约 5 天)。</p>
|
|
363
382
|
<div id="watchList"></div>
|
|
364
383
|
</div>
|
|
365
384
|
<div class="section">
|
package/web/panel.js
CHANGED
|
@@ -7,6 +7,7 @@ const IS_PLUGIN = location.pathname.indexOf('/api-dsh-pick') === 0
|
|
|
7
7
|
const P = {
|
|
8
8
|
words: IS_PLUGIN ? '/api-dsh-pick/words.js' : '/lib/words.js',
|
|
9
9
|
catalog: IS_PLUGIN ? '/api-dsh-pick/catalog.json' : '/data/catalog.json',
|
|
10
|
+
live: IS_PLUGIN ? '/api-dsh-pick/live' : '/live',
|
|
10
11
|
}
|
|
11
12
|
|
|
12
13
|
// 嵌在 DSH 里(同源 iframe)才有「+AI」和「安装」:消息交给外层(plugin/client.js)写进当前对话;
|
|
@@ -110,9 +111,10 @@ function askVerdict() {
|
|
|
110
111
|
}
|
|
111
112
|
|
|
112
113
|
function askWatch() {
|
|
113
|
-
const rows = watch().map(
|
|
114
|
+
const rows = watch().map(watchRec).filter(Boolean)
|
|
115
|
+
const live = LIVE.at ? `(npm 实时数据,${new Date(LIVE.at).toLocaleString()} 刷新;下载量含 CI / 镜像流量,不等于用户数)` : dataAsOf()
|
|
114
116
|
return [
|
|
115
|
-
`帮我看看我关注的这些 DSH 插件最近怎么样${
|
|
117
|
+
`帮我看看我关注的这些 DSH 插件最近怎么样${live}:`,
|
|
116
118
|
...rows.map(r => `- ${r.n}(${r.cat}):${factLine(r)}`),
|
|
117
119
|
'',
|
|
118
120
|
`谁在涨、谁停更了、有没有该换掉的?${TAIL}`,
|
|
@@ -251,15 +253,42 @@ function rankText(rec) {
|
|
|
251
253
|
}
|
|
252
254
|
|
|
253
255
|
const GH_SVG = '<svg viewBox="0 0 16 16" width="13" height="13" fill="currentColor"><path d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82a7.4 7.4 0 0 1 2-.27c.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A7.99 7.99 0 0 0 16 8c0-4.42-3.58-8-8-8z"/></svg>'
|
|
254
|
-
|
|
256
|
+
// npm 的方块标志(中间一个 n):缩到 13px 也认得出;颜色用 npm 的红
|
|
257
|
+
const NPM_SVG = '<svg viewBox="0 0 16 16" width="13" height="13"><path fill="#cb3837" d="M0 0h16v16H0z"/><path fill="#fff" d="M3 3h10v10h-2.5V5.5H8V13H3z"/></svg>'
|
|
255
258
|
|
|
256
|
-
|
|
259
|
+
/**
|
|
260
|
+
* 外链按钮。icon = 只要图标(排行榜这种窄列):悬停看得到完整地址
|
|
261
|
+
*/
|
|
262
|
+
function linkBtns(rec, { icon = false } = {}) {
|
|
263
|
+
const npm = rec.npmUrl || ('https://www.npmjs.com/package/' + rec.n)
|
|
264
|
+
const cls = icon ? 'lk ic' : 'lk'
|
|
257
265
|
let h = ''
|
|
258
266
|
if (rec.ghUrl) {
|
|
259
|
-
h += `<a class="
|
|
267
|
+
h += `<a class="${cls}" href="${esc(rec.ghUrl)}" target="_blank" rel="noopener" title="GitHub:${esc(rec.ghUrl)}" aria-label="GitHub">${GH_SVG}${icon ? '' : 'GitHub'}</a>`
|
|
260
268
|
}
|
|
261
|
-
h += `<a class="
|
|
262
|
-
return h
|
|
269
|
+
h += `<a class="${cls}" href="${esc(npm)}" target="_blank" rel="noopener" title="npm:${esc(npm)}" aria-label="npm">${NPM_SVG}${icon ? '' : 'npm'}</a>`
|
|
270
|
+
return h
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/** 最近一次发版:今天 / 昨天 / N天前 / N个月前 */
|
|
274
|
+
function ageText(age) {
|
|
275
|
+
if (age == null) return '—'
|
|
276
|
+
if (age <= 0) return '今天'
|
|
277
|
+
if (age === 1) return '昨天'
|
|
278
|
+
if (age < 60) return `${age}天前`
|
|
279
|
+
return `${Math.round(age / 30)}个月前`
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* 「更新」一格:上面最近一次发版,下面小字写版本节奏。两行都不折行,
|
|
284
|
+
* 比分成两列(窄栏里每列都被挤成「8 / 天 / 前」三行)省地方也整齐。超过 90 天没发版用提醒色
|
|
285
|
+
*/
|
|
286
|
+
function maintCell(d) {
|
|
287
|
+
if (!d) return '<td class="r mt">—</td>'
|
|
288
|
+
const stale = d.age != null && d.age > 90
|
|
289
|
+
const cad = d.cadence == null ? '' : `<div class="sub">${d.cadence}天/版</div>`
|
|
290
|
+
return `<td class="r mt" title="最近一次发版 ${d.lastRelease || ''}${d.cadence == null ? '' : ` · 平均 ${d.cadence} 天发一个版本`}">` +
|
|
291
|
+
`<div${stale ? ' class="stale"' : ''}>${ageText(d.age)}</div>${cad}</td>`
|
|
263
292
|
}
|
|
264
293
|
|
|
265
294
|
/* ---------------- 搜索(与 lib/engine.js 共用同一份词典 lib/words.js) --------------- */
|
|
@@ -333,8 +362,8 @@ function bindEvents() {
|
|
|
333
362
|
b.addEventListener('click', () => {
|
|
334
363
|
document.querySelectorAll('#tabs button').forEach(x => x.classList.toggle('on', x === b))
|
|
335
364
|
const t = b.dataset.tab
|
|
336
|
-
|
|
337
|
-
$('#tab-
|
|
365
|
+
// 每个标签一块内容:tab-pick / tab-rank / tab-watch
|
|
366
|
+
document.querySelectorAll('#tabs button').forEach(x => $('#tab-' + x.dataset.tab).classList.toggle('hidden', x.dataset.tab !== t))
|
|
338
367
|
})
|
|
339
368
|
})
|
|
340
369
|
|
|
@@ -349,6 +378,7 @@ function bindEvents() {
|
|
|
349
378
|
$('#aiCands').addEventListener('click', () => cands.length && askAI(askCands()))
|
|
350
379
|
$('#aiVerdict').addEventListener('click', () => picked.length >= 2 && askAI(askVerdict()))
|
|
351
380
|
$('#aiWatch').addEventListener('click', () => watch().length && askAI(askWatch()))
|
|
381
|
+
$('#btnWatchRefresh').addEventListener('click', refreshWatch)
|
|
352
382
|
|
|
353
383
|
$('#btnExport').addEventListener('click', exportHtml)
|
|
354
384
|
$('#btnWatch').addEventListener('click', addPickedToWatch)
|
|
@@ -564,9 +594,8 @@ function renderRank() {
|
|
|
564
594
|
`<td class="r"><b>${num(val(r))}</b></td>` +
|
|
565
595
|
`<td class="r">${num(r.w)}</td>` +
|
|
566
596
|
`<td class="r ${g === null ? '' : (g >= 0 ? 'up' : 'down')}">${pct(g)}</td>` +
|
|
567
|
-
|
|
568
|
-
`<td class="
|
|
569
|
-
`<td style="white-space:nowrap">${linkBtns(r)}</td>` +
|
|
597
|
+
maintCell(d) +
|
|
598
|
+
`<td class="lks">${linkBtns(r, { icon: true })}</td>` +
|
|
570
599
|
`</tr>`
|
|
571
600
|
}).join('')
|
|
572
601
|
}
|
|
@@ -585,9 +614,62 @@ function addPickedToWatch() {
|
|
|
585
614
|
alert('已加入我的监控(' + picked.join('、') + ')')
|
|
586
615
|
}
|
|
587
616
|
|
|
617
|
+
/* 刷新:去 npm 实时查监控列表里的包。结果存本地,下次打开还看得到上次刷新的数 */
|
|
618
|
+
const LIVE_KEY = 'dsh-pick-watch-live-v1'
|
|
619
|
+
let LIVE = (() => {
|
|
620
|
+
try { return JSON.parse(localStorage.getItem(LIVE_KEY)) || { at: null, rows: {} } } catch { return { at: null, rows: {} } }
|
|
621
|
+
})()
|
|
622
|
+
|
|
623
|
+
/**
|
|
624
|
+
* 监控卡片用的数据:有实时数据就用实时的(字段换成目录的短名,后面的代码不用分两套),
|
|
625
|
+
* 否则用目录里的。目录里没有、也没刷新过的返回 null
|
|
626
|
+
*/
|
|
627
|
+
function watchRec(n) {
|
|
628
|
+
const base = CAT.packages[n]
|
|
629
|
+
const live = LIVE.rows[n]
|
|
630
|
+
if (!live || !live.found) return base || null
|
|
631
|
+
const d = live.deep
|
|
632
|
+
return {
|
|
633
|
+
...(base || { n, cat: '(目录外)' }),
|
|
634
|
+
n,
|
|
635
|
+
v: live.latest || (base && base.v),
|
|
636
|
+
w: live.weekly,
|
|
637
|
+
m: live.monthly,
|
|
638
|
+
deep: d ? { base: d.baseline, growth: d.growth, cadence: d.cadence, age: d.ageDays, lastRelease: d.lastRelease, versions: d.versions, s: d.score } : null,
|
|
639
|
+
live: true,
|
|
640
|
+
catalogW: base ? base.w : null,
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
async function refreshWatch() {
|
|
645
|
+
const list = watch()
|
|
646
|
+
if (!list.length) return
|
|
647
|
+
const btn = $('#btnWatchRefresh')
|
|
648
|
+
btn.disabled = true
|
|
649
|
+
btn.textContent = '刷新中…'
|
|
650
|
+
try {
|
|
651
|
+
const r = await fetch(`${P.live}?names=${encodeURIComponent(list.join(','))}`, { cache: 'no-store' })
|
|
652
|
+
const j = await r.json()
|
|
653
|
+
if (!r.ok) throw new Error(j.error || 'HTTP ' + r.status)
|
|
654
|
+
LIVE = { at: j.at, anchor: j.anchor, rows: Object.fromEntries(j.rows.map(x => [x.name, x])) }
|
|
655
|
+
try { localStorage.setItem(LIVE_KEY, JSON.stringify(LIVE)) } catch { /* 存不下就只这次有效 */ }
|
|
656
|
+
const miss = j.rows.filter(x => !x.found).map(x => x.name)
|
|
657
|
+
toast(miss.length ? `已刷新;npm 上查不到:${miss.join('、')}` : `已刷新 ${j.rows.length} 个插件`)
|
|
658
|
+
} catch (e) {
|
|
659
|
+
toast('刷新失败:' + e.message + '(检查网络后再试)')
|
|
660
|
+
}
|
|
661
|
+
btn.disabled = false
|
|
662
|
+
btn.textContent = '↻ 刷新'
|
|
663
|
+
renderWatch()
|
|
664
|
+
}
|
|
665
|
+
|
|
588
666
|
function renderWatch() {
|
|
589
667
|
const list = watch()
|
|
590
668
|
$('#watchCount').textContent = list.length ? ` (${list.length})` : ''
|
|
669
|
+
$('#btnWatchRefresh').classList.toggle('hidden', !list.length)
|
|
670
|
+
$('#watchAsOf').innerHTML = !list.length ? ''
|
|
671
|
+
: LIVE.at ? `<b>实时数据</b>,${esc(new Date(LIVE.at).toLocaleString())} 刷新(npm 统计截止 ${esc(LIVE.anchor || '—')})。`
|
|
672
|
+
: `现在显示的是目录数据(截止 <b>${esc(CAT.anchor)}</b>)。`
|
|
591
673
|
const host = $('#watchList')
|
|
592
674
|
if (!list.length) {
|
|
593
675
|
host.innerHTML = '<div class="empty">还没有监控的插件。在「选插件」里挑几个,点「加入我的监控」。</div>'
|
|
@@ -596,16 +678,24 @@ function renderWatch() {
|
|
|
596
678
|
}
|
|
597
679
|
|
|
598
680
|
host.innerHTML = list.map(n => {
|
|
599
|
-
const r =
|
|
600
|
-
if (!r) return `<div class="mon"><span class="nm">${esc(n)}</span><span style="color:var(--t3);font-size:11.5px"
|
|
681
|
+
const r = watchRec(n)
|
|
682
|
+
if (!r) return `<div class="mon"><span class="nm">${esc(n)}</span><span style="color:var(--t3);font-size:11.5px">不在目录中,点「刷新」去 npm 查</span><button class="x" data-rm="${esc(n)}">×</button></div>`
|
|
601
683
|
const d = r.deep
|
|
684
|
+
// 和目录数据比,周下载涨了还是跌了(只在刷新过、差得明显时标)
|
|
685
|
+
let delta = ''
|
|
686
|
+
if (r.live && r.catalogW) {
|
|
687
|
+
const ch = (r.w - r.catalogW) / Math.max(r.catalogW, 1)
|
|
688
|
+
if (Math.abs(ch) >= 0.05) delta = `<span class="delta ${ch > 0 ? 'up' : 'down'}" title="和目录数据(截止 ${esc(CAT.anchor)})相比">${ch > 0 ? '↑' : '↓'}${Math.round(Math.abs(ch) * 100)}%</span>`
|
|
689
|
+
}
|
|
602
690
|
return `<div class="mon">` +
|
|
603
|
-
`<span class="nm">${esc(n)}</span>` +
|
|
691
|
+
`<span class="nm">${esc(n)}${r.v ? ` <span style="color:var(--t3);font-weight:400;font-size:11px">v${esc(r.v)}</span>` : ''}</span>` +
|
|
692
|
+
(r.live ? '<span class="live" title="刚从 npm 查的">实时</span>' : '') +
|
|
604
693
|
`<span class="mrank">${rankText(r)}</span>` +
|
|
605
694
|
`<span style="font-size:11.5px;color:var(--t3)">${esc(r.cat)}</span>` +
|
|
606
|
-
`<span style="font-size:12px"><b>${num(r.w)}</b
|
|
695
|
+
`<span style="font-size:12px"><b>${num(r.w)}</b>/周${delta}</span>` +
|
|
607
696
|
`<span style="font-size:12px;color:var(--t2)">基线 ${d ? num(d.base) : '—'}</span>` +
|
|
608
|
-
`<span
|
|
697
|
+
`<span style="font-size:12px;color:var(--t2)">${d ? ageText(d.age) + '发版' : ''}</span>` +
|
|
698
|
+
`<span class="mlinks">${linkBtns(r, { icon: true })}</span>` +
|
|
609
699
|
`<button class="x" data-rm="${esc(n)}">×</button></div>`
|
|
610
700
|
}).join('')
|
|
611
701
|
|
|
@@ -613,12 +703,12 @@ function renderWatch() {
|
|
|
613
703
|
b.addEventListener('click', () => saveWatch(watch().filter(x => x !== b.dataset.rm)) || renderWatch()))
|
|
614
704
|
|
|
615
705
|
// 对数刻度横条图
|
|
616
|
-
const rows = list.map(
|
|
706
|
+
const rows = list.map(watchRec).filter(Boolean)
|
|
617
707
|
const max = Math.max(...rows.map(r => r.w), 1)
|
|
618
708
|
$('#watchChart').innerHTML = rows.map((r, i) => {
|
|
619
709
|
const w = Math.max(Math.log10(r.w + 1) / Math.log10(max + 1) * 100, 1.5)
|
|
620
|
-
return `<div class="hrow" style="display:grid;grid-template-columns:
|
|
621
|
-
`<div style="font-size:12px;color:var(--t2);text-align:right;white-space:nowrap;overflow:hidden;text-overflow:ellipsis"><i style="display:inline-block;width:8px;height:8px;border-radius:2px;background:${colorOf(i)};margin-right:6px"></i>${esc(r.n)} <span style="color:var(--t3);font-size:10px">#${num(r.rank)}</span
|
|
710
|
+
return `<div class="hrow" style="display:grid;grid-template-columns:minmax(80px,38%) 1fr auto;align-items:center;gap:10px;padding:6px 0">` +
|
|
711
|
+
`<div style="font-size:12px;color:var(--t2);text-align:right;white-space:nowrap;overflow:hidden;text-overflow:ellipsis"><i style="display:inline-block;width:8px;height:8px;border-radius:2px;background:${colorOf(i)};margin-right:6px"></i>${esc(r.n)}${r.rank ? ` <span style="color:var(--t3);font-size:10px">#${num(r.rank)}</span>` : ''}</div>` +
|
|
622
712
|
`<div style="height:14px;background:var(--soft);border-radius:4px"><div style="height:100%;width:${w}%;background:${colorOf(i)};border-radius:4px"></div></div>` +
|
|
623
713
|
`<div style="font-size:12px;font-weight:650;text-align:right">${num(r.w)}</div></div>`
|
|
624
714
|
}).join('')
|