Jump to content

MediaWiki:Common.js: Difference between revisions

From Iskomunidad
No edit summary
No edit summary
Line 1: Line 1:
/* For Main Page Announcement area //this code pulls from the https://ilc.upd.edu.ph/all-posts-and-announcements/ PAGE */
document.addEventListener("DOMContentLoaded", async () => {
document.addEventListener("DOMContentLoaded", async () => {
    const container = document.getElementById("ilc-announcements");


     // Stop if container does not exist
     const container =
        document.getElementById("ilc-announcements");
 
     if (!container) return;
     if (!container) return;


     try {
     try {
        const url = "https://ilc.upd.edu.ph/wp-json/wp/v2/posts?per_page=5";


         const res = await fetch(url);
         const response = await fetch(
         const posts = await res.json();
            "https://ilc.upd.edu.ph/wp-json/wp/v2/posts?per_page=5",
            {
                method: "GET",
                mode: "cors"
            }
        );
 
         const posts = await response.json();
 
        let html = `
            <div class="ilc-announcement-list">
                <h2>Announcements</h2>
        `;
 
        posts.forEach(post => {
 
            html += `
                <div class="ilc-announcement-item">
                    <h3>
                        <a href="${post.link}" target="_blank">
                            ${post.title.rendered}
                        </a>
                    </h3>
                </div>
            `;
        });
 
        html += `</div>`;
 
        container.innerHTML = html;
 
    } catch (error) {


         container.innerHTML = posts.map(post => `
         console.error(error);
            <div>
                <a href="${post.link}" target="_blank">
                    ${post.title.rendered}
                </a>
            </div>
        `).join("");


    } catch (err) {
         container.innerHTML =
         container.innerHTML = "Failed to load announcements.";
            "<p>Failed to load announcements.</p>";
        console.error(err);
     }
     }
});
});

Revision as of 05:39, 10 June 2026

/* For Main Page Announcement area //this code pulls from the https://ilc.upd.edu.ph/all-posts-and-announcements/ PAGE */
document.addEventListener("DOMContentLoaded", async () => {

    const container =
        document.getElementById("ilc-announcements");

    if (!container) return;

    try {

        const response = await fetch(
            "https://ilc.upd.edu.ph/wp-json/wp/v2/posts?per_page=5",
            {
                method: "GET",
                mode: "cors"
            }
        );

        const posts = await response.json();

        let html = `
            <div class="ilc-announcement-list">
                <h2>Announcements</h2>
        `;

        posts.forEach(post => {

            html += `
                <div class="ilc-announcement-item">
                    <h3>
                        <a href="${post.link}" target="_blank">
                            ${post.title.rendered}
                        </a>
                    </h3>
                </div>
            `;
        });

        html += `</div>`;

        container.innerHTML = html;

    } catch (error) {

        console.error(error);

        container.innerHTML =
            "<p>Failed to load announcements.</p>";
    }
});
/*end of new code */

/* Any JavaScript here will be loaded for all users on every page load. */
$(window).on('load', function(){
	$('#site-tools ul').append("<li><a href='https://iskwiki.upd.edu.ph' target='_blank'>Flipbook</li>");
});