代码拉取完成,页面将自动刷新
<!-- 博客嵌入 Mastodon 嘟嘟 -->
<div id="toots"></div>
<button id="toots-moreButton" onclick="tootsShowMore()">更多</button>
<script>
let maxId = null; // 初始值为 null,表示第一页
const tootsDiv = document.getElementById('toots');
const tootsMoreButton = document.getElementById('toots-moreButton');
// 获取 Mastodon 用户公开Toots 限制条数 默认5 排除回复 toot
async function getPublicToots() {
const queryParams = maxId ? ("?limit={{ .Get 2 | default 5 }}&max_id=" + maxId) : "?limit={{ .Get 2 | default 5 }}";
const response = await fetch("{{ .Get 0 }}/api/v1/accounts/{{ .Get 1 }}/statuses" + queryParams + "&exclude_replies=true");
const toots = await response.json();
return toots;
}
// 将Toots显示在页面上
async function displayToots() {
try {
const toots = await getPublicToots();
if (toots && toots.length > 0) {
toots.forEach(toot => {
const tootDiv = document.createElement('div');
tootDiv.innerHTML = `<p>${toot.content}</p><hr>`;
tootsDiv.appendChild(tootDiv);
maxId = toot.id; // 更新最大 ID
});
tootsMoreButton.style.display = 'block';
} else {
tootsMoreButton.style.display = 'none';
}
} catch (error) {
console.error('获取Toots时出错:', error);
tootsMoreButton.style.display = 'none';
}
}
function tootsShowMore() {
displayToots();
}
displayToots();
// 页面加载时调用显示Toots函数
// window.onload = displayToots;
</script>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。