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 a specific page
// JavaScript to dynamically create and insert a table, only on the "Meals" page
(function () {
(function () {
   // Specify the page where the script should run
   // Specify the page name (MediaWiki uses underscores for spaces in page titles)
   const targetPage = "Target_Page_Name"; // Replace with your page's title (spaces replaced with underscores)
   const targetPage = "Meals";


   // Get the current page name from MediaWiki
   // Get the current page name from MediaWiki's configuration
   const currentPage = mw.config.get("wgMeals");
   const currentPage = mw.config.get("wgPageName");


   // Run the script only on 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 page content area exists
   // Check if the content area exists
   const contentArea = document.getElementById("mw-content-text");
   const contentArea = document.getElementById("mw-content-text");
   if (!contentArea) return;
   if (!contentArea) return;
Line 16: Line 16:
   // Create table element
   // Create table element
   const table = document.createElement("table");
   const table = document.createElement("table");
   table.className = "wikitable"; // MediaWiki styling class
   table.className = "wikitable"; // Apply MediaWiki's table styling
   table.style.width = "50%";
   table.style.width = "50%";
   table.style.margin = "20px auto";
   table.style.margin = "20px auto";


   // Add table header
   // Add table header
   const headers = ["Name", "Age", "City"];
   const headers = ["Food", "Category", "Calories"];
   const headerRow = document.createElement("tr");
   const headerRow = document.createElement("tr");
   headers.forEach((header) => {
   headers.forEach((header) => {
     const th = document.createElement("th");
     const th = document.createElement("th");
     th.textContent = header;
     th.textContent = header;
     headerRow.appendChild(th);
     headerRow
  });
  table.appendChild(headerRow);
 
  // Add table rows with random data
  const data = [
    ["Alice", "25", "New York"],
    ["Bob", "30", "Los Angeles"],
    ["Charlie", "35", "Chicago"]
  ];
 
  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 table at the top of the page content
})();

Revision as of 17:58, 15 December 2024

// JavaScript to dynamically create and insert a table, only on the "Meals" page
(function () {
  // Specify the page name (MediaWiki uses underscores for spaces in page titles)
  const targetPage = "Meals";

  // Get the current page name from MediaWiki's configuration
  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