Jump to content

Widget:UPDEvents: Difference between revisions

From Iskomunidad
No edit summary
No edit summary
 
(4 intermediate revisions by the same user not shown)
Line 41: Line 41:
(function () {
(function () {
   var container = document.getElementById('upd-events-widget');
   var container = document.getElementById('upd-events-widget');
   if (!container || container.dataset.loaded === '1') return;
 
   if (!container || container.dataset.loaded === '1') {
    return;
  }
 
   container.dataset.loaded = '1';
   container.dataset.loaded = '1';


   var loading = container.querySelector('.upd-events-loading');
   var loading = container.querySelector('.upd-events-loading');
   var list = container.querySelector('.upd-events-list');
   var list = container.querySelector('.upd-events-list');
  loading.textContent =
    'Widget JavaScript is running. Requesting UP Diliman...';


   var endpoint = new URL(
   var endpoint = new URL(
Line 63: Line 70:
     endpoint.searchParams.set(key, params[key]);
     endpoint.searchParams.set(key, params[key]);
   });
   });
  var finished = false;
  var timeout = setTimeout(function () {
    if (finished) return;
    finished = true;
    loading.textContent =
      'The UP Diliman request timed out or is blocked.';
  }, 10000);


   fetch(endpoint.toString(), {
   fetch(endpoint.toString(), {
Line 70: Line 87:
   })
   })
   .then(function (response) {
   .then(function (response) {
    if (finished) return null;
     if (!response.ok) {
     if (!response.ok) {
       throw new Error('HTTP ' + response.status);
       throw new Error('HTTP ' + response.status);
     }
     }
     return response.text();
     return response.text();
   })
   })
   .then(function (html) {
   .then(function (html) {
    if (finished || html === null) return;
    finished = true;
    clearTimeout(timeout);
     var doc = new DOMParser().parseFromString(html, 'text/html');
     var doc = new DOMParser().parseFromString(html, 'text/html');


Line 94: Line 119:


       try {
       try {
         url = new URL(anchor.getAttribute('href'), 'https://upd.edu.ph').href;
         url = new URL(
          anchor.getAttribute('href'),
          'https://upd.edu.ph'
        ).href;
       } catch (e) {
       } catch (e) {
         return;
         return;
Line 100: Line 128:


       if (!title || seen.has(url)) return;
       if (!title || seen.has(url)) return;
       if (!/^https:\/\/upd\.edu\.ph\/events\//i.test(url)) return;
 
       if (!/^https:\/\/upd\.edu\.ph\/events\//i.test(url)) {
        return;
      }


       seen.add(url);
       seen.add(url);
       events.push({ title: title, url: url });
       events.push({
        title: title,
        url: url
      });
     });
     });


     list.replaceChildren();
     list.replaceChildren();
    if (events.length === 0) {
      loading.textContent =
        'The request succeeded, but no event titles were found.';
      return;
    }


     events.slice(0, 5).forEach(function (event) {
     events.slice(0, 5).forEach(function (event) {
Line 122: Line 163:


     loading.style.display = 'none';
     loading.style.display = 'none';
    if (events.length === 0) {
      var empty = document.createElement('div');
      empty.className = 'upd-events-empty';
      empty.textContent = 'No upcoming events were found.';
      container.appendChild(empty);
      return;
    }
     list.style.display = 'block';
     list.style.display = 'block';
   })
   })
   .catch(function (error) {
   .catch(function (error) {
    if (finished) return;
    finished = true;
    clearTimeout(timeout);
     console.error('UPEvents widget error:', error);
     console.error('UPEvents widget error:', error);
     loading.textContent =
     loading.textContent =
       'Unable to load events automatically. Visit the ';
       'Request failed: ' +
 
      error.name + ' — ' + error.message;
    var link = document.createElement('a');
    link.href = 'https://upd.edu.ph/events-list/';
    link.textContent = 'UP Diliman Events List';
    link.target = '_blank';
    link.rel = 'noopener noreferrer';
 
    loading.appendChild(link);
    loading.appendChild(document.createTextNode(' for current listings.'));
   });
   });
})();
})();
</script>
</script>
</includeonly>
</includeonly>

Latest revision as of 03:24, 29 October 2026