MediaWiki:Common.js: Difference between revisions

No edit summary
No edit summary
Line 758: Line 758:
         var saved = (function () { try { return localStorage.getItem(LS_SCRIPT_KEY); } catch (e) { return null; } }());
         var saved = (function () { try { return localStorage.getItem(LS_SCRIPT_KEY); } catch (e) { return null; } }());
         if (saved && saved !== 'deva') { applyScript(saved); } else { currentScript = 'deva'; }
         if (saved && saved !== 'deva') { applyScript(saved); } else { currentScript = 'deva'; }
        watchNewContent();
         setTimeout(setupToc, 200);
         setTimeout(setupToc, 200);
     }
     }
Line 766: Line 767:
     });
     });


    var _translitBusy = false;
    function tagNewContent(container) {
        if (!container || !container.nodeType) return;
        _translitBusy = true;
        try {
            var walker = document.createTreeWalker(container, NodeFilter.SHOW_TEXT);
            var nodes = [];
            while (walker.nextNode()) nodes.push(walker.currentNode);
            nodes.forEach(function (node) {
                var p = node.parentNode;
                if (!p || (p.hasAttribute && p.hasAttribute('data-deva'))) return;
                if (p.closest && (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 = currentScript !== 'deva' ? transliterateText(orig, currentScript) : orig;
                p.replaceChild(span, node);
                translatableSpans.push(span);
            });
        } finally { _translitBusy = false; }
    }
     window.addEventListener('gr-new-content', function (e) {
     window.addEventListener('gr-new-content', function (e) {
         var container = e && e.detail && e.detail.container;
         tagNewContent(e && e.detail && e.detail.container);
        if (!container) return;
        var walker = document.createTreeWalker(container, NodeFilter.SHOW_TEXT);
        var nodes = [];
        while (walker.nextNode()) nodes.push(walker.currentNode);
        nodes.forEach(function (node) {
            var p = node.parentNode;
            if (!p || (p.hasAttribute && p.hasAttribute('data-deva'))) return;
            var orig = node.textContent;
            if (!orig.trim()) return;
            var span = document.createElement('span');
            span.setAttribute('data-deva', orig);
            span.textContent = currentScript !== 'deva' ? transliterateText(orig, currentScript) : orig;
            p.replaceChild(span, node);
            translatableSpans.push(span);
        });
     });
     });
    // Safety net: anything else that adds text to the page later — an
    // interleaved commentary, a sub-commentary, an edit preview — is picked
    // up and put into the reader's chosen script, rather than being left in
    // Devanagari because it wasn't there when the page was first tagged.
    function watchNewContent() {
        if (!window.MutationObserver) return;
        var content = document.querySelector('.mw-parser-output');
        if (!content || content.getAttribute('data-gr-translit-watch')) return;
        content.setAttribute('data-gr-translit-watch', '1');
        var queued = [], timer = null;
        function flush() {
            timer = null;
            var items = queued; queued = [];
            items.forEach(function (n) { if (document.body.contains(n)) tagNewContent(n); });
        }
        new MutationObserver(function (muts) {
            if (_translitBusy) return;
            muts.forEach(function (m) {
                Array.prototype.forEach.call(m.addedNodes, function (n) {
                    var el = n.nodeType === 1 ? n : (n.nodeType === 3 ? n.parentNode : null);
                    if (!el || el.nodeType !== 1) return;
                    if (el.hasAttribute && el.hasAttribute('data-deva')) return;
                    queued.push(el);
                });
            });
            if (queued.length && !timer) timer = setTimeout(flush, 60);
        }).observe(content, { childList: true, subtree: true });
    }


     try {
     try {
Line 1,592: Line 1,629:
             });
             });
             $verseEl.after($wrap);
             $verseEl.after($wrap);
            // Tell the script switcher about the new text, so an interleaved
            // commentary is transliterated like the rest of the page instead
            // of staying in Devanagari.
            try {
                window.dispatchEvent(new CustomEvent('gr-new-content', { detail: { container: $wrap[0] } }));
            } catch (e) { }
             injectedAny = true;
             injectedAny = true;
         });
         });