Difference between revisions of "MediaWiki:Common.js"

From CUGC Wiki
(Use MutationObserver to detect when $content is added to the DOM.)
(Debugging $content.)
Line 2: Line 2:
  
 
mw.hook('wikipage.content').add(function ($content) {
 
mw.hook('wikipage.content').add(function ($content) {
 +
  console.log($content);
 +
  console.log(typeof $content);
 
   var observer = new MutationObserver(function(mutations) {
 
   var observer = new MutationObserver(function(mutations) {
 
     if (document.contains($content)) {
 
     if (document.contains($content)) {

Revision as of 16:45, 12 March 2019

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

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