52,186
edits
Marblemadmax (talk | contribs) mNo edit summary |
Marblemadmax (talk | contribs) mNo edit summary |
||
(2 intermediate revisions by the same user not shown) | |||
Line 60: | Line 60: | ||
}; | }; | ||
function isAvailable(critter, day, hour) { | function isAvailable(critter, day, hour) { | ||
const | const availability = critters[critter][day]; | ||
if (! | if (!availability) return false; // 'n/a' or undefined | ||
if ( | if (availability === 'All day') return true; | ||
const [start, end] = | const [start, end] = availability.split(' to '); | ||
const startHour = parseHour(start); | const startHour = parseHour(start); | ||
const endHour = parseHour(end); | const endHour = parseHour(end); | ||
Line 71: | Line 70: | ||
} | } | ||
function parseHour(timeStr) { | function parseHour(timeStr) { | ||
const [time, period] = timeStr.split(' '); | const [time, period] = timeStr.split(' '); | ||
Line 81: | Line 79: | ||
} | } | ||
function updateCritterList() { | function updateCritterList() { | ||
const now = new Date(); | const now = new Date(); | ||
const dayOfWeek = now.toLocaleString('en-US', { weekday: 'long' }); | const dayOfWeek = now.toLocaleString('en-US', { weekday: 'long' }); | ||
const hour = now.getHours(); | const hour = now.getHours(); | ||
let listContent = ''; | |||
Object.entries(critters).forEach(([critterName, _]) => { | |||
if (isAvailable(critterName, dayOfWeek, hour)) { | if (isAvailable(critterName, dayOfWeek, hour)) { | ||
listContent += ` | listContent += `{{name|${critterName}}}<br>`; | ||
} | } | ||
} | }); | ||
parseContent(listContent); | |||
} | |||
function parseContent(wikitext) { | |||
const apiEndpoint = "/api.php"; | |||
const params = new URLSearchParams({ | |||
action: 'parse', | |||
format: 'json', | |||
contentmodel: 'wikitext', | |||
text: wikitext, | |||
prop: 'text' | |||
}); | |||
fetch(apiEndpoint + "?" + params.toString(), { | |||
method: 'POST', | |||
credentials: 'same-origin' | |||
} | }) | ||
.then(response => response.json()) | |||
.then(data => { | |||
const parsedHtml = data.parse.text['*']; | |||
document.getElementById('critterList').innerHTML = parsedHtml; | |||
}) | |||
.catch(error => console.error('Error parsing content:', error)); | |||
} | } | ||
window.addEventListener('load', function() { | window.addEventListener('load', function() { | ||
updateCritterList(); // | updateCritterList(); // Initial update | ||
setInterval(updateCritterList, 60000); // | setInterval(updateCritterList, 60000); // Refresh every minute | ||
}); | }); | ||
</script> | </script> |