Difference between revisions of "MediaWiki:Common.js"

From CUGC Wiki
(Wait for MathJax before typesetting.)
(Remove debugging statements.)
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
/* Any JavaScript here will be loaded for all users on every page load. */
 
/* Any JavaScript here will be loaded for all users on every page load. */
  
function waitForMathJax(element) {
+
function waitForMathJax($content) {
 
   if (typeof MathJax === 'undefined') {
 
   if (typeof MathJax === 'undefined') {
     setTimeout(waitForMathJax, 1000);
+
     setTimeout(function () { waitForMathJax($content); }, 1000);
    console.log('Waiting for MathJax.');
 
 
   } else {
 
   } else {
     MathJax.Hub.Queue(["Typeset", MathJax.Hub, element]).execute();
+
     MathJax.Hub.Queue(["Typeset", MathJax.Hub, $content[0]]).execute();
    console.log('Found MathJax; typeset done.');
 
 
   }
 
   }
 
}
 
}
  
mw.hook('wikipage.content').add(function ($content) {
+
mw.hook('wikipage.content').add(waitForMathJax);
  var element = $content[0];
 
  var observer = new MutationObserver(function(mutations) {
 
    if (document.contains(element)) {
 
      console.log("It's in the DOM!");
 
      observer.disconnect();
 
      waitForMathJax(element);
 
    } else {
 
      console.log("Still waiting");
 
    }
 
  });
 
  observer.observe(document, {attributes: false, childList: true, characterData: false, subtree: true});
 
});
 

Latest revision as of 16:55, 12 March 2019

/* Any JavaScript here will be loaded for all users on every page load. */

function waitForMathJax($content) {
  if (typeof MathJax === 'undefined') {
    setTimeout(function () { waitForMathJax($content); }, 1000);
  } else {
    MathJax.Hub.Queue(["Typeset", MathJax.Hub, $content[0]]).execute();
  }
}

mw.hook('wikipage.content').add(waitForMathJax);