1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
(function () { 'use strict';
function getTextDeep(el) { if (!el) return ''; if (el.nodeType === Node.TEXT_NODE) { return el.textContent.trim(); } return Array.from(el.childNodes) .map(getTextDeep) .join(' ') .replace(/\s+/g, ' ') .trim(); }
function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); }
async function extractAllComments() { const result = [];
const biliComments = document.querySelector('bili-comments'); if (!biliComments?.shadowRoot) { alert('未找到评论容器,请确认在视频播放页'); return result; }
await sleep(1500);
const threads = biliComments.shadowRoot.querySelectorAll( 'bili-comment-thread-renderer' );
threads.forEach(thread => { if (!thread.shadowRoot) return;
const renderer = thread.shadowRoot.querySelector( 'bili-comment-renderer#comment' ); if (!renderer?.shadowRoot) return;
let username = ''; const userInfo = renderer.shadowRoot.querySelector('bili-comment-user-info'); if (userInfo?.shadowRoot) { const nameBox = userInfo.shadowRoot.querySelector('#user-name'); username = getTextDeep(nameBox); }
let content = ''; const contentDiv = renderer.shadowRoot.querySelector('#content'); if (contentDiv) { const richText = contentDiv.querySelector('bili-rich-text'); if (richText?.shadowRoot) { const contents = richText.shadowRoot.querySelector('#contents'); content = getTextDeep(contents); } }
if (username || content) { result.push(`${username}:${content}`); } });
return result; }
function createButton() { const btn = document.createElement('button'); btn.innerText = '📋 一键复制评论'; Object.assign(btn.style, { position: 'fixed', top: '80px', right: '20px', zIndex: '99999', padding: '10px 14px', background: '#fb7299', color: '#fff', border: 'none', borderRadius: '8px', cursor: 'pointer', fontSize: '14px', boxShadow: '0 2px 8px rgba(0,0,0,.2)' });
btn.onclick = async () => { btn.innerText = '⏳ 正在提取...'; const comments = await extractAllComments();
if (!comments.length) { alert('未提取到评论,请确认评论已加载'); btn.innerText = '📋 一键复制评论'; return; }
const text = comments.join('\n\n'); GM_setClipboard(text);
btn.innerText = `✅ 已复制 ${comments.length} 条评论`; setTimeout(() => { btn.innerText = '📋 一键复制评论'; }, 2000); };
document.body.appendChild(btn); }
window.addEventListener('load', () => { setTimeout(createButton, 1000); }); })();
|