Widget:CommentsRSS: Difference between revisions
From Jcastle.info
No edit summary |
No edit summary |
||
Line 20: | Line 20: | ||
const maxItems = 5; | const maxItems = 5; | ||
container.innerHTML = " | container.innerHTML = "<strong>Recent Comments</strong>"; | ||
Array.from(items).slice(0, maxItems).forEach(item => { | Array.from(items).slice(0, maxItems).forEach(item => { | ||
Line 49: | Line 38: | ||
container.appendChild(entry); | container.appendChild(entry); | ||
}); | }); | ||
const moreLink = document.createElement("div"); | |||
moreLink.innerHTML = `<a href='/view/Special:RecentComments'>More comments...</a>`; | |||
moreLink.style.marginTop = "0.5em"; | |||
container.appendChild(moreLink); | |||
const subscribeLink = document.createElement("div"); | |||
subscribeLink.innerHTML = `<a href='/view/Subscribe_To_Comments' target='_blank'><i class='fas fa-envelope'></i> Subscribe to comments by email</a>`; | |||
subscribeLink.style.marginTop = "0.25em"; | |||
container.appendChild(subscribeLink); | |||
}) | }) | ||
.catch(error => { | .catch(error => { |
Revision as of 18:34, 1 June 2025
<script> function truncate(text, limit) {
if (text.length <= limit) return text; return text.slice(0, text.lastIndexOf(" ", limit)) + "…";
}
fetch('/view/Special:CommentsRSS')
.then(res => res.text()) .then(str => (new window.DOMParser()).parseFromString(str, "text/xml")) .then(data => { const items = data.querySelectorAll("item"); const container = document.getElementById("recent-comments");
if (items.length === 0) {
container.innerHTML = "
No recent comments.
";
return; }
const maxItems = 5; container.innerHTML = "Recent Comments";
Array.from(items).slice(0, maxItems).forEach(item => { const title = item.querySelector("title").textContent; const link = item.querySelector("link").textContent; const rawDesc = item.querySelector("description").textContent; const commentText = rawDesc.replace(/<br\s*\/?>/gi, '\n').replace(/<[^>]+>/g, ).trim(); const date = new Date(item.querySelector("pubDate").textContent);
const entry = document.createElement("div"); entry.style.marginBottom = "1em"; entry.innerHTML = `
<a href="${link}" target="_blank">${truncate(commentText, 200)}</a>
${date.toLocaleDateString(undefined, { year: 'numeric', month: 'short', day: 'numeric' })} `;
container.appendChild(entry); });
const moreLink = document.createElement("div"); moreLink.innerHTML = `<a href='/view/Special:RecentComments'>More comments...</a>`; moreLink.style.marginTop = "0.5em"; container.appendChild(moreLink);
const subscribeLink = document.createElement("div"); subscribeLink.innerHTML = `<a href='/view/Subscribe_To_Comments' target='_blank'> Subscribe to comments by email</a>`; subscribeLink.style.marginTop = "0.25em"; container.appendChild(subscribeLink); }) .catch(error => {
document.getElementById("recent-comments").innerHTML = "
Error loading comments.
";
console.error("RSS Load Error:", error); });
</script>