Widget:UPDEvents: Difference between revisions
Appearance
No edit summary Tag: Reverted |
No edit summary |
||
| (3 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'; | ||
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 = | ||
' | 'Request failed: ' + | ||
error.name + ' — ' + error.message; | |||
}); | }); | ||
})(); | })(); | ||
</script> | </script> | ||
</includeonly> | </includeonly> | ||