MediaWiki:Common.js: Difference between revisions

From Dragon Ball World Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 2: Line 2:
mw.loader.using(["mediawiki.util"]).then(function () {
mw.loader.using(["mediawiki.util"]).then(function () {
   (function () {
   (function () {
     const targetPage = "Meals"; // Target page name
     var targetPage = "Meals"; // Target page name
     const currentPage = mw.config.get("wgPageName");
     var currentPage = mw.config.get("wgPageName");


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


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


     // Create table element
     // Create table element
     const table = document.createElement("table");
     var table = document.createElement("table");
     table.className = "wikitable"; // Apply MediaWiki's table styling
     table.className = "wikitable"; // Apply MediaWiki's table styling
     table.style.width = "50%";
     table.style.width = "50%";
Line 19: Line 23:


     // Add table header
     // Add table header
     const headers = ["Food", "Category", "Calories"];
     var headers = ["Food", "Category", "Calories"];
     const headerRow = document.createElement("tr");
     var headerRow = document.createElement("tr");
     headers.forEach((header) => {
     headers.forEach(function (header) {
       const th = document.createElement("th");
       var th = document.createElement("th");
       th.textContent = header;
       th.textContent = header;
       headerRow.appendChild(th);
       headerRow.appendChild(th);
Line 29: Line 33:


     // Add table rows with sample data
     // Add table rows with sample data
     const data = [
     var data = [
       ["Pizza", "Fast Food", "285"],
       ["Pizza", "Fast Food", "285"],
       ["Apple", "Fruit", "52"],
       ["Apple", "Fruit", "52"],
       ["Salad", "Healthy", "150"],
       ["Salad", "Healthy", "150"]
     ];
     ];


     data.forEach((row) => {
     data.forEach(function (row) {
       const tableRow = document.createElement("tr");
       var tableRow = document.createElement("tr");
       row.forEach((cell) => {
       row.forEach(function (cell) {
         const td = document.createElement("td");
         var td = document.createElement("td");
         td.textContent = cell;
         td.textContent = cell;
         tableRow.appendChild(td);
         tableRow.appendChild(td);

Revision as of 18:13, 15 December 2024

// JavaScript to dynamically create and insert a table, only on the "Meals" page
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 table element
    var table = document.createElement("table");
    table.className = "wikitable"; // Apply MediaWiki's table styling
    table.style.width = "50%";
    table.style.margin = "20px auto";

    // Add table header
    var headers = ["Food", "Category", "Calories"];
    var headerRow = document.createElement("tr");
    headers.forEach(function (header) {
      var th = document.createElement("th");
      th.textContent = header;
      headerRow.appendChild(th);
    });
    table.appendChild(headerRow);

    // Add table rows with sample data
    var data = [
      ["Pizza", "Fast Food", "285"],
      ["Apple", "Fruit", "52"],
      ["Salad", "Healthy", "150"]
    ];

    data.forEach(function (row) {
      var tableRow = document.createElement("tr");
      row.forEach(function (cell) {
        var td = document.createElement("td");
        td.textContent = cell;
        tableRow.appendChild(td);
      });
      table.appendChild(tableRow);
    });

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