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:
console.log("BharatWiki Common.js loaded");
mw.loader.using('mediawiki.util', function () {
mw.loader.using('mediawiki.util', function () {



Revision as of 19:06, 17 December 2025

console.log("BharatWiki Common.js loaded");
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);

});