Widget:CommentsRSS: Difference between revisions

From Jcastle.info
No edit summary
No edit summary
 
(11 intermediate revisions by the same user not shown)
Line 2: Line 2:


<script>
<script>
fetch('/smw139/resources/assets/comments_feed.xml')
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(res => res.text())
   .then(str => (new window.DOMParser()).parseFromString(str, "text/xml"))
   .then(str => (new window.DOMParser()).parseFromString(str, "text/xml"))
Line 14: Line 19:
     }
     }


     let html = "";
     const maxItems = 5;
//    container.innerHTML = "<strong>Recent Comments</strong>";


     items.forEach(item => {
     Array.from(items).slice(0, maxItems).forEach(item => {
       const titleText = item.querySelector("title").textContent;
       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 rawDesc = item.querySelector("description").textContent;
      const commentText = rawDesc.replace(/<br\s*\/?>/gi, '\n').replace(/<[^>]+>/g, '').trim();
       const pubDate = new Date(item.querySelector("pubDate").textContent);
       const pubDate = new Date(item.querySelector("pubDate").textContent);


       const match = titleText.match(/^(.*?) commented on (.*?)$/);
       const match = titleText.match(/^(.+?) commented on (.+)$/);
       const actor = match ? match[1] : 'User';
       const actor = match ? match[1] : "Anonymous";
       const pageTitle = match ? match[2] : 'a page';
       const pageTitle = match ? match[2] : "(unknown)";


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


     html += `<div style="font-size: 0.85em; margin-top: 1em;">
     const moreLink = document.createElement("div");
      <a href="/view/Subscribe_To_Comments" target="_blank"><i class="fas fa-envelope"></i> Subscribe to comments by email</a>
    moreLink.innerHTML = `<a href='/view/Special:RecentComments'><i class='fas fa-comments'></i> More comments...</a>`;
     </div>`;
     moreLink.style.marginTop = "0.5em";
    container.appendChild(moreLink);


     container.innerHTML = html;
     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 => {

Latest revision as of 18:47, 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 titleText = 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 pubDate = new Date(item.querySelector("pubDate").textContent);
     const match = titleText.match(/^(.+?) commented on (.+)$/);
     const actor = match ? match[1] : "Anonymous";
     const pageTitle = match ? match[2] : "(unknown)";
     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' })})
${truncate(commentText, 200)}
     `;
     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>