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. */ | ||
− | + | mw.hook('wikipage.content').add(function ($content) { | |
− | function | + | var observer = new MutationObserver(function(mutations) { |
− | function | + | if (document.contains($content)) { |
− | if ( | + | 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 { | } else { | ||
− | console.log( | + | console.log("Still waiting"); |
− | |||
− | |||
− | |||
} | } | ||
− | } | + | }); |
− | + | observer.observe(document, {attributes: false, childList: true, characterData: false, subtree: true}); | |
− | } | + | }); |
− |
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}); });