MediaWiki:Common.js: Difference between revisions

No edit summary
No edit summary
Line 1: Line 1:
console.log("BharatWiki Common.js loaded");
console.log("BharatWiki Common.js loaded");
mw.loader.using('mediawiki.util', function () {
mw.loader.using('mediawiki.util', function () {


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


   const metaDesc =
   const metaDesc =
Line 8: Line 12:


   const pageUrl = location.href;
   const pageUrl = location.href;
   let schema = null;
   const pageTitle = mw.config.get('wgTitle');
 
  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;
  const categories = Array.from(
  }
     document.querySelectorAll('#mw-normal-catlinks a')
  ).map(a => a.innerText.toLowerCase());


   /* PERSON / BIRTHDAY */
   let entityType = null;
  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 = {
  if (categories.some(c => c.includes('companies') || c.includes('agency'))) {
      "@context": "https://schema.org",
    entityType = "Organization";
      "@type": "ProfilePage",
  } else if (categories.some(c => c.includes('people') || c.includes('birth'))) {
      "mainEntity": {
    entityType = "Person";
        "@type": "Person",
  } else if (categories.some(c => c.includes('films') || c.includes('movies'))) {
        "name": name,
    entityType = "Movie";
        "url": pageUrl,
  } else if (categories.some(c => c.includes('places'))) {
        "description": metaDesc
    entityType = "Place";
      }
    };
 
    if (image) schema.mainEntity.image = image;
   }
   }


   /* MOVIE / FILM */
   if (!entityType) return;
  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 = {
  const image = infobox.querySelector('img')?.src || null;
      "@context": "https://schema.org",
      "@type": "ProfilePage",
      "mainEntity": {
        "@type": "Movie",
        "name": title,
        "url": pageUrl,
        "description": metaDesc
      }
    };
 
    if (poster) schema.mainEntity.image = poster;
  }


   /* PLACE */
   const schema = {
  else if (document.querySelector('.infobox.place')) {
    "@context": "https://schema.org",
     const name = document.querySelector('.infobox .fn')?.innerText || mw.config.get('wgTitle');
    "@type": "ProfilePage",
    "mainEntity": {
      "@type": entityType,
      "name": pageTitle,
      "url": pageUrl,
      "description": metaDesc
     }
  };


     schema = {
  if (image) {
       "@context": "https://schema.org",
     if (entityType === "Organization") {
      "@type": "ProfilePage",
       schema.mainEntity.logo = image;
       "mainEntity": {
    } else {
        "@type": "Place",
       schema.mainEntity.image = image;
        "name": name,
     }
        "url": pageUrl,
        "description": metaDesc
      }
     };
   }
   }


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


});
});