MathJax for All

Uses MathJax to properly display LaTeX code, on any site

  1. // ==UserScript==
  2. // @name MathJax for All
  3. // @version 0.4
  4. // @description Uses MathJax to properly display LaTeX code, on any site
  5. // @namespace 1993.uk
  6. // @match https://mail.google.com/*
  7. // @match file:///*
  8. // ==/UserScript==
  9. if (window.MathJax === undefined) {
  10. var mjscr = document.createElement("script");
  11. mjscr.type = "text/javascript";
  12. mjscr.src = "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js";
  13. mjscr.text = 'MathJax.Hub.Config({extensions:["tex2jax.js"],'+"tex2jax:{inlineMath:[['$','$']]"
  14. +',skipTags:["script","noscript","pre","code"]},jax:["input/TeX","output/CommonHTML"],'
  15. +'TeX:{extensions:["autoload-all.js","noUndefined.js"]}'
  16. +'});MathJax.Hub.Startup.onload();';
  17. document.getElementsByTagName("head")[0].appendChild(mjscr);
  18.  
  19. (new MutationObserver(function(mutationsList, observer) {
  20. let el = [];
  21. for (const mutation of mutationsList) {
  22. const node = mutation.addedNodes.item(0);
  23. if (node === null || node.nodeType !== 1 || node.className == "MathJax_Preview" || node.id.substring(0,7) == "MathJax" || node.className.substring(0,4) == "mjx-" || node.isContentEditable) break;
  24. if (node.offsetParent !== null && node.innerText != "") el.push(node);
  25. }
  26. if (el.length != 0) MathJax.Hub.Queue(["Typeset", MathJax.Hub,el,{}]);
  27. })).observe(document,{subtree: true, childList: true});
  28. }