MediaWiki:Common.js: Difference between revisions
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) | ||
* | |||
* 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 () { | ||
/ | var LS_SCRIPT_KEY = 'grantha_reader_script'; | ||
function | var currentScript = 'deva'; | ||
var | |||
// ── 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 = []; | |||
var | |||
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; | |||
if ( | /* 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; | 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 | |||
setTimeout( function () { | * 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 ); | |||
} ); | |||
} | } | ||
if ( | var saved = ( function () { | ||
try { return localStorage.getItem( LS_SCRIPT_KEY ); } catch ( e ) { return null; } | |||
}() ); | |||
if ( saved && saved !== 'deva' ) { | |||
applyScript( saved ); | |||
} else { | } 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 ); | |||
} ); | |||
( function () { | // ── 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(); | |||
} | |||
}() ); | }() ); | ||
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();
}
}() );