Jump to content

MediaWiki:Common.js: Difference between revisions

From Grantha
No edit summary
Tag: Reverted
No edit summary
 
(30 intermediate revisions by the same user not shown)
Line 1: Line 1:
/* =========================
/* MediaWiki:Common.js — grantha.io  (v6)
  VERSE ACTIONS
*
========================= */
* Changes vs v5:
*  1. TOC active highlight fix: Vector 2022 sets the active class on the
*    <li.vector-toc-list-item>, but Common.js wraps all text nodes inside
*    .vector-toc-text spans in <span data-deva="…">.  The CSS colour rule
*    was targeting .vector-toc-list-item-active > a which never matched
*    because the <a> sits deeper and its text is now inside a <span>.
*    Fix: MutationObserver watches the <li> for class changes and directly
*    applies/removes the orange colour via inline style on the .vector-toc-link
*    ancestor, bypassing the CSS specificity war entirely.
*  2. All other behaviour identical to v5.
*/
 
( function () {
( function () {
  'use strict';


   /* ── Safe delegated listener ─────────────────────────────────────────── */
   var LS_SCRIPT_KEY = 'grantha_reader_script';
   function onDocClick( e ) {
  var currentScript = 'deva';
     var target = e.target;
 
  // ── 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;
  }


    // Walk up to find a matching action button
  // ── Character maps for Kannada / Tamil ─────────────────────────
     function closest( el, cls ) {
  var SCRIPT_MAP = {
       while ( el && el !== document ) {
    kn: {
        if ( el.classList && el.classList.contains( cls ) ) return el;
      'अ':'ಅ','आ':'ಆ','इ':'ಇ','ई':'ಈ','उ':'ಉ','ऊ':'ಊ','ऋ':'ಋ',
        el = el.parentNode;
      'ए':'ಏ','ऐ':'ಐ','ओ':'ಓ','औ':'ಔ','ऽ':'ಽ',
       }
      'क':'ಕ','ख':'ಖ','ग':'ಗ','घ':'ಘ','ङ':'ಙ',
       return null;
      'च':'ಚ','छ':'ಛ','ज':'ಜ','झ':'ಝ','ञ':'ಞ',
      'ट':'ಟ','ठ':'ಠ','ड':'ಡ','ढ':'ಢ','ण':'ಣ',
      'त':'ತ','थ':'ಥ','द':'ದ','ध':'ಧ','न':'ನ',
      'प':'ಪ','फ':'ಫ','ब':'ಬ','भ':'ಭ','म':'ಮ',
      'य':'ಯ','र':'ರ','ल':'ಲ','व':'ವ',
      'श':'ಶ','ष':'ಷ','स':'ಸ','ह':'ಹ',
      'ा':'ಾ','ि':'ಿ','ी':'ೀ','ु':'ು','ू':'ೂ',
      'ृ':'ೃ','े':'ೇ','ै':'ೈ','ो':'ೋ','ौ':'ೌ',
      'ं':'ಂ','ः':'ಃ','्':'್',
      '०':'೦','१':'೧','२':'೨','३':'೩','४':'೪',
      '५':'೫','६':'೬','७':'೭','८':'೮','९':'೯'
     },
    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( '' );
  }


    /* ── Commentary toggle ── */
  // ── Tag all transliteratable text nodes once per page ───────────
var commentBtn = closest( target, 'verse-action-commentary' );
   var translatableSpans = [];
if ( commentBtn ) {
  e.preventDefault();
   var verseId = commentBtn.getAttribute( 'data-verse' );
  if ( !verseId ) return;


   // Match all commentary bodies for this verse by id prefix
   function tagTextNodes() {
  var allBodies = document.querySelectorAll( '[id^="commentary-body-' + verseId + '"]' );
    var content = document.querySelector( '.mw-parser-output' );
  var allBtns  = document.querySelectorAll( '.verse-action-commentary[data-verse="' + verseId + '"]' );
    if ( content ) {
      var walker = document.createTreeWalker( content, NodeFilter.SHOW_TEXT );
      var nodes  = [];
      while ( walker.nextNode() ) nodes.push( walker.currentNode );


  var isOpen = allBodies.length && allBodies[0].classList.contains( 'open' );
      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 );
      } );
    }


  // Close all open commentaries on the page first
    // Tag .vector-toc-text spans for TOC transliteration
  document.querySelectorAll( '.commentary-body.open' ).forEach( function ( el ) {
    document.querySelectorAll( '.vector-toc .vector-toc-text' ).forEach( function ( span ) {
    el.classList.remove( 'open' );
      if ( span.hasAttribute( 'data-deva' ) ) return;
  } );
      var orig = span.textContent;
  document.querySelectorAll( '.verse-action-commentary.active' ).forEach( function ( el ) {
      if ( !orig.trim() ) return;
     el.classList.remove( 'active' );
      span.setAttribute( 'data-deva', orig );
   } );
      translatableSpans.push( span );
     } );
   }


   // If it was closed, open it
   // ── Apply a script to all tagged spans ─────────────────────────
   if ( !isOpen ) {
   function applyScript( script ) {
     allBodies.forEach( function ( el ) { el.classList.add( 'open' ); } );
     currentScript = script;
    allBtns.forEach(   function ( el ) { el.classList.add( 'active' ); } );
    translatableSpans.forEach( function ( span ) {
      if ( !span.parentNode ) return;
      var orig = span.getAttribute( 'data-deva' );
      if ( !orig ) return;
      span.textContent = ( script === 'deva' )
        ? orig
        : transliterateText( orig, script );
    } );
   }
   }
  return;
}


    /* ── Copy verse — copies verse ID for crosslinking ── */
  // ── TOC active-item highlight ────────────────────────────────────
var copyBtn = closest( target, 'verse-action-copy' );
  // FIX: Instead of relying on CSS :active selectors (which fail because
if ( copyBtn ) {
  // Common.js wraps text nodes in <span data-deva>, making > a or > .link
   e.preventDefault();
  // selectors not match the coloured text), we use a MutationObserver to
  var verseId = copyBtn.getAttribute( 'data-verse' );
  // watch each <li> for class changes and imperatively apply/remove the
  if ( !verseId ) return;
  // orange colour via inline style on the .vector-toc-link inside it.
  copyText( verseId, copyBtn );
  // This is immune to DOM depth and span nesting.
  return;
   function watchTocActive() {
}
    var toc = document.querySelector( '.vector-toc' );
     /* ── Copy ID ── */
    if ( !toc ) return;
    var idBtn = closest( target, 'copy-id-btn' );
    if ( !window.MutationObserver ) return;
     if ( idBtn ) {
     /* Guard only the structObserver — attach it once per toc element.
       e.preventDefault();
      Per-<li> attachment is already guarded by _grHighlightAttached,
       var id = idBtn.getAttribute( 'data-copyid' );
      so calling watchTocActive() multiple times is safe and picks up
      if ( !id ) return;
      any <li> items that weren't in the DOM on the first call. */
      copyText( id, idBtn );
     if ( toc._grObserved ) {
       /* Re-scan for any <li> items that arrived after first call */
       toc.querySelectorAll( '.vector-toc-list-item' ).forEach( attachHighlight );
       return;
       return;
     }
     }
   }
    toc._grObserved = true;
 
    var ACTIVE_COLOR  = '#f57c00';
    var ACTIVE_WEIGHT = '700';
 
    function setLinkActive( li, active ) {
      var link = li.querySelector( '.vector-toc-link' ) || li.querySelector( 'a' );
      if ( !link ) return;
      if ( active ) {
        link.style.setProperty( 'color',      ACTIVE_COLOR,  'important' );
        link.style.setProperty( 'font-weight', ACTIVE_WEIGHT, 'important' );
        /* Colour ALL descendant elements — covers both:
        *   • data-deva <span> wrappers (Devanagari mode, textContent replaced)
        *  • .vector-toc-text / .vector-toc-numb spans (all scripts)
        * After applyScript() runs for non-Deva scripts, textContent is a
        * plain text node so querySelectorAll('span') still finds the
        * structural spans (.vector-toc-text etc.) which need colouring. */
        link.querySelectorAll( '*' ).forEach( function ( el ) {
          el.style.setProperty( 'color', ACTIVE_COLOR, 'important' );
        } );
      } else {
        link.style.removeProperty( 'color' );
        link.style.removeProperty( 'font-weight' );
        link.querySelectorAll( '*' ).forEach( function ( el ) {
          el.style.removeProperty( 'color' );
        } );
      }
    }
 
    function attachHighlight( li ) {
      if ( li._grHighlightAttached ) return;
      li._grHighlightAttached = true;
      liObserver.observe( li, { attributes: true, attributeFilter: [ 'class' ] } );
      /* Apply immediately if already active on attachment */
      if ( li.classList.contains( 'vector-toc-list-item-active' ) ) {
        setLinkActive( li, true );
      }
    }


  document.addEventListener( 'click', onDocClick );
    var liObserver = new MutationObserver( function ( mutations ) {
      mutations.forEach( function ( m ) {
        if ( m.attributeName !== 'class' ) return;
        var li = m.target;
        var isActive = li.classList.contains( 'vector-toc-list-item-active' );
        setLinkActive( li, isActive );


  /* ── Copy helper + tooltip ───────────────────────────────────────────── */
        /* Scroll active item into view within the TOC container —
  function copyText( text, btn ) {
        * but ONLY when the TOC sidebar is actually visible and expanded.
    function showTooltip() {
        * If the TOC is collapsed or hidden, scrollIntoView scrolls the
      // Remove stale tooltip if double-clicked
        * whole page instead of just the TOC, which hijacks the reading
      var old = btn.querySelector( '.copy-tooltip' );
        * position. */
      if ( old ) old.remove();
        if ( isActive ) {
          var container = document.querySelector( '.vector-sticky-pinned-container' );
          if ( container ) {
            /* Check TOC is visible: the container must have nonzero height
            * and must not be hidden by the Vector "pinned/unpinned" toggle */
            var tocVisible = container.offsetHeight > 0 &&
                            container.offsetParent !== null &&
                            window.getComputedStyle( container ).display !== 'none' &&
                            window.getComputedStyle( container ).visibility !== 'hidden';
            if ( tocVisible ) {
              var liRect = li.getBoundingClientRect();
              var cRect  = container.getBoundingClientRect();
              if ( liRect.top < cRect.top + 8 || liRect.bottom > cRect.bottom - 8 ) {
                /* Find the scrollable ancestor within the TOC container.
                * Vector 2022 uses .vector-sticky-pinned-container as the
                * scroll host in some versions, and .vector-toc in others. */
                var scrollHost = null;
                var candidate  = li.parentNode;
                while ( candidate && candidate !== document.body ) {
                  if ( candidate.scrollHeight > candidate.clientHeight + 4 ) {
                    scrollHost = candidate;
                    break;
                  }
                  candidate = candidate.parentNode;
                }
                if ( scrollHost ) {
                  var targetTop = li.offsetTop - ( scrollHost.clientHeight / 2 );
                  scrollHost.scrollTop = Math.max( 0, targetTop );
                }
              }
            }
          }
        }
      } );
    } );


       btn.style.position = 'relative';
    var structObserver = new MutationObserver( function ( mutations ) {
      var tip = document.createElement( 'span' );
       mutations.forEach( function ( m ) {
      tip.className  = 'copy-tooltip';
        if ( m.type !== 'childList' ) return;
      tip.textContent = 'Copied ✓';
        m.addedNodes.forEach( function ( n ) {
      btn.appendChild( tip );
          if ( n.nodeType !== 1 ) return;
          /* Attach highlight observer to newly added list items */
          if ( n.classList && n.classList.contains( 'vector-toc-list-item' ) ) {
            attachHighlight( n );
          }
          if ( n.querySelectorAll ) {
            n.querySelectorAll( '.vector-toc-list-item' ).forEach( attachHighlight );
          }


      requestAnimationFrame( function () {
          /* Tag any new .vector-toc-text spans for transliteration */
        requestAnimationFrame( function () {
          var newSpans = [];
           tip.classList.add( 'copy-tooltip-visible' );
          if ( n.classList && n.classList.contains( 'vector-toc-text' ) ) newSpans.push( n );
          if ( n.querySelectorAll ) {
            n.querySelectorAll( '.vector-toc-text' ).forEach( function ( s ) { newSpans.push( s ); } );
           }
          newSpans.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 );
          } );
         } );
         } );
       } );
       } );
    } );
    toc.querySelectorAll( '.vector-toc-list-item' ).forEach( attachHighlight );
    structObserver.observe( toc, { childList: true, subtree: true } );


       setTimeout( function () {
    /* On initial load, colour the already-active item.
         tip.classList.remove( 'copy-tooltip-visible' );
    * scrollIntoView is intentionally skipped here — calling it while the
         setTimeout( function () { tip.remove(); }, 400 );
    * TOC sidebar might be collapsed causes the PAGE to scroll to the element
       }, 1400 );
    * rather than scrolling within the TOC container.  The liObserver handles
    * scrolling the TOC as the user scrolls the page content. */
    setTimeout( function () {
       var active = toc.querySelector( '.vector-toc-list-item-active' );
      if ( active ) {
        setLinkActive( active, true );
      }
    }, 300 );
  }
 
  // ── Init ────────────────────────────────────────────────────────
  function init() {
    /* Remove appearance panel + watchlist overflow.
    * Vector injects some of these elements via its own JS after DOMContentLoaded,
    * so we use a MutationObserver to catch them whenever they appear. */
    var HIDE_SELS = [
      '#vector-appearance',
      '#vector-appearance-pinned-container',
      '#vector-appearance-unpinned-container',
      '.mw-portlet-appearance',
      '.mw-portlet-vector-user-menu-overflow',
      '[aria-controls="vector-appearance"]'
    ];
    function removeHiddenEls() {
      HIDE_SELS.forEach( function ( sel ) {
        document.querySelectorAll( sel ).forEach( function ( el ) {
          if ( el.parentNode ) el.parentNode.removeChild( el );
        } );
      } );
    }
    removeHiddenEls();
    if ( window.MutationObserver ) {
      var hideObserver = new MutationObserver( function ( mutations ) {
        var needsClean = false;
        mutations.forEach( function ( m ) {
          if ( m.addedNodes.length ) needsClean = true;
        } );
         if ( needsClean ) removeHiddenEls();
      } );
      /* Watch only direct children of body — NOT subtree.
      * Vector appends the appearance panel as a direct child of body.
      * Using subtree:true would fire on every inner DOM change (including
      * Vector setting active classes on TOC items) and interfere with the
      * liObserver that handles active highlight colouring. */
      hideObserver.observe( document.body, { childList: true, subtree: false } );
      /* Also check the header area where Vector sometimes injects the button */
      var mwHeader = document.querySelector( '.mw-header' ) || document.querySelector( '#mw-head' );
      if ( mwHeader ) {
         hideObserver.observe( mwHeader, { childList: true, subtree: true } );
      }
      /* Stop observing after 8s — Vector will have finished by then */
      setTimeout( function () { hideObserver.disconnect(); }, 8000 );
    }
 
    var content      = document.querySelector( '.mw-parser-output' );
    var alreadyTagged = content && content.querySelector( '[data-deva]' );
    if ( !alreadyTagged ) {
       translatableSpans = [];
      tagTextNodes();
    } else {
      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 );
      } );
     }
     }


     if ( navigator.clipboard && window.isSecureContext ) {
    var saved = ( function () {
       navigator.clipboard.writeText( text ).then( showTooltip );
      try { return localStorage.getItem( LS_SCRIPT_KEY ); } catch ( e ) { return null; }
    }() );
     if ( saved && saved !== 'deva' ) {
       applyScript( saved );
     } else {
     } else {
       var ta = document.createElement( 'textarea' );
       currentScript = 'deva';
      ta.value = text;
      ta.style.cssText = 'position:fixed;opacity:0;pointer-events:none;';
      document.body.appendChild( ta );
      ta.select();
      document.execCommand( 'copy' );
      document.body.removeChild( ta );
      showTooltip();
     }
     }
    watchTocActive();
    /* Retry for Vector 2022 TOC which renders after DOMContentLoaded */
    setTimeout( watchTocActive, 300 );
    setTimeout( watchTocActive, 800 );
   }
   }


}() );
  // ── React to toolbar dropdown changes ──────────────────────────
/* ════════════════════════════════════════════════
  window.addEventListener( 'gr-script-change', function ( e ) {
  Grantha — Site chrome (hide wiki UI, inject top bar)
    var script = e && e.detail && e.detail.script;
  Edit at: yourdomain.com/wiki/MediaWiki:Common.js
    if ( script ) applyScript( script );
  ════════════════════════════════════════════════ */
  } );


( function () {
  // ── MediaWiki hook (SPA-style navigation) ───────────────────────
'use strict';
  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 {
          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 );
          } );
        }
        if ( currentScript !== 'deva' ) applyScript( currentScript );
        watchTocActive();
      }, 150 );
    } );
  }


/* Only run on NS_MAIN pages, skip Special pages */
  if ( document.readyState === 'loading' ) {
if ( mw.config.get( 'wgNamespaceNumber' ) !== 0 ) return;
    document.addEventListener( 'DOMContentLoaded', init );
  } else {
    init();
  }


/* ── 1. Inject hide-chrome CSS immediately ── */
}() );
var css = [
/* Sidebar */
'#mw-navigation,#mw-panel,.mw-sidebar,.vector-sidebar-container,',
'.vector-sidebar-container-no-toc,#p-logo,.mw-portlet-logo{display:none!important}',
/* Page action tabs */
'#p-views,#p-cactions,.mw-portlet-views,.vector-page-tools,',
'.vector-page-tools-landmark,#ca-edit,#ca-ve-edit,#ca-history,',
'#ca-talk,#ca-nstab-main,.page-actions-menu,',
'.vector-page-titlebar-toc{display:none!important}',
/* User/personal tools */
'#p-personal,.vector-user-links,.vector-user-menu,',
'#pt-createaccount,#pt-login,#pt-logout,',
'.mw-portlet-personal,.vector-client-prefs-landmark{display:none!important}',
/* Header search */
'#p-search,.vector-search-box,.mw-header .cdx-search-input{display:none!important}',
/* Top header strip */
'.vector-header-start,.vector-header-end,.mw-header{display:none!important}',
/* Footer */
'#footer,.mw-footer,.vector-footer,.page-footer{display:none!important}',
/* Misc */
'#siteNotice,#localNotice,.usermessage,#catlinks,.catlinks,',
'.printfooter,.mw-indicators{display:none!important}',
/* Remove sidebar margin, full-width content */
'#mw-content-text,#content,.mw-body,.mw-body-content,',
'.vector-body,#bodyContent{margin-left:0!important;padding-left:0!important}',
'#content,#mw-content-container,.mw-page-container,',
'.vector-page-container,.mw-body{max-width:100%!important;padding:0!important;margin:0!important}',
'#mw-content-text,#bodyContent{margin-top:0!important}',
/* Top bar */
'#grantha-topbar{',
'  position:fixed;top:0;left:0;right:0;z-index:10000;height:50px;',
'  background:#1a2e40;display:flex;align-items:center;',
'  justify-content:space-between;padding:0 24px;',
'  box-shadow:0 1px 0 rgba(255,255,255,0.06);',
'  font-family:system-ui,sans-serif;',
'}',
'#grantha-topbar .gt-brand{',
'  display:flex;align-items:center;gap:10px;text-decoration:none;',
'}',
'#grantha-topbar .gt-favicon{',
'  width:22px;height:22px;border-radius:4px;object-fit:contain;',
'  filter:brightness(0) invert(1);opacity:.9;',
'}',
'#grantha-topbar .gt-name{',
'  color:#fff;font-size:15px;font-weight:700;letter-spacing:-.01em;',
'}',
'#grantha-topbar .gt-right{display:flex;align-items:center;gap:10px;}',
'#grantha-topbar .gt-btn{',
'  font-size:12.5px;padding:6px 16px;border-radius:20px;',
'  border:1.5px solid rgba(255,255,255,0.3);',
'  background:rgba(255,255,255,0.08);color:#fff;',
'  cursor:pointer;font-family:inherit;font-weight:600;',
'  transition:background .15s,border-color .15s;white-space:nowrap;',
'}',
'#grantha-topbar .gt-btn:hover{',
'  background:rgba(255,255,255,0.18);border-color:rgba(255,255,255,0.55);',
'}',
'body{padding-top:50px!important}',
'@media print{#grantha-topbar{display:none!important}body{padding-top:0!important}}'
].join( '' );


if ( !document.getElementById( 'grantha-chrome-css' ) ) {
var style = document.createElement( 'style' );
style.id = 'grantha-chrome-css';
style.textContent = css;
( document.head || document.documentElement ).appendChild( style );
}


/* ── 2. Build the top bar ── */
// ── Main page: by-Grantha / by-Author toggle ──────────────────
mw.hook( 'wikipage.content' ).add( function () {
( function () {
if ( document.getElementById( 'grantha-topbar' ) ) return;
  function grHomeView( v ) {
    var gView = document.getElementById( 'gr-view-grantha' );
    var aView = document.getElementById( 'gr-view-author' );
    var gBtn  = document.getElementById( 'gr-toggle-grantha' );
    var aBtn  = document.getElementById( 'gr-toggle-author' );
    if ( !gView || !aView || !gBtn || !aBtn ) return;


/* Hide topbar on edit pages — the editor toolbar takes over */
    gView.style.display = ( v === 'grantha' ) ? '' : 'none';
var action = mw.config.get( 'wgAction' );
    aView.style.display = ( v === 'author' ) ? '' : 'none';
if ( action === 'edit' || action === 'submit' ) return;
    gBtn.className = 'gr-toggle-btn' + ( v === 'grantha' ? ' gr-toggle-active' : '' );
    aBtn.className = 'gr-toggle-btn' + ( v === 'author'  ? ' gr-toggle-active' : '' );
    try { localStorage.setItem( 'gr_home_view', v ); } catch ( e ) {}
  }


var mainPage = ( mw.config.get( 'wgArticlePath' ) || '/wiki/$1' )
  function initHomeToggle() {
.replace( '$1', encodeURIComponent(
    var gBtn = document.getElementById( 'gr-toggle-grantha' );
mw.config.get( 'wgMainPageTitle' ) || 'Main_Page'
    var aBtn = document.getElementById( 'gr-toggle-author' );
) );
    if ( !gBtn || !aBtn ) return;


var bar = document.createElement( 'div' );
    gBtn.addEventListener( 'click', function () { grHomeView( 'grantha' ); } );
bar.id = 'grantha-topbar';
    aBtn.addEventListener( 'click', function () { grHomeView( 'author' );  } );
bar.innerHTML =
    [ gBtn, aBtn ].forEach( function ( btn ) {
'<a class="gt-brand" href="' + mainPage + '">' +
      btn.addEventListener( 'keydown', function ( e ) {
'  <img class="gt-favicon" src="/favicon.ico" alt="" />' +
        if ( e.key === 'Enter' || e.key === ' ' ) btn.click();
' <span class="gt-name">Grantha</span>' +
      } );
'</a>' +
    } );
'<div class="gt-right">' +
' <button class="gt-btn" id="gt-newdoc">✚ New Document</button>' +
'</div>';


document.body.prepend( bar );
    var saved;
 
    try { saved = localStorage.getItem( 'gr_home_view' ); } catch ( e ) {}
document.getElementById( 'gt-newdoc' ).addEventListener( 'click', function () {
    if ( saved === 'author' ) grHomeView( 'author' );
/* NewPageDialog is provided by the Editor extension */
  }
if ( window.NewPageDialog ) NewPageDialog.show();
} );
} );


  if ( document.readyState === 'loading' ) {
    document.addEventListener( 'DOMContentLoaded', initHomeToggle );
  } else {
    initHomeToggle();
  }
}() );
}() );

Latest revision as of 10:17, 24 April 2026

/* MediaWiki:Common.js — grantha.io  (v6)
 *
 * Changes vs v5:
 *  1. TOC active highlight fix: Vector 2022 sets the active class on the
 *     <li.vector-toc-list-item>, but Common.js wraps all text nodes inside
 *     .vector-toc-text spans in <span data-deva="…">.  The CSS colour rule
 *     was targeting .vector-toc-list-item-active > a which never matched
 *     because the <a> sits deeper and its text is now inside a <span>.
 *     Fix: MutationObserver watches the <li> for class changes and directly
 *     applies/removes the orange colour via inline style on the .vector-toc-link
 *     ancestor, bypassing the CSS specificity war entirely.
 *  2. All other behaviour identical to v5.
 */

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

    // Tag .vector-toc-text spans for TOC transliteration
    document.querySelectorAll( '.vector-toc .vector-toc-text' ).forEach( function ( span ) {
      if ( span.hasAttribute( 'data-deva' ) ) return;
      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;
      var orig = span.getAttribute( 'data-deva' );
      if ( !orig ) return;
      span.textContent = ( script === 'deva' )
        ? orig
        : transliterateText( orig, script );
    } );
  }

  // ── TOC active-item highlight ────────────────────────────────────
  // FIX: Instead of relying on CSS :active selectors (which fail because
  // Common.js wraps text nodes in <span data-deva>, making > a or > .link
  // selectors not match the coloured text), we use a MutationObserver to
  // watch each <li> for class changes and imperatively apply/remove the
  // orange colour via inline style on the .vector-toc-link inside it.
  // This is immune to DOM depth and span nesting.
  function watchTocActive() {
    var toc = document.querySelector( '.vector-toc' );
    if ( !toc ) return;
    if ( !window.MutationObserver ) return;
    /* Guard only the structObserver — attach it once per toc element.
       Per-<li> attachment is already guarded by _grHighlightAttached,
       so calling watchTocActive() multiple times is safe and picks up
       any <li> items that weren't in the DOM on the first call. */
    if ( toc._grObserved ) {
      /* Re-scan for any <li> items that arrived after first call */
      toc.querySelectorAll( '.vector-toc-list-item' ).forEach( attachHighlight );
      return;
    }
    toc._grObserved = true;

    var ACTIVE_COLOR  = '#f57c00';
    var ACTIVE_WEIGHT = '700';

    function setLinkActive( li, active ) {
      var link = li.querySelector( '.vector-toc-link' ) || li.querySelector( 'a' );
      if ( !link ) return;
      if ( active ) {
        link.style.setProperty( 'color',       ACTIVE_COLOR,  'important' );
        link.style.setProperty( 'font-weight', ACTIVE_WEIGHT, 'important' );
        /* Colour ALL descendant elements — covers both:
         *   • data-deva <span> wrappers (Devanagari mode, textContent replaced)
         *   • .vector-toc-text / .vector-toc-numb spans (all scripts)
         * After applyScript() runs for non-Deva scripts, textContent is a
         * plain text node so querySelectorAll('span') still finds the
         * structural spans (.vector-toc-text etc.) which need colouring. */
        link.querySelectorAll( '*' ).forEach( function ( el ) {
          el.style.setProperty( 'color', ACTIVE_COLOR, 'important' );
        } );
      } else {
        link.style.removeProperty( 'color' );
        link.style.removeProperty( 'font-weight' );
        link.querySelectorAll( '*' ).forEach( function ( el ) {
          el.style.removeProperty( 'color' );
        } );
      }
    }

    function attachHighlight( li ) {
      if ( li._grHighlightAttached ) return;
      li._grHighlightAttached = true;
      liObserver.observe( li, { attributes: true, attributeFilter: [ 'class' ] } );
      /* Apply immediately if already active on attachment */
      if ( li.classList.contains( 'vector-toc-list-item-active' ) ) {
        setLinkActive( li, true );
      }
    }

    var liObserver = new MutationObserver( function ( mutations ) {
      mutations.forEach( function ( m ) {
        if ( m.attributeName !== 'class' ) return;
        var li = m.target;
        var isActive = li.classList.contains( 'vector-toc-list-item-active' );
        setLinkActive( li, isActive );

        /* Scroll active item into view within the TOC container —
         * but ONLY when the TOC sidebar is actually visible and expanded.
         * If the TOC is collapsed or hidden, scrollIntoView scrolls the
         * whole page instead of just the TOC, which hijacks the reading
         * position. */
        if ( isActive ) {
          var container = document.querySelector( '.vector-sticky-pinned-container' );
          if ( container ) {
            /* Check TOC is visible: the container must have nonzero height
             * and must not be hidden by the Vector "pinned/unpinned" toggle */
            var tocVisible = container.offsetHeight > 0 &&
                             container.offsetParent !== null &&
                             window.getComputedStyle( container ).display !== 'none' &&
                             window.getComputedStyle( container ).visibility !== 'hidden';
            if ( tocVisible ) {
              var liRect = li.getBoundingClientRect();
              var cRect  = container.getBoundingClientRect();
              if ( liRect.top < cRect.top + 8 || liRect.bottom > cRect.bottom - 8 ) {
                /* Find the scrollable ancestor within the TOC container.
                 * Vector 2022 uses .vector-sticky-pinned-container as the
                 * scroll host in some versions, and .vector-toc in others. */
                var scrollHost = null;
                var candidate  = li.parentNode;
                while ( candidate && candidate !== document.body ) {
                  if ( candidate.scrollHeight > candidate.clientHeight + 4 ) {
                    scrollHost = candidate;
                    break;
                  }
                  candidate = candidate.parentNode;
                }
                if ( scrollHost ) {
                  var targetTop = li.offsetTop - ( scrollHost.clientHeight / 2 );
                  scrollHost.scrollTop = Math.max( 0, targetTop );
                }
              }
            }
          }
        }
      } );
    } );

    var structObserver = new MutationObserver( function ( mutations ) {
      mutations.forEach( function ( m ) {
        if ( m.type !== 'childList' ) return;
        m.addedNodes.forEach( function ( n ) {
          if ( n.nodeType !== 1 ) return;
          /* Attach highlight observer to newly added list items */
          if ( n.classList && n.classList.contains( 'vector-toc-list-item' ) ) {
            attachHighlight( n );
          }
          if ( n.querySelectorAll ) {
            n.querySelectorAll( '.vector-toc-list-item' ).forEach( attachHighlight );
          }

          /* Tag any new .vector-toc-text spans for transliteration */
          var newSpans = [];
          if ( n.classList && n.classList.contains( 'vector-toc-text' ) ) newSpans.push( n );
          if ( n.querySelectorAll ) {
            n.querySelectorAll( '.vector-toc-text' ).forEach( function ( s ) { newSpans.push( s ); } );
          }
          newSpans.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 );
          } );
        } );
      } );
    } );

    toc.querySelectorAll( '.vector-toc-list-item' ).forEach( attachHighlight );
    structObserver.observe( toc, { childList: true, subtree: true } );

    /* On initial load, colour the already-active item.
     * scrollIntoView is intentionally skipped here — calling it while the
     * TOC sidebar might be collapsed causes the PAGE to scroll to the element
     * rather than scrolling within the TOC container.  The liObserver handles
     * scrolling the TOC as the user scrolls the page content. */
    setTimeout( function () {
      var active = toc.querySelector( '.vector-toc-list-item-active' );
      if ( active ) {
        setLinkActive( active, true );
      }
    }, 300 );
  }

  // ── Init ────────────────────────────────────────────────────────
  function init() {
    /* Remove appearance panel + watchlist overflow.
     * Vector injects some of these elements via its own JS after DOMContentLoaded,
     * so we use a MutationObserver to catch them whenever they appear. */
    var HIDE_SELS = [
      '#vector-appearance',
      '#vector-appearance-pinned-container',
      '#vector-appearance-unpinned-container',
      '.mw-portlet-appearance',
      '.mw-portlet-vector-user-menu-overflow',
      '[aria-controls="vector-appearance"]'
    ];
    function removeHiddenEls() {
      HIDE_SELS.forEach( function ( sel ) {
        document.querySelectorAll( sel ).forEach( function ( el ) {
          if ( el.parentNode ) el.parentNode.removeChild( el );
        } );
      } );
    }
    removeHiddenEls();
    if ( window.MutationObserver ) {
      var hideObserver = new MutationObserver( function ( mutations ) {
        var needsClean = false;
        mutations.forEach( function ( m ) {
          if ( m.addedNodes.length ) needsClean = true;
        } );
        if ( needsClean ) removeHiddenEls();
      } );
      /* Watch only direct children of body — NOT subtree.
       * Vector appends the appearance panel as a direct child of body.
       * Using subtree:true would fire on every inner DOM change (including
       * Vector setting active classes on TOC items) and interfere with the
       * liObserver that handles active highlight colouring. */
      hideObserver.observe( document.body, { childList: true, subtree: false } );
      /* Also check the header area where Vector sometimes injects the button */
      var mwHeader = document.querySelector( '.mw-header' ) || document.querySelector( '#mw-head' );
      if ( mwHeader ) {
        hideObserver.observe( mwHeader, { childList: true, subtree: true } );
      }
      /* Stop observing after 8s — Vector will have finished by then */
      setTimeout( function () { hideObserver.disconnect(); }, 8000 );
    }

    var content       = document.querySelector( '.mw-parser-output' );
    var alreadyTagged = content && content.querySelector( '[data-deva]' );
    if ( !alreadyTagged ) {
      translatableSpans = [];
      tagTextNodes();
    } else {
      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 saved = ( function () {
      try { return localStorage.getItem( LS_SCRIPT_KEY ); } catch ( e ) { return null; }
    }() );
    if ( saved && saved !== 'deva' ) {
      applyScript( saved );
    } else {
      currentScript = 'deva';
    }

    watchTocActive();
    /* Retry for Vector 2022 TOC which renders after DOMContentLoaded */
    setTimeout( watchTocActive, 300 );
    setTimeout( watchTocActive, 800 );
  }

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

  // ── MediaWiki hook (SPA-style navigation) ───────────────────────
  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 {
          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 );
          } );
        }
        if ( currentScript !== 'deva' ) applyScript( currentScript );
        watchTocActive();
      }, 150 );
    } );
  }

  if ( document.readyState === 'loading' ) {
    document.addEventListener( 'DOMContentLoaded', init );
  } else {
    init();
  }

}() );


// ── Main page: by-Grantha / by-Author toggle ──────────────────
( function () {
  function grHomeView( v ) {
    var gView = document.getElementById( 'gr-view-grantha' );
    var aView = document.getElementById( 'gr-view-author' );
    var gBtn  = document.getElementById( 'gr-toggle-grantha' );
    var aBtn  = document.getElementById( 'gr-toggle-author' );
    if ( !gView || !aView || !gBtn || !aBtn ) return;

    gView.style.display = ( v === 'grantha' ) ? '' : 'none';
    aView.style.display = ( v === 'author'  ) ? '' : 'none';
    gBtn.className = 'gr-toggle-btn' + ( v === 'grantha' ? ' gr-toggle-active' : '' );
    aBtn.className = 'gr-toggle-btn' + ( v === 'author'  ? ' gr-toggle-active' : '' );
    try { localStorage.setItem( 'gr_home_view', v ); } catch ( e ) {}
  }

  function initHomeToggle() {
    var gBtn = document.getElementById( 'gr-toggle-grantha' );
    var aBtn = document.getElementById( 'gr-toggle-author' );
    if ( !gBtn || !aBtn ) return;

    gBtn.addEventListener( 'click', function () { grHomeView( 'grantha' ); } );
    aBtn.addEventListener( 'click', function () { grHomeView( 'author' );  } );
    [ gBtn, aBtn ].forEach( function ( btn ) {
      btn.addEventListener( 'keydown', function ( e ) {
        if ( e.key === 'Enter' || e.key === ' ' ) btn.click();
      } );
    } );

    var saved;
    try { saved = localStorage.getItem( 'gr_home_view' ); } catch ( e ) {}
    if ( saved === 'author' ) grHomeView( 'author' );
  }

  if ( document.readyState === 'loading' ) {
    document.addEventListener( 'DOMContentLoaded', initHomeToggle );
  } else {
    initHomeToggle();
  }
}() );