Jump to content

MediaWiki:Common.js: Difference between revisions

From Grantha
No edit summary
No edit summary
 
(21 intermediate revisions by the same user not shown)
Line 1: Line 1:
/* =========================
/* MediaWiki:Common.js — grantha.io
  VERSE ACTIONS
*
========================= */
* Responsibilities:
* 1. Transliteration engine (Devanāgarī → IAST / Kannada / Tamil).
*  2. Tag all transliteratable text nodes once per page load.
*  3. Apply the script stored in localStorage('grantha_reader_script')
*    so every page opens in the user's preferred script automatically.
*  4. Listen for the 'gr-script-change' CustomEvent dispatched by the
*    ReaderToolbar dropdown and re-apply the new script immediately.
*  5. TOC active-item highlight via MutationObserver (unchanged).
*
* The script-switcher bar/buttons previously built here are removed —
* the ReaderToolbar extension now owns the dropdown UI.
*
* localStorage key shared with ReaderToolbar: 'grantha_reader_script'
*/
 
( function () {
( function () {
  'use strict';


   /* ── Safe delegated listener ─────────────────────────────────────────── */
   var LS_SCRIPT_KEY = 'grantha_reader_script';
   function onDocClick( e ) {
   var currentScript = 'deva';
    var target = e.target;


    // Walk up to find a matching action button
  // ── IAST transliteration ────────────────────────────────────────
    function closest( el, cls ) {
  function devanagariToIAST( text ) {
       while ( el && el !== document ) {
    var CONSONANTS = {
         if ( el.classList && el.classList.contains( cls ) ) return el;
      'क':'k','ख':'kh','ग':'g','घ':'gh','ङ':'ṅ',
         el = el.parentNode;
      'च':'c','छ':'ch','ज':'j','झ':'jh','ञ':'ñ',
       }
      'ट':'ṭ','ठ':'ṭh','ड':'ḍ','ढ':'ḍh','ण':'ṇ',
       return null;
      'त':'t','थ':'th','द':'d','ध':'dh','न':'n',
      'प':'p','फ':'ph','ब':'b','भ':'bh','म':'m',
      'य':'y','र':'r','ल':'l','ळ':'ḷ','व':'v',
      'श':'ś','ष':'ṣ','स':'s','ह':'h'
    };
    var DIACRITICS = {
      'ा':'ā','ि':'i','ी':'ī','ु':'u','ू':'ū',
      'ृ':'ṛ','ॄ':'ṝ','े':'e','ै':'ai','ो':'o','ौ':'au'
    };
    var VOWELS = {
      'अ':'a','आ':'ā','इ':'i','ई':'ī','उ':'u','ऊ':'ū',
      'ऋ':'ṛ','ॠ':'ṝ','ए':'e','ऐ':'ai','ओ':'o','औ':'au','ऽ':"'"
    };
    var MISC = {
       'ं':'ṃ','ः':'ḥ','ँ':'m̐','ॐ':'oṃ',
      '०':'0','१':'1','२':'2','३':'3','४':'4',
      '५':'5','६':'6','७':'7','८':'8','९':'9'
    };
    var HALANTA = '्';
    var chars = Array.from( text );
    var result = '';
    var i = 0;
    while ( i < chars.length ) {
      var ch  = chars[ i ];
      var next = chars[ i + 1 ];
      if ( CONSONANTS[ ch ] ) {
        var base = CONSONANTS[ ch ];
         if ( next === HALANTA )          { result += base;              i += 2; }
        else if ( DIACRITICS[ next ] )   { result += base + DIACRITICS[ next ]; i += 2; }
        else if ( next === 'ं' || next === 'ः' ) { result += base + 'a' + MISC[ next ]; i += 2; }
         else                            { result += base + 'a';        i++;    }
      } else if ( VOWELS[ ch ] )        { result += VOWELS[ ch ];      i++; }
      else if ( DIACRITICS[ ch ] )      { result += DIACRITICS[ ch ];  i++; }
       else if ( MISC[ ch ] )            { result += MISC[ ch ];        i++; }
       else                              { result += ch;                 i++; }
     }
     }
    return result;
  }


    /* ── Commentary toggle ── */
  // ── Character maps for Kannada / Tamil ─────────────────────────
var commentBtn = closest( target, 'verse-action-commentary' );
  var SCRIPT_MAP = {
if ( commentBtn ) {
    kn: {
  e.preventDefault();
      'अ':'ಅ','आ':'ಆ','इ':'ಇ','ई':'ಈ','उ':'ಉ','ऊ':'ಊ','ऋ':'ಋ',
  var verseId = commentBtn.getAttribute( 'data-verse' );
      'ए':'ಏ','ऐ':'ಐ','ओ':'ಓ','औ':'ಔ','ऽ':'ಽ',
  if ( !verseId ) return;
      'क':'ಕ','ख':'ಖ','ग':'ಗ','घ':'ಘ','ङ':'ಙ',
 
      'च':'ಚ','छ':'ಛ','ज':'ಜ','झ':'ಝ','ञ':'ಞ',
  // Match all commentary bodies for this verse by id prefix
      'ट':'ಟ','ठ':'ಠ','ड':'ಡ','ढ':'ಢ','ण':'ಣ',
  var allBodies = document.querySelectorAll( '[id^="commentary-body-' + verseId + '"]' );
      'त':'ತ','थ':'ಥ','द':'ದ','ध':'ಧ','न':'ನ',
  var allBtns  = document.querySelectorAll( '.verse-action-commentary[data-verse="' + verseId + '"]' );
      'प':'ಪ','फ':'ಫ','ब':'ಬ','भ':'ಭ','म':'ಮ',
 
      'य':'ಯ','र':'ರ','ल':'ಲ','व':'ವ',
  var isOpen = allBodies.length && allBodies[0].classList.contains( 'open' );
      'श':'ಶ','ष':'ಷ','स':'ಸ','ह':'ಹ',
 
      'ा':'ಾ','ि':'ಿ','ी':'ೀ','ु':'ು','ू':'ೂ',
  // Close all open commentaries on the page first
      'ृ':'ೃ','े':'ೇ','ै':'ೈ','ो':'ೋ','ौ':'ೌ',
  document.querySelectorAll( '.commentary-body.open' ).forEach( function ( el ) {
      'ं':'ಂ','ः':'ಃ','्':'್',
    el.classList.remove( 'open' );
      '०':'೦','१':'೧','२':'೨','३':'೩','४':'೪',
  } );
      '५':'೫','६':'೬','७':'೭','८':'೮','९':'೯'
  document.querySelectorAll( '.verse-action-commentary.active' ).forEach( function ( el ) {
    },
    el.classList.remove( 'active' );
    ta: {
   } );
      'अ':'அ','आ':'ஆ','इ':'இ','ई':'ஈ','उ':'உ','ऊ':'ஊ',
      'ऋ':'ரு','ॠ':'ரூ',
      'ए':'ஏ','ऐ':'ஐ','ओ':'ஓ','औ':'ஔ',
      'क':'க','ख':'க','ग':'க','घ':'க','ङ':'ங',
      'च':'ச','छ':'ச','ज':'ஜ','झ':'ஜ','ञ':'ஞ',
      'ट':'ட','ठ':'ட','ड':'ட','ढ':'ட','':'',
      'त':'த','थ':'த','द':'த','ध':'த','':'',
      'प':'ப','फ':'ப','ब':'ப','भ':'ப','म':'ம',
      'य':'ய','र':'ர','ल':'ல','ळ':'ழ','व':'',
      'श':'ஶ','ष':'ஷ','स':'ஸ','ह':'ஹ',
      'ा':'ா','ि':'ி','ी':'ீ','ु':'ு','ू':'ூ',
      'ृ':'ு','ॄ':'',
      'े':'ே','ै':'ை','ो':'ோ','ौ':'',
      'ं':'ம்','ः':':','ँ':'ம்','्':'்','ॐ':'ௐ','ऽ':'ௗ',
      '०':'0','१':'1','२':'2','३':'3','४':'4',
      '५':'5','६':'6','७':'7','८':'8','९':'9'
    }
   };


   // If it was closed, open it
   var PRE = [
  if ( !isOpen ) {
    [ /ङ्क/g, 'ंक' ], [ /ङ्ख/g, 'ंख' ], [ /ङ्ग/g, 'ंग' ], [ /ङ्घ/g, 'ंघ' ],
     allBodies.forEach( function ( el ) { el.classList.add( 'open' ); } );
     [ /ञ्च/g, 'ंच' ], [ /ञ्ज/g, 'ंज' ], [ /ण्ट/g, 'ंट' ], [ /ण्ड/g, 'ंड' ],
     allBtns.forEach(  function ( el ) { el.classList.add( 'active' ); } );
     [ /न्त/g, 'ंत' ], [ /न्द/g, 'ंद' ], [ /म्ब/g, 'ंब' ], [ /म्भ/g, 'ंभ' ]
  }
   ];
   return;
}


    /* ── Copy verse — copies verse ID for crosslinking ── */
  function transliterateText( text, script ) {
var copyBtn = closest( target, 'verse-action-copy' );
    if ( script === 'en' ) return devanagariToIAST( text );
if ( copyBtn ) {
    var map = SCRIPT_MAP[ script ];
  e.preventDefault();
    if ( !map ) return text;
  var verseId = copyBtn.getAttribute( 'data-verse' );
     var t = text;
  if ( !verseId ) return;
     PRE.forEach( function ( p ) { t = t.replace( p[ 0 ], p[ 1 ] ); } );
  copyText( verseId, copyBtn );
    return Array.from( t ).map( function ( ch ) {
  return;
       return map[ ch ] !== undefined ? map[ ch ] : ch;
}
     } ).join( '' );
    /* ── Copy ID ── */
     var idBtn = closest( target, 'copy-id-btn' );
     if ( idBtn ) {
      e.preventDefault();
      var id = idBtn.getAttribute( 'data-copyid' );
      if ( !id ) return;
      copyText( id, idBtn );
       return;
     }
   }
   }


   document.addEventListener( 'click', onDocClick );
   // ── Tag all transliteratable text nodes once per page ───────────
  var translatableSpans = [];


  /* ── Copy helper + tooltip ───────────────────────────────────────────── */
   function tagTextNodes() {
   function copyText( text, btn ) {
     // ── Main article content ──────────────────────────────────────
     function showTooltip() {
    var content = document.querySelector( '.mw-parser-output' );
      // Remove stale tooltip if double-clicked
    if ( content ) {
      var old = btn.querySelector( '.copy-tooltip' );
       var walker = document.createTreeWalker( content, NodeFilter.SHOW_TEXT );
       if ( old ) old.remove();
      var nodes  = [];
      while ( walker.nextNode() ) nodes.push( walker.currentNode );


       btn.style.position = 'relative';
       nodes.forEach( function ( node ) {
      var tip = document.createElement( 'span' );
        var p = node.parentNode;
      tip.className  = 'copy-tooltip';
        if ( !p ) return;
      tip.textContent = 'Copied ✓';
        if ( p.hasAttribute && p.hasAttribute( 'data-deva' ) ) return;
      btn.appendChild( tip );
        if ( p.closest ) {
 
          if ( p.closest( '.gr-controls' )    ||
      requestAnimationFrame( function () {
              p.closest( '.mw-editsection' ) ) return;
         requestAnimationFrame( function () {
        }
          tip.classList.add( 'copy-tooltip-visible' );
        var orig = node.textContent;
         } );
        if ( !orig.trim() ) return;
         var span = document.createElement( 'span' );
        span.setAttribute( 'data-deva', orig );
        span.textContent = orig;
        p.replaceChild( span, node );
         translatableSpans.push( span );
       } );
       } );
      setTimeout( function () {
        tip.classList.remove( 'copy-tooltip-visible' );
        setTimeout( function () { tip.remove(); }, 400 );
      }, 1400 );
     }
     }


     if ( navigator.clipboard && window.isSecureContext ) {
     // ── Sidebar TOC (.vector-toc-text spans) ─────────────────────
      navigator.clipboard.writeText( text ).then( showTooltip );
    // We target the .vector-toc-text spans that Vector itself renders,
     } else {
    // rather than walking raw text nodes. Mutating textContent of an
      var ta = document.createElement( 'textarea' );
    // existing child span is a characterData mutation — Vector's own
      ta.value = text;
    // MutationObserver only watches for childList changes on the <li>,
       ta.style.cssText = 'position:fixed;opacity:0;pointer-events:none;';
     // so it will NOT fire, and the "unknown title" bug is avoided.
       document.body.appendChild( ta );
    document.querySelectorAll( '.vector-toc .vector-toc-text' ).forEach( function ( span ) {
       ta.select();
       if ( span.hasAttribute( 'data-deva' ) ) return; // already tagged
       document.execCommand( 'copy' );
       var orig = span.textContent;
       document.body.removeChild( ta );
       if ( !orig.trim() ) return;
      showTooltip();
       span.setAttribute( 'data-deva', orig );
    }
       translatableSpans.push( span );
    } );
   }
   }


}() );
  // ── Apply a script to all tagged spans ─────────────────────────
mw.loader.using(['mediawiki.util']).then(function () {
  function applyScript( script ) {
    currentScript = script;
    translatableSpans.forEach( function ( span ) {
      if ( !span.parentNode ) return; // detached node
      var orig = span.getAttribute( 'data-deva' );
      if ( !orig ) return;
      span.textContent = ( script === 'deva' )
        ? orig
        : transliterateText( orig, script );
    } );
  }


   if (mw.config.get('wgPageName') !== 'Main_Page') return;
   // ── TOC active-item highlight ───────────────────────────────────
  function watchTocActive() {
    var toc = document.querySelector( '.vector-toc' );
    if ( !toc || toc._grObserved ) return;
    toc._grObserved = true;
    if ( !window.MutationObserver ) return;


  var groups = mw.config.get('wgUserGroups') || [];
    function attachHighlight( li ) {
  var isAdmin = groups.indexOf('sysop') !== -1;
      observer.observe( li, { attributes: true, attributeFilter: [ 'class' ] } );
    }


  if (!isAdmin) return;
    var observer = new MutationObserver( function ( mutations ) {
      mutations.forEach( function ( m ) {
        // New list items added (lazy render) → attach highlight + tag for transliteration
        if ( m.type === 'childList' ) {
          m.addedNodes.forEach( function ( n ) {
            if ( n.nodeType !== 1 ) return;
            if ( n.classList.contains( 'vector-toc-list-item' ) ) {
              attachHighlight( n );
            }
            n.querySelectorAll && n.querySelectorAll( '.vector-toc-list-item' )
              .forEach( attachHighlight );


  var container = document.querySelector('.vector-header-end');
            // Tag any newly revealed .vector-toc-text spans for transliteration
  if (!container) return;
            var newTextSpans = [];
            if ( n.classList && n.classList.contains( 'vector-toc-text' ) ) {
              newTextSpans.push( n );
            }
            if ( n.querySelectorAll ) {
              n.querySelectorAll( '.vector-toc-text' ).forEach( function ( s ) {
                newTextSpans.push( s );
              } );
            }
            newTextSpans.forEach( function ( span ) {
              if ( span.hasAttribute( 'data-deva' ) ) return;
              var orig = span.textContent;
              if ( !orig.trim() ) return;
              span.setAttribute( 'data-deva', orig );
              if ( currentScript !== 'deva' ) {
                span.textContent = transliterateText( orig, currentScript );
              }
              translatableSpans.push( span );
            } );
          } );
          return;
        }


  var btn = document.createElement('button');
        // Class change → scroll active item into view if needed.
  btn.className = 'grantha-new-btn';
        if ( m.attributeName !== 'class' ) return;
   btn.innerText = '+ New';
        var li = m.target;
        if ( !li.classList.contains( 'vector-toc-list-item-active' ) ) return;
        var container = document.querySelector( '.vector-sticky-pinned-container' );
        if ( !container ) return;
        var liRect  = li.getBoundingClientRect();
        var cRect   = container.getBoundingClientRect();
        var isAbove = liRect.top    < cRect.top    + 8;
        var isBelow = liRect.bottom > cRect.bottom - 8;
        if ( isAbove || isBelow ) {
          li.scrollIntoView({ block: 'nearest', behavior: 'smooth' });
        }
      } );
    } );


  btn.onclick = openCreateDialog;
    // Observe existing list items for class changes
    toc.querySelectorAll( '.vector-toc-list-item' ).forEach( attachHighlight );
    // Watch for new items (lazy TOC population)
    observer.observe( toc, { childList: true, subtree: true } );


  container.prepend(btn);
    // On initial load: scroll to already-active item
    setTimeout( function () {
      var active = toc.querySelector( '.vector-toc-list-item-active' );
      if ( active ) active.scrollIntoView({ block: 'nearest', behavior: 'auto' });
    }, 400 );
  }


});
  // ── Init ──────────────────────────────────────────────────────
function openCreateDialog() {
  function init() {
    var content      = document.querySelector( '.mw-parser-output' );
    var alreadyTagged = content && content.querySelector( '[data-deva]' );
    if ( !alreadyTagged ) {
      translatableSpans = [];
      tagTextNodes();
    } else {
      // Content already tagged — still tag TOC spans if not yet done
      document.querySelectorAll( '.vector-toc .vector-toc-text:not([data-deva])' ).forEach( function ( span ) {
        var orig = span.textContent;
        if ( !orig.trim() ) return;
        span.setAttribute( 'data-deva', orig );
        translatableSpans.push( span );
      } );
    }


  var overlay = document.createElement('div');
    // Apply the globally persisted script preference immediately
  overlay.className = 'grantha-modal';
    var saved = ( function () {
      try { return localStorage.getItem( LS_SCRIPT_KEY ); } catch ( e ) { return null; }
    }() );
    if ( saved && saved !== 'deva' ) {
      applyScript( saved );
    } else {
      currentScript = 'deva';
    }


  overlay.innerHTML = `
     watchTocActive();
     <div class="grantha-modal-box">
   }
      <div class="gm-title">Create New Document</div>
      <input type="text" id="gm-input" placeholder="Enter page name"/>
      <div class="gm-actions">
        <button id="gm-cancel">Cancel</button>
        <button id="gm-create">Create</button>
      </div>
    </div>
   `;


   document.body.appendChild(overlay);
   // ── React to toolbar dropdown changes on the same page ─────────
  window.addEventListener( 'gr-script-change', function ( e ) {
    var script = e && e.detail && e.detail.script;
    if ( script ) applyScript( script );
  } );


   document.getElementById('gm-cancel').onclick = function () {
   // ── MediaWiki hook (SPA-style navigation support) ───────────────
    overlay.remove();
  if ( window.mw ) {
   };
    mw.hook( 'wikipage.content' ).add( function () {
      setTimeout( function () {
        var content      = document.querySelector( '.mw-parser-output' );
        var alreadyTagged = content && content.querySelector( '[data-deva]' );
        if ( !alreadyTagged ) {
          translatableSpans = [];
          tagTextNodes();
        } else {
          // Tag any untagged TOC spans after navigation
          document.querySelectorAll( '.vector-toc .vector-toc-text:not([data-deva])' ).forEach( function ( span ) {
            var orig = span.textContent;
            if ( !orig.trim() ) return;
            span.setAttribute( 'data-deva', orig );
            translatableSpans.push( span );
          } );
        }
        // Re-apply current script to newly loaded content
        if ( currentScript !== 'deva' ) {
          applyScript( currentScript );
        }
        watchTocActive();
      }, 150 );
    } );
   }


   document.getElementById('gm-create').onclick = function () {
   // ── Fallback for non-MW environments ───────────────────────────
     var name = document.getElementById('gm-input').value.trim();
  if ( document.readyState === 'loading' ) {
     if (!name) return;
     document.addEventListener( 'DOMContentLoaded', init );
  } else {
     init();
  }


    name = name.replace(/\s+/g, '_');
}() );
 
    window.location.href = mw.util.getUrl(name, { action: 'edit' });
  };
}

Latest revision as of 07:16, 17 April 2026

/* MediaWiki:Common.js — grantha.io
 *
 * Responsibilities:
 *  1. Transliteration engine (Devanāgarī → IAST / Kannada / Tamil).
 *  2. Tag all transliteratable text nodes once per page load.
 *  3. Apply the script stored in localStorage('grantha_reader_script')
 *     so every page opens in the user's preferred script automatically.
 *  4. Listen for the 'gr-script-change' CustomEvent dispatched by the
 *     ReaderToolbar dropdown and re-apply the new script immediately.
 *  5. TOC active-item highlight via MutationObserver (unchanged).
 *
 * The script-switcher bar/buttons previously built here are removed —
 * the ReaderToolbar extension now owns the dropdown UI.
 *
 * localStorage key shared with ReaderToolbar: 'grantha_reader_script'
 */

( function () {

  var LS_SCRIPT_KEY = 'grantha_reader_script';
  var currentScript = 'deva';

  // ── IAST transliteration ────────────────────────────────────────
  function devanagariToIAST( text ) {
    var CONSONANTS = {
      'क':'k','ख':'kh','ग':'g','घ':'gh','ङ':'ṅ',
      'च':'c','छ':'ch','ज':'j','झ':'jh','ञ':'ñ',
      'ट':'ṭ','ठ':'ṭh','ड':'ḍ','ढ':'ḍh','ण':'ṇ',
      'त':'t','थ':'th','द':'d','ध':'dh','न':'n',
      'प':'p','फ':'ph','ब':'b','भ':'bh','म':'m',
      'य':'y','र':'r','ल':'l','ळ':'ḷ','व':'v',
      'श':'ś','ष':'ṣ','स':'s','ह':'h'
    };
    var DIACRITICS = {
      'ा':'ā','ि':'i','ी':'ī','ु':'u','ू':'ū',
      'ृ':'ṛ','ॄ':'ṝ','े':'e','ै':'ai','ो':'o','ौ':'au'
    };
    var VOWELS = {
      'अ':'a','आ':'ā','इ':'i','ई':'ī','उ':'u','ऊ':'ū',
      'ऋ':'ṛ','ॠ':'ṝ','ए':'e','ऐ':'ai','ओ':'o','औ':'au','ऽ':"'"
    };
    var MISC = {
      'ं':'ṃ','ः':'ḥ','ँ':'m̐','ॐ':'oṃ',
      '०':'0','१':'1','२':'2','३':'3','४':'4',
      '५':'5','६':'6','७':'7','८':'8','९':'9'
    };
    var HALANTA = '्';
    var chars = Array.from( text );
    var result = '';
    var i = 0;
    while ( i < chars.length ) {
      var ch   = chars[ i ];
      var next = chars[ i + 1 ];
      if ( CONSONANTS[ ch ] ) {
        var base = CONSONANTS[ ch ];
        if ( next === HALANTA )          { result += base;               i += 2; }
        else if ( DIACRITICS[ next ] )   { result += base + DIACRITICS[ next ]; i += 2; }
        else if ( next === 'ं' || next === 'ः' ) { result += base + 'a' + MISC[ next ]; i += 2; }
        else                             { result += base + 'a';         i++;    }
      } else if ( VOWELS[ ch ] )         { result += VOWELS[ ch ];       i++; }
      else if ( DIACRITICS[ ch ] )       { result += DIACRITICS[ ch ];   i++; }
      else if ( MISC[ ch ] )             { result += MISC[ ch ];         i++; }
      else                               { result += ch;                 i++; }
    }
    return result;
  }

  // ── Character maps for Kannada / Tamil ─────────────────────────
  var SCRIPT_MAP = {
    kn: {
      'अ':'ಅ','आ':'ಆ','इ':'ಇ','ई':'ಈ','उ':'ಉ','ऊ':'ಊ','ऋ':'ಋ',
      'ए':'ಏ','ऐ':'ಐ','ओ':'ಓ','औ':'ಔ','ऽ':'ಽ',
      'क':'ಕ','ख':'ಖ','ग':'ಗ','घ':'ಘ','ङ':'ಙ',
      'च':'ಚ','छ':'ಛ','ज':'ಜ','झ':'ಝ','ञ':'ಞ',
      'ट':'ಟ','ठ':'ಠ','ड':'ಡ','ढ':'ಢ','ण':'ಣ',
      'त':'ತ','थ':'ಥ','द':'ದ','ध':'ಧ','न':'ನ',
      'प':'ಪ','फ':'ಫ','ब':'ಬ','भ':'ಭ','म':'ಮ',
      'य':'ಯ','र':'ರ','ल':'ಲ','व':'ವ',
      'श':'ಶ','ष':'ಷ','स':'ಸ','ह':'ಹ',
      'ा':'ಾ','ि':'ಿ','ी':'ೀ','ु':'ು','ू':'ೂ',
      'ृ':'ೃ','े':'ೇ','ै':'ೈ','ो':'ೋ','ौ':'ೌ',
      'ं':'ಂ','ः':'ಃ','्':'್',
      '०':'೦','१':'೧','२':'೨','३':'೩','४':'೪',
      '५':'೫','६':'೬','७':'೭','८':'೮','९':'೯'
    },
    ta: {
      'अ':'அ','आ':'ஆ','इ':'இ','ई':'ஈ','उ':'உ','ऊ':'ஊ',
      'ऋ':'ரு','ॠ':'ரூ',
      'ए':'ஏ','ऐ':'ஐ','ओ':'ஓ','औ':'ஔ',
      'क':'க','ख':'க','ग':'க','घ':'க','ङ':'ங',
      'च':'ச','छ':'ச','ज':'ஜ','झ':'ஜ','ञ':'ஞ',
      'ट':'ட','ठ':'ட','ड':'ட','ढ':'ட','ण':'ண',
      'त':'த','थ':'த','द':'த','ध':'த','न':'ந',
      'प':'ப','फ':'ப','ब':'ப','भ':'ப','म':'ம',
      'य':'ய','र':'ர','ल':'ல','ळ':'ழ','व':'வ',
      'श':'ஶ','ष':'ஷ','स':'ஸ','ह':'ஹ',
      'ा':'ா','ि':'ி','ी':'ீ','ु':'ு','ू':'ூ',
      'ृ':'ு','ॄ':'ூ',
      'े':'ே','ै':'ை','ो':'ோ','ौ':'ௌ',
      'ं':'ம்','ः':':','ँ':'ம்','्':'்','ॐ':'ௐ','ऽ':'ௗ',
      '०':'0','१':'1','२':'2','३':'3','४':'4',
      '५':'5','६':'6','७':'7','८':'8','९':'9'
    }
  };

  var PRE = [
    [ /ङ्क/g, 'ंक' ], [ /ङ्ख/g, 'ंख' ], [ /ङ्ग/g, 'ंग' ], [ /ङ्घ/g, 'ंघ' ],
    [ /ञ्च/g, 'ंच' ], [ /ञ्ज/g, 'ंज' ], [ /ण्ट/g, 'ंट' ], [ /ण्ड/g, 'ंड' ],
    [ /न्त/g, 'ंत' ], [ /न्द/g, 'ंद' ], [ /म्ब/g, 'ंब' ], [ /म्भ/g, 'ंभ' ]
  ];

  function transliterateText( text, script ) {
    if ( script === 'en' ) return devanagariToIAST( text );
    var map = SCRIPT_MAP[ script ];
    if ( !map ) return text;
    var t = text;
    PRE.forEach( function ( p ) { t = t.replace( p[ 0 ], p[ 1 ] ); } );
    return Array.from( t ).map( function ( ch ) {
      return map[ ch ] !== undefined ? map[ ch ] : ch;
    } ).join( '' );
  }

  // ── Tag all transliteratable text nodes once per page ───────────
  var translatableSpans = [];

  function tagTextNodes() {
    // ── Main article content ──────────────────────────────────────
    var content = document.querySelector( '.mw-parser-output' );
    if ( content ) {
      var walker = document.createTreeWalker( content, NodeFilter.SHOW_TEXT );
      var nodes  = [];
      while ( walker.nextNode() ) nodes.push( walker.currentNode );

      nodes.forEach( function ( node ) {
        var p = node.parentNode;
        if ( !p ) return;
        if ( p.hasAttribute && p.hasAttribute( 'data-deva' ) ) return;
        if ( p.closest ) {
          if ( p.closest( '.gr-controls' )    ||
               p.closest( '.mw-editsection' ) ) return;
        }
        var orig = node.textContent;
        if ( !orig.trim() ) return;
        var span = document.createElement( 'span' );
        span.setAttribute( 'data-deva', orig );
        span.textContent = orig;
        p.replaceChild( span, node );
        translatableSpans.push( span );
      } );
    }

    // ── Sidebar TOC (.vector-toc-text spans) ─────────────────────
    // We target the .vector-toc-text spans that Vector itself renders,
    // rather than walking raw text nodes. Mutating textContent of an
    // existing child span is a characterData mutation — Vector's own
    // MutationObserver only watches for childList changes on the <li>,
    // so it will NOT fire, and the "unknown title" bug is avoided.
    document.querySelectorAll( '.vector-toc .vector-toc-text' ).forEach( function ( span ) {
      if ( span.hasAttribute( 'data-deva' ) ) return; // already tagged
      var orig = span.textContent;
      if ( !orig.trim() ) return;
      span.setAttribute( 'data-deva', orig );
      translatableSpans.push( span );
    } );
  }

  // ── Apply a script to all tagged spans ─────────────────────────
  function applyScript( script ) {
    currentScript = script;
    translatableSpans.forEach( function ( span ) {
      if ( !span.parentNode ) return; // detached node
      var orig = span.getAttribute( 'data-deva' );
      if ( !orig ) return;
      span.textContent = ( script === 'deva' )
        ? orig
        : transliterateText( orig, script );
    } );
  }

  // ── TOC active-item highlight ───────────────────────────────────
  function watchTocActive() {
    var toc = document.querySelector( '.vector-toc' );
    if ( !toc || toc._grObserved ) return;
    toc._grObserved = true;
    if ( !window.MutationObserver ) return;

    function attachHighlight( li ) {
      observer.observe( li, { attributes: true, attributeFilter: [ 'class' ] } );
    }

    var observer = new MutationObserver( function ( mutations ) {
      mutations.forEach( function ( m ) {
        // New list items added (lazy render) → attach highlight + tag for transliteration
        if ( m.type === 'childList' ) {
          m.addedNodes.forEach( function ( n ) {
            if ( n.nodeType !== 1 ) return;
            if ( n.classList.contains( 'vector-toc-list-item' ) ) {
              attachHighlight( n );
            }
            n.querySelectorAll && n.querySelectorAll( '.vector-toc-list-item' )
              .forEach( attachHighlight );

            // Tag any newly revealed .vector-toc-text spans for transliteration
            var newTextSpans = [];
            if ( n.classList && n.classList.contains( 'vector-toc-text' ) ) {
              newTextSpans.push( n );
            }
            if ( n.querySelectorAll ) {
              n.querySelectorAll( '.vector-toc-text' ).forEach( function ( s ) {
                newTextSpans.push( s );
              } );
            }
            newTextSpans.forEach( function ( span ) {
              if ( span.hasAttribute( 'data-deva' ) ) return;
              var orig = span.textContent;
              if ( !orig.trim() ) return;
              span.setAttribute( 'data-deva', orig );
              if ( currentScript !== 'deva' ) {
                span.textContent = transliterateText( orig, currentScript );
              }
              translatableSpans.push( span );
            } );
          } );
          return;
        }

        // Class change → scroll active item into view if needed.
        if ( m.attributeName !== 'class' ) return;
        var li = m.target;
        if ( !li.classList.contains( 'vector-toc-list-item-active' ) ) return;
        var container = document.querySelector( '.vector-sticky-pinned-container' );
        if ( !container ) return;
        var liRect  = li.getBoundingClientRect();
        var cRect   = container.getBoundingClientRect();
        var isAbove = liRect.top    < cRect.top    + 8;
        var isBelow = liRect.bottom > cRect.bottom - 8;
        if ( isAbove || isBelow ) {
          li.scrollIntoView({ block: 'nearest', behavior: 'smooth' });
        }
      } );
    } );

    // Observe existing list items for class changes
    toc.querySelectorAll( '.vector-toc-list-item' ).forEach( attachHighlight );
    // Watch for new items (lazy TOC population)
    observer.observe( toc, { childList: true, subtree: true } );

    // On initial load: scroll to already-active item
    setTimeout( function () {
      var active = toc.querySelector( '.vector-toc-list-item-active' );
      if ( active ) active.scrollIntoView({ block: 'nearest', behavior: 'auto' });
    }, 400 );
  }

  // ── Init ──────────────────────────────────────────────────────
  function init() {
    var content       = document.querySelector( '.mw-parser-output' );
    var alreadyTagged = content && content.querySelector( '[data-deva]' );
    if ( !alreadyTagged ) {
      translatableSpans = [];
      tagTextNodes();
    } else {
      // Content already tagged — still tag TOC spans if not yet done
      document.querySelectorAll( '.vector-toc .vector-toc-text:not([data-deva])' ).forEach( function ( span ) {
        var orig = span.textContent;
        if ( !orig.trim() ) return;
        span.setAttribute( 'data-deva', orig );
        translatableSpans.push( span );
      } );
    }

    // Apply the globally persisted script preference immediately
    var saved = ( function () {
      try { return localStorage.getItem( LS_SCRIPT_KEY ); } catch ( e ) { return null; }
    }() );
    if ( saved && saved !== 'deva' ) {
      applyScript( saved );
    } else {
      currentScript = 'deva';
    }

    watchTocActive();
  }

  // ── React to toolbar dropdown changes on the same page ─────────
  window.addEventListener( 'gr-script-change', function ( e ) {
    var script = e && e.detail && e.detail.script;
    if ( script ) applyScript( script );
  } );

  // ── MediaWiki hook (SPA-style navigation support) ───────────────
  if ( window.mw ) {
    mw.hook( 'wikipage.content' ).add( function () {
      setTimeout( function () {
        var content      = document.querySelector( '.mw-parser-output' );
        var alreadyTagged = content && content.querySelector( '[data-deva]' );
        if ( !alreadyTagged ) {
          translatableSpans = [];
          tagTextNodes();
        } else {
          // Tag any untagged TOC spans after navigation
          document.querySelectorAll( '.vector-toc .vector-toc-text:not([data-deva])' ).forEach( function ( span ) {
            var orig = span.textContent;
            if ( !orig.trim() ) return;
            span.setAttribute( 'data-deva', orig );
            translatableSpans.push( span );
          } );
        }
        // Re-apply current script to newly loaded content
        if ( currentScript !== 'deva' ) {
          applyScript( currentScript );
        }
        watchTocActive();
      }, 150 );
    } );
  }

  // ── Fallback for non-MW environments ───────────────────────────
  if ( document.readyState === 'loading' ) {
    document.addEventListener( 'DOMContentLoaded', init );
  } else {
    init();
  }

}() );