Widget:CommentsRSS: Difference between revisions

From Jcastle.info
(Created page with "<div id="recent-comments" style="font-size: 0.9em;"></div> <script> 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 = "<p>No recent comments.</p>"; return; } container.innerHTML = "...")
 
No edit summary
Line 2: Line 2:


<script>
<script>
function truncate(text, limit) {
  if (text.length <= limit) return text;
  return text.slice(0, text.lastIndexOf(" ", limit)) + "…";
}
fetch('/view/Special:CommentsRSS')
fetch('/view/Special:CommentsRSS')
   .then(res => res.text())
   .then(res => res.text())
Line 18: Line 23:
       const title = item.querySelector("title").textContent;
       const title = item.querySelector("title").textContent;
       const link = item.querySelector("link").textContent;
       const link = item.querySelector("link").textContent;
       const desc = item.querySelector("description").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 date = new Date(item.querySelector("pubDate").textContent);


Line 24: Line 30:
       entry.style.marginBottom = "1em";
       entry.style.marginBottom = "1em";
       entry.innerHTML = `
       entry.innerHTML = `
         <a href="${link}" target="_blank" style="font-weight: bold;">${title}</a><br>
         <div><a href="${link}" target="_blank">${truncate(commentText, 200)}</a></div>
         <span style="color: #555;">${desc}</span><br>
         <small style="color: #555;">${date.toLocaleDateString(undefined, { year: 'numeric', month: 'short', day: 'numeric' })}</small>
        <small>${date.toLocaleDateString(undefined, { year: 'numeric', month: 'short', day: 'numeric' })}</small>
       `;
       `;



Revision as of 20:56, 25 May 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;
   }

container.innerHTML = "

Recent Comments

";

   items.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);
   });
 })
 .catch(error => {

document.getElementById("recent-comments").innerHTML = "

Error loading comments.

";

   console.error("RSS Load Error:", error);
 });

</script>