Widget:AvailableCritters: Difference between revisions

m
no edit summary
mNo edit summary
mNo edit summary
Line 60: Line 60:
};
};


    // Function to check if a critter is available
    function isAvailable(critter, day, hour) {
        const times = critters[critter][day];
        if (!times) return false; // 'n/a' or undefined
        if (times === 'All day') return true;
        const [start, end] = times.split(' to ');
        const startHour = parseHour(start);
        const endHour = parseHour(end);
        return hour >= startHour && hour < endHour;
    }
    // Helper function to parse hour string to 24-hour format number
    function parseHour(timeStr) {
        const [time, period] = timeStr.split(' ');
        let [hours, minutes] = time.split(':');
        hours = parseInt(hours);
        if (period === 'PM' && hours !== 12) hours += 12;
        if (period === 'AM' && hours === 12) hours = 0;
        return hours;
    }
    // Function to update the critter list based on the current time
     function updateCritterList() {
     function updateCritterList() {
         const now = new Date();
         const now = new Date();
         const dayOfWeek = now.toLocaleString('en-US', { weekday: 'long' }); // Get day name
         const dayOfWeek = now.toLocaleString('en-US', { weekday: 'long' });
         const hour = now.getHours(); // Get current hour in 24-hour format
         const hour = now.getHours();
        let listContent = '';


         let listContent = '';
         Object.entries(critters).forEach(([critterName, _]) => {
        for (const [critterName, _] of Object.entries(critters)) {
             if (isAvailable(critterName, dayOfWeek, hour)) {
             if (isAvailable(critterName, dayOfWeek, hour)) {
                 listContent += `<p>{{name|${critterName}}}</p>`;
                 listContent += `{{name|${critterName}}}<br>`;
             }
             }
         }
         });
 
        parseContent(listContent);
    }
 
    function parseContent(wikitext) {
        const apiEndpoint = "/w/api.php";
        const params = new URLSearchParams({
            action: 'parse',
            format: 'json',
            contentmodel: 'wikitext',
            text: wikitext,
            prop: 'text'
        });


         const listContainer = document.getElementById('critterList');
         fetch(apiEndpoint + "?" + params.toString(), {
         if (listContainer) {
            method: 'POST',
             listContainer.innerHTML = listContent;
            credentials: 'same-origin',
         } else {
        })
            console.log('List container not found');
         .then(response => response.json())
        }
        .then(data => {
             const parsedHtml = data.parse.text['*'];
            document.getElementById('critterList').innerHTML = parsedHtml;
         })
        .catch(error => console.error('Error parsing content:', error));
     }
     }


    // Ensure the script runs after the page has loaded
     window.addEventListener('load', function() {
     window.addEventListener('load', function() {
         updateCritterList(); // Update the list immediately upon loading
         updateCritterList(); // Initial update
         setInterval(updateCritterList, 60000); // Update the list every minute
         setInterval(updateCritterList, 60000); // Refresh every minute
     });
     });
</script>
</script>