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 = "All_pages"; // Try using a category with known pages for debugging
     var category = "Resources"; // The name of the category
     var sidebar = $("#mp-upper"); // Targeting the left sidebar in Timeless
     var sidebar = $(".sidebar-chunk"); // Targeting the sidebar using the class


    // Log if the sidebar is found
     if (sidebar.length === 0) {
     if (sidebar.length === 0) {
         console.error("Sidebar container not found");
         console.error("Sidebar container not found");
Line 11: Line 10:
     console.log("Sidebar container found, loading menu...");
     console.log("Sidebar container found, loading menu...");


    // Create a new menu using Timeless' built-in styling classes
     var $menu = $("<div>").addClass("portal").attr("id", "custom-category-menu");
     var $menu = $("<div>").addClass("portal").attr("id", "custom-category-menu"); // Using Timeless' portal class
     var $heading = $("<h3>").addClass("portal-heading").text("Pages in " + category);
     var $heading = $("<h3>").addClass("portal-heading").text("Pages in " + category);
     var $list = $("<ul>").addClass("portal-body");
     var $list = $("<ul>").addClass("portal-body");


    // Fetch category members from the API
     new mw.Api().get({
     new mw.Api().get({
         action: "query",
         action: "query",
Line 25: Line 22:
     }).done(function (data) {
     }).done(function (data) {
         if (data.query && data.query.categorymembers.length > 0) {
         if (data.query && data.query.categorymembers.length > 0) {
            console.log("Category members found:", data.query.categorymembers);
             data.query.categorymembers.forEach(function (page) {
             data.query.categorymembers.forEach(function (page) {
                 $list.append($("<li>").append($("<a>").attr("href", mw.util.getUrl(page.title)).text(page.title)));
                 $list.append($("<li>").append($("<a>").attr("href", mw.util.getUrl(page.title)).text(page.title)));
Line 31: Line 27:


             $menu.append($heading).append($list);
             $menu.append($heading).append($list);
             sidebar.append($menu); // Append the menu to the Timeless left sidebar
             sidebar.append($menu); // Append the menu to the sidebar


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

Revision as of 07:02, 7 March 2025

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

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

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

    var $menu = $("<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)));
            });

            $menu.append($heading).append($list);
            sidebar.append($menu); // Append the menu to the sidebar

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