MediaWiki:Common.js: Difference between revisions

No edit summary
No edit summary
Line 217: Line 217:
   // ── Init ───────────────────────────────────────────────────────
   // ── Init ───────────────────────────────────────────────────────
   function init() {
   function init() {
     tagTextNodes();
     var content = document.querySelector('.mw-parser-output');
    var alreadyTagged = content && content.querySelector('[data-deva]');
    if (!alreadyTagged) {
      translatableSpans = [];
      tagTextNodes();
    }
     buildBar();
     buildBar();
     watchTocActive();
     watchTocActive();
   }
   }


  // MW hook fires when page content is ready (including after MW re-renders)
  var hooked = false;
   if (window.mw) {
   if (window.mw) {
     mw.hook('wikipage.content').add(function () {
     mw.hook('wikipage.content').add(function () {
       var oldBar = document.querySelector('.gr-script-bar');
       setTimeout(function () {
      if (oldBar) oldBar.remove();
        var oldBar = document.querySelector('.gr-script-bar');
      if (!hooked) {
        if (oldBar) oldBar.remove();
         // First run — tag nodes and build everything
 
         hooked = true;
         // Check if content is already tagged from a previous hook fire
         translatableSpans = [];
         var content = document.querySelector('.mw-parser-output');
         setTimeout(init, 100);
         var alreadyTagged = content && content.querySelector('[data-deva]');
      } else {
 
         // Subsequent MW re-renders — nodes are already tagged,
         if (!alreadyTagged) {
        // just rebuild the bar and re-attach the TOC observer
          translatableSpans = [];
         setTimeout(function () {
          tagTextNodes();
          buildBar();
         }
          watchTocActive();
 
        }, 100);
         buildBar();
      }
        watchTocActive();
      }, 150);
     });
     });
   }
   }