Guest User

MathJax on Wikipedia/Reddit

a guest
Jun 2nd, 2013
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. // ==UserScript==
  3. // @name           MathJax in Wikipedia and Reddit
  4. // @namespace      http://www.mathjax.org/
  5. // @description    Insert MathJax into Wikipedia and Reddit pages
  6. // @include        http://en.wikipedia.org/wiki/*
  7. // @include        http://www.reddit.com/*
  8. // ==/UserScript==
  9.  
  10. /* this is a slight modification of http://docs.mathjax.org/en/latest/_static/mathjax_wikipedia.user.js to work on Reddit pages */
  11.  
  12. var CDN_url = "https://c328740.ssl.cf1.rackcdn.com/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML-full";
  13.  
  14. if ((window.unsafeWindow == null ? window : unsafeWindow).MathJax == null) {
  15.   if(document.location.href.indexOf('wikipedia.org') != -1) WikiImages();
  16.   if(document.location.href.indexOf('reddit.com') != -1) uncode();
  17.  
  18.   inlineConfig();
  19.   MathJax();
  20. }
  21.  
  22. //
  23. //  Replace the images with MathJax scripts of type math/tex
  24. //
  25. function WikiImages() {
  26.   var images = document.getElementsByTagName('img'), count = 0;
  27.   for (var i = images.length - 1; i >= 0; i--) {
  28.     var img = images[i];
  29.     if (img.className === "tex") {
  30.       var script = document.createElement("script"); script.type = "math/tex";
  31.       if (window.opera) {script.innerHTML = img.alt} else {script.text = img.alt}
  32.       img.parentNode.replaceChild(script,img); count++;
  33.     }
  34.   }
  35.   return count;
  36. }
  37.  
  38. //
  39. // Unescape <code> surrounding TeX the World brackets [; ;]
  40. //
  41. function uncode() {
  42.   var code = document.getElementsByTagName("code");
  43.   for (var i = 0; i < code.length; ) {
  44.     var cd = code.item(i);
  45.     if (!cd.innerHTML.match(/\[;[\s\S]+;\]/)) {
  46.       ++i; continue;
  47.     }
  48.    
  49.     var span = document.createElement('span');
  50.     span.innerHTML = cd.innerHTML;
  51.     cd.parentNode.replaceChild(span,cd);
  52.   }
  53. }
  54.  
  55. function inlineConfig() {
  56.   var head = document.getElementsByTagName("head")[0], script;
  57.   script = document.createElement("script");
  58.   script.type = "text/x-mathjax-config";
  59.   script[(window.opera ? "innerHTML" : "text")] =
  60.     "MathJax.Hub.Config({\n" +
  61.     "  tex2jax: { inlineMath: [ ['[;',';]'], ['\\\\(','\\\\)'] ]}\n" +
  62.     "});"
  63.   head.appendChild(script);
  64. }
  65.  
  66. function MathJax() {
  67.   var head = document.getElementsByTagName("head")[0], script;
  68.   script = document.createElement("script");
  69.   script.type = "text/javascript";
  70.   script.src  = CDN_url;
  71.   head.appendChild(script);
  72. }
Add Comment
Please, Sign In to add comment