Difference between revisions of "MediaWiki:Common.js"

From CUGC Wiki
(Don't use $content as there are errors in MathJax using it (TypeError: c.nodeName is undefined).)
(Use MutationObserver to detect when $content is added to the DOM.)
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. */
  
// MathJax typesetting in edit previews
+
mw.hook('wikipage.content').add(function ($content) {
function mathJaxTypeset($content) {
+
   var observer = new MutationObserver(function(mutations) {
   function waitForMathJax() {
+
     if (document.contains($content)) {
     if (typeof MathJax === 'undefined') {
+
       console.log("It's in the DOM!");
       setTimeout(waitForMathJax, 1000);
+
       // using $content directly throws errors
       console.log('Waiting for MathJax.');
+
      MathJax.Hub.Queue(["Typeset", MathJax.Hub, document.getElementById("wikiPreview")]).execute();
 +
      observer.disconnect();
 
     } else {
 
     } else {
       console.log($content);
+
       console.log("Still waiting");
      // delay as wikipage.content is fired before $content is added to the page
 
      setTimeout(MathJax.Hub.Queue(["Typeset", MathJax.Hub, "wikiPreview"]).execute, 1000);
 
      console.log('Found MathJax; typeset done.');
 
 
     }
 
     }
   }
+
   });
   waitForMathJax();
+
   observer.observe(document, {attributes: false, childList: true, characterData: false, subtree: true});
}
+
});
mw.hook('wikipage.content').add(mathJaxTypeset);
 

Revision as of 16:40, 12 March 2019

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

mw.hook('wikipage.content').add(function ($content) {
  var observer = new MutationObserver(function(mutations) {
    if (document.contains($content)) {
      console.log("It's in the DOM!");
      // using $content directly throws errors
      MathJax.Hub.Queue(["Typeset", MathJax.Hub, document.getElementById("wikiPreview")]).execute();
      observer.disconnect();
    } else {
      console.log("Still waiting");
    }
  });
  observer.observe(document, {attributes: false, childList: true, characterData: false, subtree: true});
});