MediaWiki:Common.js: Difference between revisions
No edit summary |
No edit summary |
||
| Line 2: | Line 2: | ||
var currentScript = 'deva'; | var currentScript = 'deva'; | ||
// ── IAST transliteration (inline, no external library) ───────── | // ── IAST transliteration (inline, no external library) ───────── | ||
| Line 113: | Line 112: | ||
var translatableSpans = []; | var translatableSpans = []; | ||
function tagTextNodes() { | |||
var content = document.querySelector('.mw-parser-output'); | |||
if (!content) return; | |||
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; | |||
// Skip already tagged | |||
if (p.hasAttribute && p.hasAttribute('data-deva')) return; | |||
// Skip UI elements | |||
if (p.closest) { | |||
if (p.closest('.gr-script-bar') || | |||
p.closest('.vector-toc') || | |||
p.closest('#toc') || | |||
p.closest('.mw-editsection')) return; | |||
} | } | ||
var orig = node.textContent; | |||
if (!orig.trim()) return; | |||
// Wrap in span with original stored | |||
var span = document.createElement('span'); | |||
span.setAttribute('data-deva', orig); | |||
span.textContent = orig; | |||
p.replaceChild(span, node); | |||
translatableSpans.push(span); | |||
}); | }); | ||
} | |||
} | |||
function applyScript(script) { | function applyScript(script) { | ||
| Line 229: | Line 217: | ||
// ── Init ─────────────────────────────────────────────────────── | // ── Init ─────────────────────────────────────────────────────── | ||
function init() { | function init() { | ||
tagTextNodes(); | tagTextNodes(); | ||
buildBar(); | buildBar(); | ||
| Line 236: | Line 222: | ||
} | } | ||
// | // MW hook fires when page content is ready | ||
if (window.mw) { | if (window.mw) { | ||
mw.hook('wikipage.content').add(function () { | mw.hook('wikipage.content').add(function () { | ||
translatableSpans = []; | translatableSpans = []; | ||
var oldBar = document.querySelector('.gr-script-bar'); | |||
if (oldBar) oldBar.remove(); | |||
setTimeout(init, 100); | setTimeout(init, 100); | ||
}); | }); | ||
} | |||
// Fallback for non-MW environments | |||
if (document.readyState === 'loading') { | |||
document.addEventListener('DOMContentLoaded', init); | |||
} else { | } else { | ||
init(); | |||
} | } | ||
})(); | })(); | ||