MediaWiki:Common.js: Difference between revisions

From Dragon Ball World Wiki
Jump to navigation Jump to search
(Created page with "→‎Any JavaScript here will be loaded for all users on every page load.: // JavaScript to dynamically create and insert a table into a MediaWiki page (function () { // Check if the page content area exists const contentArea = document.getElementById("mw-content-text"); if (!contentArea) return; // Create table element const table = document.createElement("table"); table.className = "wikitable"; // MediaWiki styling class table.style.width = "50%"; tabl...")
 
No edit summary
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
mw.loader.using(["mediawiki.util"]).then(function () {
// JavaScript to dynamically create and insert a table into a MediaWiki page
  (function () {
(function () {
    var targetPage = "Meals"; // Target page name
  // Check if the page content area exists
    var currentPage = mw.config.get("wgPageName");
  const contentArea = document.getElementById("mw-content-text");
  if (!contentArea) return;


  // Create table element
    // Run the script only if the current page matches the target page
  const table = document.createElement("table");
    if (currentPage !== targetPage) {
  table.className = "wikitable"; // MediaWiki styling class
      return;
  table.style.width = "50%";
    }
  table.style.margin = "20px auto";


  // Add table header
    // Check if the content area exists
  const headers = ["Name", "Age", "City"];
    var contentArea = document.getElementById("mw-content-text");
  const headerRow = document.createElement("tr");
    if (!contentArea) {
  headers.forEach((header) => {
      return;
    const th = document.createElement("th");
     }
    th.textContent = header;
     headerRow.appendChild(th);
  });
  table.appendChild(headerRow);


  // Add table rows with random data
    // Create a table of contents
  const data = [
    var toc = document.createElement("div");
     ["Alice", "25", "New York"],
     toc.className = "toc";
     ["Bob", "30", "Los Angeles"],
    toc.style.marginBottom = "20px";
     ["Charlie", "35", "Chicago"]
     toc.style.fontWeight = "bold";
  ];
     toc.innerHTML = "<h2>Contents</h2>";


  data.forEach((row) => {
    // Define the content list (title and section ids)
     const tableRow = document.createElement("tr");
    var contentList = [
     row.forEach((cell) => {
      { name: "Common Monsters", id: "common-monsters" },
       const td = document.createElement("td");
      { name: "Mini Boss Monsters", id: "mini-boss-monsters" },
       td.textContent = cell;
      { name: "Saga Monsters", id: "saga-monsters" },
      tableRow.appendChild(td);
      { name: "Maze Monsters", id: "maze-monsters" },
      { name: "Ancient Temple [100 - 169]", id: "ancient-temple" },
      { name: "Hellish Vanguard [100 - 169]", id: "hellish-vanguard" },
      { name: "Lost Graveyard [170 - 239]", id: "lost-graveyard" },
      { name: "Heaven Hills [170 - 239]", id: "heaven-hills" },
      { name: "Frozen Lair [240 - 299]", id: "frozen-lair" },
      { name: "Saiyan Bandits Hideout Siege [240 - 299]", id: "saiyan-bandits-hideout" },
      { name: "Curse of Pharaoh (20-39)", id: "curse-of-pharaoh" },
      { name: "Mafia Dungeon (40-59)", id: "mafia-dungeon" },
      { name: "Army Secret (60-99)", id: "army-secret" },
      { name: "Jungle Adventure (100-129)", id: "jungle-adventure" },
      { name: "Namek Dungeon (130-169)", id: "namek-dungeon" }
    ];
 
    // Create a list of links for the table of contents
    var tocList = document.createElement("ul");
    contentList.forEach(function (content) {
      var listItem = document.createElement("li");
      var link = document.createElement("a");
      link.href = "#" + content.id;
      link.textContent = content.name;
      listItem.appendChild(link);
      tocList.appendChild(listItem);
    });
    toc.appendChild(tocList);
 
    // Insert the table of contents at the top of the content area
    contentArea.prepend(toc);
 
    // Define the categories and their sample monsters
    var monsterCategories = {
      "common-monsters": [
        ["Slime", "Level 1-10", "Low health, basic attack"],
        ["Goblin", "Level 5-15", "Quick and nimble"],
        ["Wolf", "Level 10-20", "Can be found in packs"],
        ["Bandit", "Level 15-25", "Average strength, basic weapons"],
        ["Rat", "Level 1-5", "Weak, commonly found in caves"],
        ["Skeleton", "Level 20-30", "Basic undead with moderate strength"],
        ["Orc", "Level 25-35", "Strong and slow, uses heavy weapons"],
        ["Zombie", "Level 30-40", "Slow, but high health"],
        ["Imp", "Level 10-20", "Magic user with fire attacks"],
        ["Mummy", "Level 15-25", "Undead with poison attack"]
      ],
      "mini-boss-monsters": [
        ["Vampire Lord", "Level 50-60", "High health, drains life"],
        ["Dark Knight", "Level 45-55", "Powerful and armored"],
        ["Demon Beast", "Level 60-70", "Summons minions, strong attack"],
        ["Cyclops", "Level 50-60", "High damage, weak defense"],
        ["Werewolf", "Level 55-65", "Fast and aggressive"],
        ["Giant Spider", "Level 45-55", "Poisonous bite, high defense"],
        ["Dragon Whelp", "Level 65-75", "Breathes fire, tough scales"],
        ["Griffin", "Level 60-70", "Aerial monster with fast attacks"],
        ["Minotaur", "Level 70-80", "Brutal strength, large axe"],
        ["Basilisk", "Level 55-65", "Stone gaze and poison attack"]
      ],
      // Add more categories as needed
    };
 
    // Create a table to display the monsters
    var table = document.createElement("table");
    table.className = "wikitable"; // Apply MediaWiki's table styling
    table.style.width = "100%";
    table.style.margin = "20px auto";
 
    // Add table header
    var headers = ["Monster", "Level Range", "Description"];
     var headerRow = document.createElement("tr");
     headers.forEach(function (header) {
      var th = document.createElement("th");
      th.textContent = header;
      headerRow.appendChild(th);
    });
    table.appendChild(headerRow);
 
    // Add rows for each category with sample monsters
    Object.keys(monsterCategories).forEach(function (category) {
       // Add category header with link
      var sectionRow = document.createElement("tr");
      var sectionCell = document.createElement("td");
      sectionCell.colSpan = "3";
      var anchor = document.createElement("a");
      anchor.name = category;  // Create anchor link for this section
      sectionCell.appendChild(anchor);
      sectionCell.textContent = category.replace("-", " ").toUpperCase();
       sectionRow.appendChild(sectionCell);
      table.appendChild(sectionRow);
 
      // Add monsters for this category
      monsterCategories[category].forEach(function (monster) {
        var monsterRow = document.createElement("tr");
        monster.forEach(function (data) {
          var cell = document.createElement("td");
          cell.textContent = data;
          monsterRow.appendChild(cell);
        });
        table.appendChild(monsterRow);
      });
     });
     });
    table.appendChild(tableRow);
  });


  // Insert the table into the page content area
    // Insert the table after the table of contents
  contentArea.prepend(table); // Add table at the top of the page content
    contentArea.appendChild(table);
})();
  })();
});

Latest revision as of 18:29, 15 December 2024

mw.loader.using(["mediawiki.util"]).then(function () {
  (function () {
    var targetPage = "Meals"; // Target page name
    var currentPage = mw.config.get("wgPageName");

    // Run the script only if the current page matches the target page
    if (currentPage !== targetPage) {
      return;
    }

    // Check if the content area exists
    var contentArea = document.getElementById("mw-content-text");
    if (!contentArea) {
      return;
    }

    // Create a table of contents
    var toc = document.createElement("div");
    toc.className = "toc";
    toc.style.marginBottom = "20px";
    toc.style.fontWeight = "bold";
    toc.innerHTML = "<h2>Contents</h2>";

    // Define the content list (title and section ids)
    var contentList = [
      { name: "Common Monsters", id: "common-monsters" },
      { name: "Mini Boss Monsters", id: "mini-boss-monsters" },
      { name: "Saga Monsters", id: "saga-monsters" },
      { name: "Maze Monsters", id: "maze-monsters" },
      { name: "Ancient Temple [100 - 169]", id: "ancient-temple" },
      { name: "Hellish Vanguard [100 - 169]", id: "hellish-vanguard" },
      { name: "Lost Graveyard [170 - 239]", id: "lost-graveyard" },
      { name: "Heaven Hills [170 - 239]", id: "heaven-hills" },
      { name: "Frozen Lair [240 - 299]", id: "frozen-lair" },
      { name: "Saiyan Bandits Hideout Siege [240 - 299]", id: "saiyan-bandits-hideout" },
      { name: "Curse of Pharaoh (20-39)", id: "curse-of-pharaoh" },
      { name: "Mafia Dungeon (40-59)", id: "mafia-dungeon" },
      { name: "Army Secret (60-99)", id: "army-secret" },
      { name: "Jungle Adventure (100-129)", id: "jungle-adventure" },
      { name: "Namek Dungeon (130-169)", id: "namek-dungeon" }
    ];

    // Create a list of links for the table of contents
    var tocList = document.createElement("ul");
    contentList.forEach(function (content) {
      var listItem = document.createElement("li");
      var link = document.createElement("a");
      link.href = "#" + content.id;
      link.textContent = content.name;
      listItem.appendChild(link);
      tocList.appendChild(listItem);
    });
    toc.appendChild(tocList);

    // Insert the table of contents at the top of the content area
    contentArea.prepend(toc);

    // Define the categories and their sample monsters
    var monsterCategories = {
      "common-monsters": [
        ["Slime", "Level 1-10", "Low health, basic attack"],
        ["Goblin", "Level 5-15", "Quick and nimble"],
        ["Wolf", "Level 10-20", "Can be found in packs"],
        ["Bandit", "Level 15-25", "Average strength, basic weapons"],
        ["Rat", "Level 1-5", "Weak, commonly found in caves"],
        ["Skeleton", "Level 20-30", "Basic undead with moderate strength"],
        ["Orc", "Level 25-35", "Strong and slow, uses heavy weapons"],
        ["Zombie", "Level 30-40", "Slow, but high health"],
        ["Imp", "Level 10-20", "Magic user with fire attacks"],
        ["Mummy", "Level 15-25", "Undead with poison attack"]
      ],
      "mini-boss-monsters": [
        ["Vampire Lord", "Level 50-60", "High health, drains life"],
        ["Dark Knight", "Level 45-55", "Powerful and armored"],
        ["Demon Beast", "Level 60-70", "Summons minions, strong attack"],
        ["Cyclops", "Level 50-60", "High damage, weak defense"],
        ["Werewolf", "Level 55-65", "Fast and aggressive"],
        ["Giant Spider", "Level 45-55", "Poisonous bite, high defense"],
        ["Dragon Whelp", "Level 65-75", "Breathes fire, tough scales"],
        ["Griffin", "Level 60-70", "Aerial monster with fast attacks"],
        ["Minotaur", "Level 70-80", "Brutal strength, large axe"],
        ["Basilisk", "Level 55-65", "Stone gaze and poison attack"]
      ],
      // Add more categories as needed
    };

    // Create a table to display the monsters
    var table = document.createElement("table");
    table.className = "wikitable"; // Apply MediaWiki's table styling
    table.style.width = "100%";
    table.style.margin = "20px auto";

    // Add table header
    var headers = ["Monster", "Level Range", "Description"];
    var headerRow = document.createElement("tr");
    headers.forEach(function (header) {
      var th = document.createElement("th");
      th.textContent = header;
      headerRow.appendChild(th);
    });
    table.appendChild(headerRow);

    // Add rows for each category with sample monsters
    Object.keys(monsterCategories).forEach(function (category) {
      // Add category header with link
      var sectionRow = document.createElement("tr");
      var sectionCell = document.createElement("td");
      sectionCell.colSpan = "3";
      var anchor = document.createElement("a");
      anchor.name = category;  // Create anchor link for this section
      sectionCell.appendChild(anchor);
      sectionCell.textContent = category.replace("-", " ").toUpperCase();
      sectionRow.appendChild(sectionCell);
      table.appendChild(sectionRow);

      // Add monsters for this category
      monsterCategories[category].forEach(function (monster) {
        var monsterRow = document.createElement("tr");
        monster.forEach(function (data) {
          var cell = document.createElement("td");
          cell.textContent = data;
          monsterRow.appendChild(cell);
        });
        table.appendChild(monsterRow);
      });
    });

    // Insert the table after the table of contents
    contentArea.appendChild(table);
  })();
});