MediaWiki:Timeless.js: Difference between revisions

From The HOA
No edit summary
No edit summary
Line 1: Line 1:
mw.loader.using(['mediawiki.api']).then(function () {
mw.loader.using(['mediawiki.api']).then(function () {
     var category = "Resources"; // The name of the category
     var category = "Resources"; // The name of the category
     var sidebar = $(".sidebar-chunk"); // Targeting the sidebar using the class
     var sidebar = $(".sidebar-chunk").first(); // Target the first sidebar-chunk


     if (sidebar.length === 0) {
     if (sidebar.length === 0) {
Line 30: Line 30:
             $menuBlock.append($heading).append($list);
             $menuBlock.append($heading).append($list);


             // Append the new block to the sidebar directly
             // Add the new block at the very top of the sidebar
             sidebar.append($menuBlock); // Ensure it's added outside of "site-tools"
             sidebar.prepend($menuBlock); // This forces the menu block to appear independently


             console.log("Menu added to sidebar.");
             console.log("Menu added to sidebar.");

Revision as of 07:05, 7 March 2025

mw.loader.using(['mediawiki.api']).then(function () {
    var category = "Resources"; // The name of the category
    var sidebar = $(".sidebar-chunk").first(); // Target the first sidebar-chunk

    if (sidebar.length === 0) {
        console.error("Sidebar container not found");
        return;
    }

    console.log("Sidebar container found, loading menu...");

    // Create a new block for the menu (with a unique ID)
    var $menuBlock = $("<div>").addClass("portal").attr("id", "custom-category-menu");
    var $heading = $("<h3>").addClass("portal-heading").text("Pages in " + category);
    var $list = $("<ul>").addClass("portal-body");

    new mw.Api().get({
        action: "query",
        list: "categorymembers",
        cmtitle: "Category:" + category,
        cmlimit: 10,
        format: "json"
    }).done(function (data) {
        if (data.query && data.query.categorymembers.length > 0) {
            data.query.categorymembers.forEach(function (page) {
                $list.append($("<li>").append($("<a>").attr("href", mw.util.getUrl(page.title)).text(page.title)));
            });

            // Append the heading and list to the new block
            $menuBlock.append($heading).append($list);

            // Add the new block at the very top of the sidebar
            sidebar.prepend($menuBlock); // This forces the menu block to appear independently

            console.log("Menu added to sidebar.");
        } else {
            console.log("No pages found in the category.");
        }
    }).fail(function () {
        console.error("API request failed.");
    });
});