Widget:CommentsRSS: Difference between revisions

From Jcastle.info
No edit summary
No edit summary
Line 14: Line 14:
     }
     }


     container.innerHTML = "";
     container.innerHTML = "<h3>Recent Comments</h3>";
     items.forEach(item => {
     items.forEach(item => {
      const titleText = item.querySelector("title").textContent;
       const link = item.querySelector("link").textContent;
       const link = item.querySelector("link").textContent;
       const commentText = item.querySelector("description").textContent;
       const commentText = item.querySelector("description").textContent;
       const date = new Date(item.querySelector("pubDate").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';


       const entry = document.createElement("div");
       const entry = document.createElement("div");
       entry.style.marginBottom = "1em";
       entry.style.marginBottom = "1em";
       entry.innerHTML = `
       entry.innerHTML = `
         <div><a href="${link}" target="_blank">${commentText}</a></div>
         <div>
        <small style="color: #555;">${date.toLocaleDateString(undefined, { year: 'numeric', month: 'short', day: 'numeric' })}</small>
          <strong>${actor}</strong> commented on
          <a href="${link}" target="_blank">${pageTitle}</a>
          <small style="color: #555;"> (${pubDate.toLocaleDateString(undefined, { year: 'numeric', month: 'short', day: 'numeric' })})</small>
        </div>
        <div style="margin-top: 0.25em;">${commentText}</div>
       `;
       `;



Revision as of 21:35, 25 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;
   }

container.innerHTML = "

Recent Comments

";

   items.forEach(item => {
     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';
     const entry = document.createElement("div");
     entry.style.marginBottom = "1em";
     entry.innerHTML = `
         ${actor} commented on 
         <a href="${link}" target="_blank">${pageTitle}</a>
          (${pubDate.toLocaleDateString(undefined, { year: 'numeric', month: 'short', day: 'numeric' })})
${commentText}
     `;
     container.appendChild(entry);
   });
 })
 .catch(error => {

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

Error loading comments.

";

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

</script>