|
|
| Line 1: |
Line 1: |
| <includeonly> | | <includeonly> |
| <div id="upd-events-widget"> | | <div id="upd-events-widget"> |
| <div class="upd-events-loading"> | | <div class="upd-events-loading">Loading upcoming events...</div> |
| Loading upcoming events...
| |
| </div>
| |
| <ul class="upd-events-list" style="display:none;"></ul> | | <ul class="upd-events-list" style="display:none;"></ul> |
| </div> | | </div> |
| Line 14: |
Line 12: |
|
| |
|
| var loading = container.querySelector('.upd-events-loading'); | | var loading = container.querySelector('.upd-events-loading'); |
| var list = container.querySelector('.upd-events-list');
| |
| var pageUrl = 'https://upd.edu.ph/events-list/'; | | var pageUrl = 'https://upd.edu.ph/events-list/'; |
|
| |
|
| function showError() { | | loading.textContent = 'Testing connection to UP Diliman...'; |
| loading.textContent = 'Unable to load events automatically. ';
| |
| var link = document.createElement('a');
| |
| link.href = pageUrl;
| |
| link.textContent = 'View all UP Diliman events';
| |
| link.target = '_blank';
| |
| link.rel = 'noopener noreferrer';
| |
| loading.appendChild(link);
| |
| }
| |
|
| |
|
| function parseEventDate(text) { | | fetch(pageUrl) |
| var match = text.match( | | .then(function (response) { |
| /\b(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z]*\s+(\d{1,2})\s*\|\s*(\d{1,2}):(\d{2})\s*(AM|PM)/i | | if (!response.ok) { |
| );
| | throw new Error('HTTP ' + response.status); |
| | } |
| | 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/"]' |
| | ); |
|
| |
|
| if (!match) return null;
| | loading.textContent = |
| | | 'Page received (' + html.length + |
| var months = {
| | ' characters). Event links found: ' + anchors.length; |
| jan:0, feb:1, mar:2, apr:3, may:4, jun:5,
| | }) |
| jul:6, aug:7, sep:8, oct:9, nov:10, dec:11 | | .catch(function (error) { |
| };
| | loading.textContent = 'Connection failed: ' + error.message; |
| | | console.error('UPEvents diagnostic:', error); |
| var now = new Date();
| | }); |
| var month = months[match[1].slice(0, 3).toLowerCase()];
| |
| var day = Number(match[2]);
| |
| var hour = Number(match[3]) % 12;
| |
| var minute = Number(match[4]);
| |
| | |
| if (match[5].toUpperCase() === 'PM') hour += 12;
| |
| | |
| var year = now.getFullYear();
| |
| var eventDate = new Date(year, month, day, hour, minute);
| |
| | |
| // Handle year boundaries.
| |
| if (month === 0 && now.getMonth() === 11) {
| |
| eventDate.setFullYear(year + 1);
| |
| } else if (month === 11 && now.getMonth() === 0) {
| |
| eventDate.setFullYear(year - 1);
| |
| }
| |
| | |
| return eventDate;
| |
| }
| |
| | |
|
| |
| loading.textContent = 'Testing connection to UP Diliman...'; | |
| | |
| fetch(pageUrl)
| |
| .then(function (response) {
| |
| loading.textContent =
| |
| 'Connection response: HTTP ' + response.status +
| |
| '. Reading event list...';
| |
| | |
| if (!response.ok) {
| |
| throw new Error('HTTP ' + response.status);
| |
| }
| |
| | |
| return response.text();
| |
| })
| |
| .then(function (html) {
| |
| loading.textContent =
| |
| 'Page received (' + html.length +
| |
| ' characters). Checking event markup...';
| |
| | |
| var doc = new DOMParser().parseFromString(html, 'text/html');
| |
| | |
| var anchors = doc.querySelectorAll(
| |
| '#em-events-list-grouped-1 a[href*="/events/"]'
| |
| );
| |
| | |
| loading.textContent =
| |
| 'Page received. Event links found: ' + anchors.length;
| |
| })
| |
| .catch(function (error) {
| |
| loading.textContent =
| |
| 'Connection failed: ' + error.message;
| |
| console.error('UPEvents diagnostic:', error);
| |
| });
| |
| | |
| | |
| .catch(function (error) {
| |
| console.error('UPEvents widget error:', error);
| |
| loading.textContent =
| |
| 'Fetch error: ' + error.message +
| |
| '. Check the browser Console for details.';
| |
| }); | |
| ); | |
| })(); | | })(); |
| </script> | | </script> |
| </includeonly> | | </includeonly> |