Difference between revisions of "MediaWiki:Common.js"

From CUGC Wiki
(Use MutationObserver to detect when $content is added to the DOM.)
(Remove debugging statements.)
 
(6 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. */
  
mw.hook('wikipage.content').add(function ($content) {
+
function waitForMathJax($content) {
   var observer = new MutationObserver(function(mutations) {
+
   if (typeof MathJax === 'undefined') {
    if (document.contains($content)) {
+
    setTimeout(function () { waitForMathJax($content); }, 1000);
      console.log("It's in the DOM!");
+
  } else {
      // using $content directly throws errors
+
    MathJax.Hub.Queue(["Typeset", MathJax.Hub, $content[0]]).execute();
      MathJax.Hub.Queue(["Typeset", MathJax.Hub, document.getElementById("wikiPreview")]).execute();
+
  }
      observer.disconnect();
+
}
    } else {
+
 
      console.log("Still waiting");
+
mw.hook('wikipage.content').add(waitForMathJax);
    }
 
  });
 
  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);