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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
(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 pageTitle = document.querySelector("title")?.textContent || document.title || ""; const pageUrl = document.querySelector("meta[property='og:url']")?.getAttribute("content") || window.location.href; if (pageTitle || pageUrl) { result.push(`标题:${pageTitle.trim()}\n链接:${pageUrl}\n---`); }
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}`); }
const repliesContainer = thread.shadowRoot.querySelector("#replies bili-comment-replies-renderer"); if (repliesContainer?.shadowRoot) { const replyRenderers = repliesContainer.shadowRoot.querySelectorAll("bili-comment-reply-renderer"); replyRenderers.forEach((replyRenderer) => { if (!replyRenderer.shadowRoot) return;
let replyUsername = ""; const replyUserInfo = replyRenderer.shadowRoot.querySelector("bili-comment-user-info"); if (replyUserInfo?.shadowRoot) { const nameBox = replyUserInfo.shadowRoot.querySelector("#user-name"); replyUsername = getTextDeep(nameBox); }
let replyContent = ""; const replyRichText = replyRenderer.shadowRoot.querySelector("bili-rich-text"); if (replyRichText?.shadowRoot) { const contents = replyRichText.shadowRoot.querySelector("#contents"); replyContent = getTextDeep(contents); }
if (replyUsername || replyContent) { result.push(` - ${replyUsername}:${replyContent}`); } }); } });
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); }); })();
|