Guest User

quick mod of http://userscripts.org/scripts/show/81977

a guest
Jul 22nd, 2010
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name           TeX to Image (Isaac's Version)
  3. // @namespace      http://math.stackexchange.com/
  4. // @description    Turns TeX markup surrounded by '$' into images via the Google Chart API, or any other service.
  5. // @include        http://math.stackexchange.com/*
  6. // @include        http://meta.math.stackexchange.com/*
  7. // @include        http://stats.stackexchange.com/*
  8. // @include        http://meta.stats.stackexchange.com/*
  9. // @exclude        http://*.stackexchange.com/*/edit
  10. // ==/UserScript==
  11.  
  12. if (typeof GM_deleteValue == 'undefined') {
  13.     GM_deleteValue = function(name) {
  14.         localStorage.removeItem(name);
  15.     }
  16.  
  17.     GM_getValue = function(name, defaultValue) {
  18.         var value = localStorage.getItem(name);
  19.         if (!value)
  20.             return defaultValue;
  21.         var type = value[0];
  22.         value = value.substring(1);
  23.         switch (type) {
  24.             case 'b':
  25.                 return value == 'true';
  26.             case 'n':
  27.                 return Number(value);
  28.             default:
  29.                 return value;
  30.         }
  31.     }
  32.  
  33.     GM_log = function(message) {
  34.         console.log(message);
  35.     }
  36.  
  37.     GM_setValue = function(name, value) {
  38.         value = (typeof value)[0] + value;
  39.         localStorage.setItem(name, value);
  40.     }
  41. }
  42.  
  43. var document = unsafeWindow.document;
  44.  
  45. var baseUrls = [
  46.     { name : "Google Charts", url : "http://chart.apis.google.com/chart?cht=tx&chf=bg,s,FFFFFF00&chl=" },
  47.     { name : "72Pines", url : "http://tex.72pines.com/latex.php?latex=" },
  48.     { name : "Mathtran.org", url : "http://www.mathtran.org/cgi-bin/mathtran?D=1;tex=" }
  49.   ];
  50.  
  51. var baseUrl = GM_getValue('baseUrl', baseUrls[0].url);
  52.  
  53. for (var i = 0; i < baseUrls.length; i++) {
  54.     var url = baseUrls[i];
  55.     var selected = url.url == baseUrl;
  56.    
  57.     GM_registerMenuCommand((selected ? "[\u2713]  " : "[   ]  ") + "Use " + url.name, createMenuCommand(url.url));
  58. }
  59.  
  60. function createMenuCommand(url) {
  61.     return function () {
  62.         GM_setValue('baseUrl', url);
  63.         window.location.href = window.location.href;
  64.     }
  65. }
  66.  
  67. function goGoGadgetMath() {
  68.  
  69.     var nodes = getTextNodes();
  70.    
  71.     var splitRegex = /(\$[^$]+\$)/g;
  72.     var extractRegex = /^\$([^$]+)\$$/g;
  73.  
  74.     for (var i = 0; i < nodes.length; i++) {
  75.         var node = nodes[i];
  76.         if (splitRegex.test(node.data)) {
  77.             var parent = node.parentNode;
  78.            
  79.             var sections = node.data.split(splitRegex);
  80.            
  81.             for (var s = 0; s < sections.length; s++) {
  82.                 var newNode = null;
  83.                
  84.                 if (!splitRegex.test(sections[s])) {
  85.                     newNode = document.createTextNode(sections[s]);
  86.                 } else {
  87.                     var TeX = sections[s].replace(extractRegex, '$1');
  88.                    
  89.                     var img = document.createElement('img');
  90.                     img.setAttribute("src", baseUrl + encodeURIComponent(TeX));
  91.                     img.setAttribute("alt", "$" + TeX + "$");
  92.                    
  93.                     newNode = img;
  94.                 }
  95.                
  96.                 parent.insertBefore(newNode, node);
  97.             }
  98.            
  99.             parent.removeChild(node);
  100.         }
  101.     }
  102. }
  103.  
  104. function getTextNodes(node) {
  105.     var currentNode = (node || document.body);
  106.    
  107.     var ret = [];
  108.    
  109.     var children = currentNode.childNodes;
  110.     for (var i = 0; i < children.length; i++)
  111.     {
  112.         var child = children[i];
  113.        
  114.         if (child.nodeType == 3) {
  115.             GM_log(child.data);
  116.             ret.push(child);
  117.         } else {
  118.             var name = child.nodeName.toLowerCase();
  119.            
  120.             if (name == 'head' ||
  121.                 name == 'script' ||
  122.                 name == 'iframe' ||
  123.                 name == 'style' ||
  124.                 name == 'title' ||
  125.                 name == 'meta' ||
  126.                 name == 'textarea' ||
  127.                 name == 'object') {
  128.                 continue;
  129.             }
  130.            
  131.             var subNodes = getTextNodes(child);
  132.            
  133.             for (var j = 0; j < subNodes.length; j++) {
  134.                 ret.push(subNodes[j]);
  135.             }
  136.         }
  137.     }
  138.    
  139.     return ret;
  140. }
  141.  
  142. goGoGadgetMath();
Add Comment
Please, Sign In to add comment