Jump to content

Widget:UPDEvents: Difference between revisions

From Iskomunidad
No edit summary
No edit summary
Line 60: Line 60:
   }
   }


   fetch(pageUrl)
    
    .then(function (response) {
loading.textContent = 'Testing connection to UP Diliman...';
      if (!response.ok) throw new Error('Unable to retrieve events page');
      return response.text();
    })
    .then(function (html) {
      var doc = new DOMParser().parseFromString(html, 'text/html');
      var anchors = doc.querySelectorAll(
        '#em-events-list-grouped-1 a[href*="/events/"]'
      );


       var events = [];
fetch(pageUrl)
       var seen = {};
  .then(function (response) {
    loading.textContent =
       'Connection response: HTTP ' + response.status +
       '. Reading event list...';


      anchors.forEach(function (anchor) {
    if (!response.ok) {
        var heading = anchor.querySelector('h4.d-teal.fw-700');
      throw new Error('HTTP ' + response.status);
        var dateText = anchor.querySelector('li.list-inline-item');
    }


        if (!heading || !dateText) return;
    return response.text();
  })
  .then(function (html) {
    loading.textContent =
      'Page received (' + html.length +
      ' characters). Checking event markup...';


        var title = heading.textContent.trim();
    var doc = new DOMParser().parseFromString(html, 'text/html');
        var date = parseEventDate(dateText.textContent);
        var url = new URL(anchor.getAttribute('href'), pageUrl).href;


        if (!title || !date || date <= new Date()) return;
    var anchors = doc.querySelectorAll(
        if (seen[url]) return;
      '#em-events-list-grouped-1 a[href*="/events/"]'
    );


        seen[url] = true;
    loading.textContent =
        events.push({
      'Page received. Event links found: ' + anchors.length;
          title: title,
  })
          url: url,
  .catch(function (error) {
          date: date
    loading.textContent =
        });
      'Connection failed: ' + error.message;
      });
    console.error('UPEvents diagnostic:', error);
  });


      events.sort(function (a, b) {
        return a.date - b.date;
      });
      events = events.slice(0, 5);
      if (!events.length) {
        loading.textContent = 'No upcoming events were found.';
        return;
      }
      events.forEach(function (event) {
        var item = document.createElement('li');
        var link = document.createElement('a');
        link.href = event.url;
        link.textContent = event.title;
        link.target = '_blank';
        link.rel = 'noopener noreferrer';
        item.appendChild(link);
        list.appendChild(item);
      });
      loading.style.display = 'none';
      list.style.display = 'block';
    })
      
      
.catch(function (error) {
.catch(function (error) {

Revision as of 02:28, 29 October 2026