网页插件:帖子列表预览回帖内容
效果预览


在帖子列表中,显示最后一条回帖内容的预览。
导入网页插件
导入网页插件:帖子列表预览回帖内容(当前用户:3,总安装次数:3)
<script src="https://hu60.cn/q.php/api.webplug-file/17528_public_webplug_topic_last_post/dist.js"></script>
说明
- 支持经典主题和 Jhin 主题
- 支持主页、论坛板块、帖子搜索等页面
- 为了避免触发限速,回帖内容默认分批加载
- 当帖子没有回帖时,会显示主楼内容
源码 & 自定义配置
如需自定义文字外观、加载限速等配置,可以使用下方代码自行新建一个网页插件,并在 CONFIG 处修改相关配置。
<script>
// 帖子列表预览回帖内容,版本 1,2025-11-26
// 你可以在 CONFIG 处自定义回帖文字外观,加载限速等配置
document.addEventListener('DOMContentLoaded', function () {
'use strict';
const CONFIG = {
// 回帖文字外观配置
fontSize: '12px', // 字体大小
fontColor: '#888', // 字体颜色
marginTop: '0px', // 上边距
marginBottom: '2px', // 下边距
lineHeight: '1.4', // 行高系数
lineCount: 1, // 文字行数
// 失败重试配置
retryCount: 2, // 失败最大重试次数(不含首次)
retryDelay: 500, // 重试延迟(毫秒)
// 限速配置
rateLimitCount: 5, // 每次批处理的最大请求数
rateLimitInterval: 1000, // 批处理的时间间隔(毫秒)
// 帖子接口地址模板
apiTemplate: (id) => `/q.php/bbs.topic.${id}.json?floorReverse=1&pageSize=2&_json=compact&_content=html`
};
// 帖子内容清洗
function cleanContent(html) {
const tempDiv = document.createElement('div');
tempDiv.innerHTML = html;
// 移除帖子状态(如待审核)提示内容
const infoBoxes = tempDiv.querySelectorAll('.tp.info-box');
infoBoxes.forEach(box => box.remove());
let text = tempDiv.textContent || tempDiv.innerText || '';
return text.replace(/\s+/g, ' ').trim();
}
// 处理单个帖子
function processTopic(linkElement) {
const href = linkElement.getAttribute('href');
if (!href) return;
const match = href.match(/bbs\.topic\.(\d+)\.html/);
if (!match) return;
const topicId = match[1];
const url = CONFIG.apiTemplate(topicId);
// 执行数据加载
function loadData(retriesLeft) {
fetch(url)
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
})
.then(data => {
if (data.tContents && data.tContents.length > 0) {
const lastPost = data.tContents[data.tContents.length - 1];
const userName = lastPost.uinfo ? lastPost.uinfo.name : '未知';
const plainText = cleanContent(lastPost.content);
const parent = linkElement.parentNode;
if (parent.lastElementChild && parent.lastElementChild.className === 'topic-preview-text') {
return;
}
const infoDiv = document.createElement('div');
infoDiv.className = 'topic-preview-text';
infoDiv.style.cssText = `
font-size: ${CONFIG.fontSize};
color: ${CONFIG.fontColor};
margin-top: ${CONFIG.marginTop};
margin-bottom: ${CONFIG.marginBottom};
line-height: ${CONFIG.lineHeight};
clear: both;
display: -webkit-box;
-webkit-line-clamp: ${CONFIG.lineCount};
-webkit-box-orient: vertical;
overflow: hidden;
text-overflow: ellipsis;
word-break: break-all;
`;
infoDiv.textContent = `${userName}:${plainText}`;
if (parent) {
parent.appendChild(infoDiv);
}
}
})
.catch(err => {
if (retriesLeft > 0) {
setTimeout(() => {
loadData(retriesLeft - 1);
}, CONFIG.retryDelay);
} else {
console.error(`帖子 ${topicId} 预览加载失败:`, err);
}
});
}
loadData(CONFIG.retryCount);
}
const topicLinks = Array.from(document.querySelectorAll('a.user-title'));
// 限速请求队列处理
function processQueue() {
if (topicLinks.length === 0) return;
const batch = topicLinks.splice(0, CONFIG.rateLimitCount);
batch.forEach(link => processTopic(link));
if (topicLinks.length > 0) {
setTimeout(processQueue, CONFIG.rateLimitInterval);
}
}
processQueue();
});
</script>