detail.js
1 import { cli } from '@jackwener/opencli/registry'; 2 3 cli({ 4 site: 'nowcoder', 5 name: 'detail', 6 description: 'Post detail view (supports ID / UUID / URL)', 7 domain: 'www.nowcoder.com', 8 args: [ 9 { name: 'id', positional: true, required: true, help: 'Post ID, UUID, or URL' }, 10 ], 11 columns: ['title', 'author', 'school', 'content', 'likes', 'comments', 'views', 'time', 'location'], 12 pipeline: [ 13 { navigate: 'https://www.nowcoder.com' }, 14 { evaluate: `(async () => { 15 const raw = \${{ args.id | json }}; 16 const base = 'https://gw-c.nowcoder.com'; 17 const strip = (html) => (html || '').replace(/<[^>]+>/g, '').replace(/ /g, ' ').replace(/</g, '<').replace(/>/g, '>').replace(/&/g, '&').trim(); 18 19 let id = raw; 20 const urlMatch = raw.match(/discuss\\/(\\d+)/); 21 if (urlMatch) id = urlMatch[1]; 22 23 let data = null; 24 25 if (/[a-f]/.test(id) && id.length > 20) { 26 const r = await fetch(base + '/api/sparta/detail/moment-data/detail/' + id, {credentials: 'include'}); 27 const d = await r.json(); 28 if (d.success && d.data) data = d.data; 29 } 30 31 if (!data && /^\\d+$/.test(id)) { 32 const r = await fetch(base + '/api/sparta/detail/content-data/detail/' + id, {credentials: 'include'}); 33 const d = await r.json(); 34 if (d.success && d.data) data = d.data; 35 } 36 37 if (!data && /^\\d+$/.test(id)) { 38 const r = await fetch(base + '/api/sparta/detail/moment-data/detail/' + id, {credentials: 'include'}); 39 const d = await r.json(); 40 if (d.success && d.data) data = d.data; 41 } 42 43 if (!data) throw new Error('Post not found: ' + id); 44 45 const user = data.userBrief || {}; 46 const freq = data.frequencyData || {}; 47 return [{ 48 title: data.title || '(untitled)', 49 author: user.nickname || '', 50 school: user.educationInfo || '', 51 content: strip(data.content || '').substring(0, 500), 52 likes: freq.likeCnt || 0, 53 comments: freq.commentCnt || freq.totalCommentCnt || 0, 54 views: freq.viewCnt || 0, 55 time: data.createdAt ? new Date(data.createdAt).toISOString().slice(0, 19) : '', 56 location: data.ip4Location || '', 57 }]; 58 })() 59 ` }, 60 ], 61 });