匹配范围
*://tieba.baidu.com/*tieba-hot-topics用途 读取百度贴吧热门话题页面,返回热门话题标题、讨论量、简介和链接。 执行前提 请先打开百度贴吧热门话题页面,或 tieba.baidu.com 域名下的页面:https://tieba.baidu.com/hottopic/browse/topicList?res_type=1。脚本读取当前热门话题 DOM;如果在同域其他页面执行,会尝试跳转到热门话题…
*://tieba.baidu.com/*dom.read1return await (async () => {2 const targetUrl = "https://tieba.baidu.com/hottopic/browse/topicList?res_type=1";3 const input = typeof params === "object" && params ? params : {};4 const limitValue = Number(input.limit);5 const limit = Math.max(1, Math.min(Number.isFinite(limitValue) ? Math.trunc(limitValue) : 20, 50));6 7 const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));8 const cleanText = (node) => (node?.textContent || "").replace(/\s+/g, " ").trim();9 10 if (!location.href.startsWith(targetUrl)) {11 location.assign(targetUrl);12 }13 14 const startedAt = Date.now();15 while (Date.now() - startedAt < 15000) {16 const hasItems = document.querySelector("li.topic-top-item");17 const blockedText = document.body?.innerText || "";18 if (hasItems || /百度安全验证|安全验证|请完成验证|验证码/.test(blockedText)) break;19 await sleep(250);20 }21 22 const items = Array.from(document.querySelectorAll("li.topic-top-item"))23 .map((item, index) => {24 const titleNode = item.querySelector("a.topic-text");25 const href = titleNode?.getAttribute("href") || "";26 const url = href ? (href.startsWith("http") ? href : `https://tieba.baidu.com${href}`) : "";27 28 return {29 rank: index + 1,30 title: cleanText(titleNode),31 discussions: cleanText(item.querySelector("span.topic-num")),32 description: cleanText(item.querySelector("p.topic-top-item-desc")),33 url,34 };35 })36 .filter((item) => item.title)37 .slice(0, limit);38 39 return {40 sourceUrl: targetUrl,41 count: items.length,42 items,43 };44})();