转换为换行 + const text = contentHtml + .replace(/
/gi, '\n') + .replace(/]*>/gi, '') // 移除链接标签开始 + .replace(/<\/a>/gi, '') // 移除链接标签结束 + .replace(/<[^>]+>/g, '') // 移除其他 HTML 标签 + .replace(/ /g, ' ') + .replace(/&/g, '&') + .replace(/</g, '<') + .replace(/>/g, '>') + .replace(/"/g, '"'); + + // 按空行分割段落 + const lines = text.split('\n'); + for (const line of lines) { + const para = line.trim(); + if (para && para.length > 0) { + paragraphs.push(para); + } + } + } + + // 生成 markdown + const mdLines = [ + `# ${title || '无标题'}`, + '', + `> **作者**: ${author || '未知'}`, + `> **发布时间**: ${publishTime || '未知'}`, + `> **浏览**: ${views || '-'} | **评论**: ${comments || '-'}`, + `> **原文**: ${window.location.href}`, + '', + '---', + '' + ]; + + for (const para of paragraphs) { + const clean = para.replace(/\s+/g, ' ').trim(); + if (clean) { + mdLines.push(clean); + mdLines.push(''); + } + } + + return { + title: title || '无标题', + author: author || '未知', + publish_time: publishTime || '未知', + views: views, + comments: comments, + url: window.location.href, + paragraphs: paragraphs.length, + markdown: mdLines.join('\n') + }; + })() + +columns: [title, author, publish_time, views, comments, paragraphs] diff --git a/src/clis/tgb/user-posts.yaml b/src/clis/tgb/user-posts.yaml new file mode 100644 index 0000000..fc9c0e9 --- /dev/null +++ b/src/clis/tgb/user-posts.yaml @@ -0,0 +1,123 @@ +site: tgb +name: user-posts +description: 获取用户文章列表 +domain: tgb.cn +strategy: cookie +browser: true + +args: + user_id: + type: str + required: true + description: 用户ID + limit: + type: int + default: 20 + description: 获取文章数量,默认20 + +pipeline: + - navigate: "https://www.tgb.cn/user/blog/moreTopic?userID=${{ args.user_id }}" + - evaluate: | + (async () => { + // 优先尝试从已加载的DOM中提取(更快) + const tables = document.querySelectorAll('table'); + const posts = []; + + for (const table of tables) { + const rows = table.querySelectorAll('tr'); + + for (const row of rows) { + const cells = row.querySelectorAll('td'); + if (cells.length < 8) continue; + + const titleCell = cells[1]; + if (!titleCell) continue; + + const link = titleCell.querySelector('a'); + if (!link) continue; + + const href = link.getAttribute('href') || ''; + if (!href.match(/^a\/[a-zA-Z0-9]+/)) continue; + + const url = 'https://www.tgb.cn/' + href; + const title = titleCell.innerText.trim(); + const author = cells[2]?.innerText?.trim() || ''; + const commentTime = cells[3]?.innerText?.trim() || ''; + const publishDate = cells[7]?.innerText?.trim() || ''; + + if (title && url) { + posts.push({ + title: title, + author: author, + comment_time: commentTime, + publish_date: publishDate, + url: url + }); + } + } + } + + // 如果DOM提取不到,尝试fetch获取HTML + if (posts.length === 0) { + try { + const res = await fetch(window.location.href, { + credentials: 'include' + }); + const html = await res.text(); + + // 解析表格行 + const rowRegex = /