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:
mw.loader.using('mediawiki.util', function () {
mw.loader.using('mediawiki.util', function () {


   if (mw.config.get('wgNamespaceNumber') !== 0) return;
   // Wait until page HTML is fully ready
  document.addEventListener('DOMContentLoaded', function () {


  const infobox = document.querySelector('.infobox');
    if (mw.config.get('wgNamespaceNumber') !== 0) return;
  if (!infobox) return;


  const metaDesc =
    const infobox = document.querySelector('table.infobox');
    document.querySelector('meta[name="description"]')?.content || "";
    if (!infobox) return;


  const pageUrl = location.href;
    const metaDesc =
  const pageTitle = mw.config.get('wgTitle');
      document.querySelector('meta[name="description"]')?.content || "";


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


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


     schema = {
     const infoboxText = infobox.innerText.toLowerCase();
      "@context": "https://schema.org",
      "@type": "Organization",
      "name": pageTitle,
      "url": pageUrl,
      "description": metaDesc
    };


     const logo = infobox.querySelector('img')?.src;
     // COMPANY
    if (logo) schema.logo = logo;
    if (infoboxText.includes('industry') || infoboxText.includes('founded')) {
  }
      schema = {
        "@context": "https://schema.org",
        "@type": "Organization",
        "name": pageTitle,
        "url": pageUrl,
        "description": metaDesc
      };


  // PERSON (Infobox person)
      const logo = infobox.querySelector('img')?.src;
  else if (infobox.innerHTML.toLowerCase().includes('born')) {
      if (logo) schema.logo = logo;
    }


     schema = {
     // PERSON
      "@context": "https://schema.org",
    else if (infoboxText.includes('born')) {
      "@type": "Person",
      schema = {
      "name": pageTitle,
        "@context": "https://schema.org",
      "url": pageUrl,
        "@type": "Person",
      "description": metaDesc
        "name": pageTitle,
    };
        "url": pageUrl,
        "description": metaDesc
      };


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


  if (!schema) return;
    if (!schema) return;


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


});
});

Revision as of 19:22, 17 December 2025

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

  // Wait until page HTML is fully ready
  document.addEventListener('DOMContentLoaded', function () {

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

    const infobox = document.querySelector('table.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;

    const infoboxText = infobox.innerText.toLowerCase();

    // COMPANY
    if (infoboxText.includes('industry') || infoboxText.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
    else if (infoboxText.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);

  });

});