// ==UserScript== // @name 小红书个人作品链接抓取 // @namespace https://www.xiaohongshu.com/ // @version 1.0 // @description 在小红书用户个人主页一键抓取全部作品链接 // @match https://www.xiaohongshu.com/user/profile/* // @grant none // @run-at document-idle // @license MIT // ==/UserScript== (function () { 'use strict'; const SEL = 'a.cover.mask.ld'; const STEP = 0.8, WAIT = 600, IDLE = 5; const sleep = ms => new Promise(r => setTimeout(r, ms)); async function grabLinks() { const found = new Set(); const collect = () => document.querySelectorAll(SEL).forEach(a => { const h = a.getAttribute('href'); if (h) found.add(new URL(h, location.origin).href); }); const startY = window.scrollY; let last = -1, stall = 0; for (let i = 0; i < 2000; i++) { collect(); window.scrollTo(0, window.scrollY + window.innerHeight * STEP); await sleep(WAIT); const atBottom = window.scrollY + window.innerHeight >= document.documentElement.scrollHeight - 5; if (atBottom) await sleep(800); if (found.size === last && atBottom) stall++; else stall = 0; last = found.size; btn.textContent = `抓取中… 第 ${i + 1} 轮 / 累计 ${found.size} 条`; if (stall >= IDLE) break; } window.scrollTo(0, startY); btn.textContent = `抓取完成 (${found.size} 条) — 点击重新抓取`; btn.disabled = false; showPanel([...found]); } function showPanel(list) { const text = list.join('\n'); closePanel(); const wrap = document.createElement('div'); wrap.id = '__xhs_grabber_panel__'; wrap.style.cssText = 'position:fixed;top:20px;right:20px;z-index:2147483647;width:500px;height:460px;background:#1e1e1e;border:1px solid #444;border-radius:8px;display:flex;flex-direction:column;padding:10px;box-shadow:0 8px 24px rgba(0,0,0,.45);font:12px/1.4 sans-serif;color:#ddd'; const bar = document.createElement('div'); bar.style.cssText = 'display:flex;gap:8px;align-items:center;margin-bottom:8px'; const title = document.createElement('b'); title.textContent = `共 ${list.length} 条链接`; title.style.marginRight = 'auto'; bar.appendChild(title); const mkBtn = (label, fn, bg) => { const b = document.createElement('button'); b.textContent = label; b.style.cssText = `background:${bg || '#3a7afe'};color:#fff;border:0;border-radius:4px;padding:6px 10px;cursor:pointer`; b.onclick = fn; return b; }; const ta = document.createElement('textarea'); ta.value = text; ta.style.cssText = 'flex:1;width:100%;box-sizing:border-box;resize:none;background:#111;color:#9cdcfe;border:1px solid #333;border-radius:4px;padding:8px;font:12px Consolas,monospace;white-space:pre;overflow:auto'; const copyBtn = mkBtn('复制', async () => { try { await navigator.clipboard.writeText(ta.value); copyBtn.textContent = '已复制 ✓'; } catch { ta.select(); document.execCommand('copy'); copyBtn.textContent = '已复制 ✓'; } setTimeout(() => copyBtn.textContent = '复制', 1500); }); const dlBtn = mkBtn('下载', () => { const a = Object.assign(document.createElement('a'), { href: URL.createObjectURL(new Blob([ta.value], { type: 'text/plain;charset=utf-8' })), download: `xhs_links_${Date.now()}.txt` }); a.click(); setTimeout(() => URL.revokeObjectURL(a.href), 3000); }, '#28a745'); const closeBtn = mkBtn('关闭', closePanel, '#666'); bar.append(title, copyBtn, dlBtn, closeBtn); wrap.appendChild(bar); wrap.appendChild(ta); document.body.appendChild(wrap); ta.focus(); } function closePanel() { document.getElementById('__xhs_grabber_panel__')?.remove(); } // ---------- 悬浮触发按钮 ---------- const btn = document.createElement('button'); btn.textContent = '📥 抓取全部作品链接'; btn.style.cssText = 'position:fixed;bottom:24px;right:24px;z-index:2147483647;background:#ff2442;color:#fff;border:0;border-radius:24px;padding:12px 20px;font-size:14px;font-weight:600;cursor:pointer;box-shadow:0 4px 16px rgba(255,36,66,.4);transition:transform .15s'; btn.onmouseenter = () => btn.style.transform = 'scale(1.05)'; btn.onmouseleave = () => btn.style.transform = 'scale(1)'; btn.onclick = () => { btn.disabled = true; btn.style.opacity = '0.7'; grabLinks().finally(() => { btn.style.opacity = '1'; }); }; // 等 body 出现再挂按钮 if (document.body) { document.body.appendChild(btn); } else { const ob = new MutationObserver(() => { if (document.body) { document.body.appendChild(btn); ob.disconnect(); } }); ob.observe(document.documentElement, { childList: true }); } })();