MediaWiki:Common.js: Difference between revisions

From Dragon Ball World Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
// JavaScript to dynamically create and insert a table, only on the "Meals" page
// JavaScript to dynamically create and insert a table, only on the "Meals" page
(function () {
mw.loader.using(["mediawiki.util"]).then(function () {
   // Specify the page name (MediaWiki uses underscores for spaces in page titles)
   (function () {
  const targetPage = "Meals";
    const targetPage = "Meals"; // Target page name
    const currentPage = mw.config.get("wgPageName");


  // Get the current page name from MediaWiki's configuration
    // Run the script only if the current page matches the target page
  const currentPage = mw.config.get("wgPageName");
    if (currentPage !== targetPage) return;


  // Run the script only if the current page matches the target page
    // Check if the content area exists
  if (currentPage !== targetPage) return;
    const contentArea = document.getElementById("mw-content-text");
    if (!contentArea) return;


  // Check if the content area exists
    // Create table element
  const contentArea = document.getElementById("mw-content-text");
    const table = document.createElement("table");
  if (!contentArea) return;
    table.className = "wikitable"; // Apply MediaWiki's table styling
    table.style.width = "50%";
    table.style.margin = "20px auto";


  // Create table element
    // Add table header
  const table = document.createElement("table");
    const headers = ["Food", "Category", "Calories"];
  table.className = "wikitable"; // Apply MediaWiki's table styling
    const headerRow = document.createElement("tr");
  table.style.width = "50%";
    headers.forEach((header) => {
  table.style.margin = "20px auto";
      const th = document.createElement("th");
      th.textContent = header;
      headerRow.appendChild(th);
    });
    table.appendChild(headerRow);


  // Add table header
    // Add table rows with sample data
  const headers = ["Food", "Category", "Calories"];
    const data = [
  const headerRow = document.createElement("tr");
      ["Pizza", "Fast Food", "285"],
  headers.forEach((header) => {
      ["Apple", "Fruit", "52"],
    const th = document.createElement("th");
      ["Salad", "Healthy", "150"]
    th.textContent = header;
    ];
     headerRow
 
    data.forEach((row) => {
      const tableRow = document.createElement("tr");
      row.forEach((cell) => {
        const 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
  })();
});

Revision as of 18:07, 15 December 2024

// JavaScript to dynamically create and insert a table, only on the "Meals" page
mw.loader.using(["mediawiki.util"]).then(function () {
  (function () {
    const targetPage = "Meals"; // Target page name
    const 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
    const contentArea = document.getElementById("mw-content-text");
    if (!contentArea) return;

    // Create table element
    const table = document.createElement("table");
    table.className = "wikitable"; // Apply MediaWiki's table styling
    table.style.width = "50%";
    table.style.margin = "20px auto";

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

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

    data.forEach((row) => {
      const tableRow = document.createElement("tr");
      row.forEach((cell) => {
        const 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
  })();
});