Difference between revisions of "MediaWiki:Common.js"

From CUGC Wiki
(Get element from $content.)
(Wait for MathJax before typesetting.)
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) {
 +
  if (typeof MathJax === 'undefined') {
 +
    setTimeout(waitForMathJax, 1000);
 +
    console.log('Waiting for MathJax.');
 +
  } else {
 +
    MathJax.Hub.Queue(["Typeset", MathJax.Hub, element]).execute();
 +
    console.log('Found MathJax; typeset done.');
 +
  }
 +
}
  
 
mw.hook('wikipage.content').add(function ($content) {
 
mw.hook('wikipage.content').add(function ($content) {
  console.log($content);
 
  console.log($content[0]);
 
 
   var element = $content[0];
 
   var element = $content[0];
 
   var observer = new MutationObserver(function(mutations) {
 
   var observer = new MutationObserver(function(mutations) {
 
     if (document.contains(element)) {
 
     if (document.contains(element)) {
 
       console.log("It's in the DOM!");
 
       console.log("It's in the DOM!");
      MathJax.Hub.Queue(["Typeset", MathJax.Hub, element]).execute();
 
 
       observer.disconnect();
 
       observer.disconnect();
 +
      waitForMathJax(element);
 
     } else {
 
     } else {
 
       console.log("Still waiting");
 
       console.log("Still waiting");

Revision as of 16:49, 12 March 2019

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

function waitForMathJax(element) {
  if (typeof MathJax === 'undefined') {
    setTimeout(waitForMathJax, 1000);
    console.log('Waiting for MathJax.');
  } else {
    MathJax.Hub.Queue(["Typeset", MathJax.Hub, element]).execute();
    console.log('Found MathJax; typeset done.');
  }
}

mw.hook('wikipage.content').add(function ($content) {
  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});
});