Jump to content

MediaWiki:Common.js: Difference between revisions

From A Modern Way To Be Known
No edit summary
No edit summary
Line 12: Line 12:
   const pageTitle = mw.config.get('wgTitle');
   const pageTitle = mw.config.get('wgTitle');


   const categories = Array.from(
   let schema = null;
    document.querySelectorAll('#mw-normal-catlinks a')
  ).map(a => a.innerText.toLowerCase());


   let schema = null;
   // COMPANY (Infobox company)
  if (infobox.innerHTML.toLowerCase().includes('industry')
      && infobox.innerHTML.toLowerCase().includes('founded')) {


  // COMPANY
  if (categories.some(c => c.includes('companies') || c.includes('agency'))) {
     schema = {
     schema = {
       "@context": "https://schema.org",
       "@context": "https://schema.org",
Line 27: Line 25:
       "description": metaDesc
       "description": metaDesc
     };
     };
     const logo = infobox.querySelector('img')?.src;
     const logo = infobox.querySelector('img')?.src;
     if (logo) schema.logo = logo;
     if (logo) schema.logo = logo;
   }
   }


   // PERSON
   // PERSON (Infobox person)
   else if (categories.some(c => c.includes('people') || c.includes('birth'))) {
   else if (infobox.innerHTML.toLowerCase().includes('born')) {
 
     schema = {
     schema = {
       "@context": "https://schema.org",
       "@context": "https://schema.org",
Line 40: Line 40:
       "description": metaDesc
       "description": metaDesc
     };
     };
     const image = infobox.querySelector('img')?.src;
     const image = infobox.querySelector('img')?.src;
     if (image) schema.image = image;
     if (image) schema.image = image;
  }
  // MOVIE
  else if (categories.some(c => c.includes('films') || c.includes('movies'))) {
    schema = {
      "@context": "https://schema.org",
      "@type": "Movie",
      "name": pageTitle,
      "url": pageUrl,
      "description": metaDesc
    };
    const poster = infobox.querySelector('img')?.src;
    if (poster) schema.image = poster;
   }
   }



Revision as of 19:20, 17 December 2025

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

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

  const infobox = document.querySelector('.infobox');
  if (!infobox) return;

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

  const pageUrl = location.href;
  const pageTitle = mw.config.get('wgTitle');

  let schema = null;

  // COMPANY (Infobox company)
  if (infobox.innerHTML.toLowerCase().includes('industry')
      && infobox.innerHTML.toLowerCase().includes('founded')) {

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

    const logo = infobox.querySelector('img')?.src;
    if (logo) schema.logo = logo;
  }

  // PERSON (Infobox person)
  else if (infobox.innerHTML.toLowerCase().includes('born')) {

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

    const image = infobox.querySelector('img')?.src;
    if (image) schema.image = image;
  }

  if (!schema) return;

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

});