Jump to content

MediaWiki:Common.js: Difference between revisions

From A Modern Way To Be Known
No edit summary
No edit summary
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
mw.config.set('wgRawHtml', true);
mw.loader.using('mediawiki.util', function () {
mw.loader.using('mediawiki.util', function () {


  // Run only on content pages
   if (mw.config.get('wgNamespaceNumber') !== 0) return;
   if (mw.config.get('wgNamespaceNumber') !== 0) return;


  // Detect company pages by infobox
   const metaDesc =
   const infobox = document.querySelector('.infobox.company');
    document.querySelector('meta[name="description"]')?.content || "";
  if (!infobox) return;


  // Extract values safely
   const pageUrl = location.href;
   const name =
  let schema = null;
    infobox.querySelector('.fn')?.innerText ||
    mw.config.get('wgTitle');


   const logoImg = infobox.querySelector('img');
   function inject(schemaObj) {
  const logo = logoImg ? logoImg.src : null;
    const script = document.createElement('script');
    script.type = 'application/ld+json';
    script.text = JSON.stringify(schemaObj);
    document.head.appendChild(script);
  }
 
  /* COMPANY / BRAND */
  if (document.querySelector('.infobox.company')) {
    const name = document.querySelector('.infobox .fn')?.innerText || mw.config.get('wgTitle');
    const logo = document.querySelector('.infobox img')?.src || null;
 
    schema = {
      "@context": "https://schema.org",
      "@type": "ProfilePage",
      "mainEntity": {
        "@type": "Organization",
        "name": name,
        "url": pageUrl,
        "description": metaDesc,
        "address": { "@type": "PostalAddress", "addressCountry": "IN" }
      }
    };
 
    if (logo) schema.mainEntity.logo = logo;
  }


   const description =
   /* PERSON / BIRTHDAY */
    document.querySelector('meta[name="description"]')?.content || '';
  else if (document.querySelector('.infobox.person')) {
    const name = document.querySelector('.infobox .fn')?.innerText || mw.config.get('wgTitle');
    const image = document.querySelector('.infobox img')?.src || null;


  const schema = {
    schema = {
    "@context": "https://schema.org",
      "@context": "https://schema.org",
    "@type": "ProfilePage",
      "@type": "ProfilePage",
    "mainEntity": {
      "mainEntity": {
      "@type": "Organization",
        "@type": "Person",
      "name": name,
        "name": name,
      "url": location.href,
        "url": pageUrl,
      "description": description,
        "description": metaDesc
      "address": {
        "@type": "PostalAddress",
        "addressCountry": "IN"
       }
       }
     }
     };
   };
 
    if (image) schema.mainEntity.image = image;
   }


   if (logo) {
   /* MOVIE / FILM */
     schema.mainEntity.logo = logo;
  else if (document.querySelector('.infobox.film')) {
     const title = document.querySelector('.infobox .summary')?.innerText || mw.config.get('wgTitle');
    const poster = document.querySelector('.infobox img')?.src || null;
 
    schema = {
      "@context": "https://schema.org",
      "@type": "ProfilePage",
      "mainEntity": {
        "@type": "Movie",
        "name": title,
        "url": pageUrl,
        "description": metaDesc
      }
    };
 
    if (poster) schema.mainEntity.image = poster;
  }
 
  /* PLACE */
  else if (document.querySelector('.infobox.place')) {
    const name = document.querySelector('.infobox .fn')?.innerText || mw.config.get('wgTitle');
 
    schema = {
      "@context": "https://schema.org",
      "@type": "ProfilePage",
      "mainEntity": {
        "@type": "Place",
        "name": name,
        "url": pageUrl,
        "description": metaDesc
      }
    };
   }
   }


   // Inject schema
   if (schema) inject(schema);
  const script = document.createElement('script');
  script.type = 'application/ld+json';
  script.text = JSON.stringify(schema);
  document.head.appendChild(script);


});
});

Revision as of 19:03, 17 December 2025

mw.loader.using('mediawiki.util', function () {

  if (mw.config.get('wgNamespaceNumber') !== 0) return;

  const metaDesc =
    document.querySelector('meta[name="description"]')?.content || "";

  const pageUrl = location.href;
  let schema = null;

  function inject(schemaObj) {
    const script = document.createElement('script');
    script.type = 'application/ld+json';
    script.text = JSON.stringify(schemaObj);
    document.head.appendChild(script);
  }

  /* COMPANY / BRAND */
  if (document.querySelector('.infobox.company')) {
    const name = document.querySelector('.infobox .fn')?.innerText || mw.config.get('wgTitle');
    const logo = document.querySelector('.infobox img')?.src || null;

    schema = {
      "@context": "https://schema.org",
      "@type": "ProfilePage",
      "mainEntity": {
        "@type": "Organization",
        "name": name,
        "url": pageUrl,
        "description": metaDesc,
        "address": { "@type": "PostalAddress", "addressCountry": "IN" }
      }
    };

    if (logo) schema.mainEntity.logo = logo;
  }

  /* PERSON / BIRTHDAY */
  else if (document.querySelector('.infobox.person')) {
    const name = document.querySelector('.infobox .fn')?.innerText || mw.config.get('wgTitle');
    const image = document.querySelector('.infobox img')?.src || null;

    schema = {
      "@context": "https://schema.org",
      "@type": "ProfilePage",
      "mainEntity": {
        "@type": "Person",
        "name": name,
        "url": pageUrl,
        "description": metaDesc
      }
    };

    if (image) schema.mainEntity.image = image;
  }

  /* MOVIE / FILM */
  else if (document.querySelector('.infobox.film')) {
    const title = document.querySelector('.infobox .summary')?.innerText || mw.config.get('wgTitle');
    const poster = document.querySelector('.infobox img')?.src || null;

    schema = {
      "@context": "https://schema.org",
      "@type": "ProfilePage",
      "mainEntity": {
        "@type": "Movie",
        "name": title,
        "url": pageUrl,
        "description": metaDesc
      }
    };

    if (poster) schema.mainEntity.image = poster;
  }

  /* PLACE */
  else if (document.querySelector('.infobox.place')) {
    const name = document.querySelector('.infobox .fn')?.innerText || mw.config.get('wgTitle');

    schema = {
      "@context": "https://schema.org",
      "@type": "ProfilePage",
      "mainEntity": {
        "@type": "Place",
        "name": name,
        "url": pageUrl,
        "description": metaDesc
      }
    };
  }

  if (schema) inject(schema);

});