Widget:CommentsRSS: Difference between revisions

From Jcastle.info
No edit summary
No edit summary
Line 16: Line 16:
     let html = "";
     let html = "";


     items.forEach(item => {
     items.forEach((item, index) => {
      if (index >= 5) return; // ⛔ stop after 5 items
       const titleText = item.querySelector("title").textContent;
       const titleText = item.querySelector("title").textContent;
       const link = item.querySelector("link").textContent;
       const link = item.querySelector("link").textContent;

Revision as of 23:57, 26 May 2025

<script> fetch('/smw139/resources/assets/comments_feed.xml')

 .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;
   }
   let html = "";
   items.forEach((item, index) => {
     if (index >= 5) return; // ⛔ stop after 5 items
     const titleText = item.querySelector("title").textContent;
     const link = item.querySelector("link").textContent;
     const commentText = item.querySelector("description").textContent;
     const pubDate = new Date(item.querySelector("pubDate").textContent);
     const match = titleText.match(/^(.*?) commented on (.*?)$/);
     const actor = match ? match[1] : 'User';
     const pageTitle = match ? match[2] : 'a page';
     html += `
           ${actor} commented on 
           <a href="${link}" target="_blank">${pageTitle}</a>
            (${pubDate.toLocaleDateString(undefined, { year: 'numeric', month: 'short', day: 'numeric' })})
${commentText}
     `;
   });

html += `

      <a href="/view/Subscribe_To_Comments" target="_blank"> Subscribe to comments by email</a>

`;

   container.innerHTML = html;
 })
 .catch(error => {

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

Error loading comments.

";

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

</script>