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 () {


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


     if (mw.config.get('wgNamespaceNumber') !== 0) return;
  function injectSchema() {
 
    // Avoid duplicate injection
     if (document.getElementById('bw-entity-schema')) return;


     const infobox = document.querySelector('table.infobox');
     const infobox = document.querySelector('table.infobox');
Line 12: Line 14:
       document.querySelector('meta[name="description"]')?.content || "";
       document.querySelector('meta[name="description"]')?.content || "";


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


    const text = infobox.innerText.toLowerCase();
     let schema = null;
     let schema = null;
    const infoboxText = infobox.innerText.toLowerCase();


     // COMPANY
     // COMPANY
     if (infoboxText.includes('industry') || infoboxText.includes('founded')) {
     if (text.includes('industry') || text.includes('founded')) {
       schema = {
       schema = {
         "@context": "https://schema.org",
         "@context": "https://schema.org",
Line 34: Line 35:


     // PERSON
     // PERSON
     else if (infoboxText.includes('born')) {
     else if (text.includes('born')) {
       schema = {
       schema = {
         "@context": "https://schema.org",
         "@context": "https://schema.org",
Line 51: Line 52:
     const script = document.createElement('script');
     const script = document.createElement('script');
     script.type = 'application/ld+json';
     script.type = 'application/ld+json';
    script.id = 'bw-entity-schema';
     script.text = JSON.stringify(schema);
     script.text = JSON.stringify(schema);
     document.head.appendChild(script);
     document.head.appendChild(script);
  }
  // Watch for SEO plugin injecting Article schema
  const observer = new MutationObserver(injectSchema);
  observer.observe(document.head, { childList: true, subtree: true });


   });
   // Also try once after load
  window.addEventListener('load', injectSchema);


});
});

Revision as of 19:24, 17 December 2025

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

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

  function injectSchema() {

    // Avoid duplicate injection
    if (document.getElementById('bw-entity-schema')) return;

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

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

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

    const text = infobox.innerText.toLowerCase();
    let schema = null;

    // COMPANY
    if (text.includes('industry') || text.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 (text.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.id = 'bw-entity-schema';
    script.text = JSON.stringify(schema);

    document.head.appendChild(script);
  }

  // Watch for SEO plugin injecting Article schema
  const observer = new MutationObserver(injectSchema);
  observer.observe(document.head, { childList: true, subtree: true });

  // Also try once after load
  window.addEventListener('load', injectSchema);

});