Facebook
From Sexy Macaque, 8 Years ago, written in JavaScript.
Embed
Download Paste or View Raw
Hits: 411
  1. <script>
  2.                                 var DataTypes = ["abstract", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue",
  3.                                 "debugger", "default", "delete", "do", "double", "else", "enum", "export", "extends", "false",
  4.                                 "final", "finally", "float", "for", "function", "goto", "if", "implements", "import", "in",
  5.                                 "instanceof", "int", "interface", "long", "native", "new", "null", "package", "private",
  6.                                 "protected", "public", "return", "short", "static", "super", "switch", "synchronized", "this",
  7.                                 "throw", "throws", "transient", "true", "try", "typeof", "var", "void", "volatile", "while", "with", "length", "random", "Math", "for("];
  8.  
  9.  
  10.                                 for(var t = 0 ; t < DataTypes.length ; ++t){
  11.                                     var type = DataTypes[t];
  12.                                     //var color = "#" + Math.random().toString(16).slice(2, 8);
  13.                                         //if()color="#c70183";
  14.                                         //else  color="#000000";
  15.                                     code = document.getElementById("code").innerHTML; //wez <p> element z kodem
  16.                                     var matches = getMatches(type, code);            //wez indeksy pierwszych pojawien sie wyrazu int w tekscie
  17.                                        
  18.                                     for(var i = matches.length-1 ; i >= 0 ; --i){   // przelec wszystkie pojawienia sie od tylu. Od tylu, bo jak dodasz do tekstu "<span style='color:red'>" to sie tekst wydluzy i te indeksy beda juz nieaktualne                            
  19.                                        
  20.                                         var m = matches[i];                           // indeks pojawienia sie int w tekscie
  21.                                         code = spliceSplit(code, m+type.length, 0, "</b></span>") // ta funkcja dodaje tekst w okreslonym indeksie, paramtry ( w jakim tekscie, na ktorym indeksie, to niewazne, co dodac)
  22.                                         if (type=='for' || type=='var' || type=='length' || type=='function' || type=='return' || type=='int' || type=='boolean' || type=='short' || type=='long' || type=='return') color="#800000";
  23.                                         else color="#000000";
  24.                                         code = spliceSplit(code, m, 0, "<span style='color:"+color+"'><b>")
  25.                                         //console.log(code);
  26.                                     }
  27.                                
  28.  
  29.                                     document.getElementById("code").innerHTML = code;   //podmien caly tekst w htmlu  zmienionym tekstem
  30.                                 }
  31.  
  32.                                 function spliceSplit(str, index, count, add) {
  33.                                     var ar = str.split('');
  34.                                     ar.splice(index, count, add);
  35.                                     return ar.join('');
  36.                                 }
  37.  
  38.                                 function getMatches(needle, haystack) {
  39.                                     var myRe = new RegExp("\\b" + needle + "\\b((?!\\W(?=\\w))|(?=\\s))", "gi"),
  40.                                             myArray, myResult = [];
  41.                                     while ((myArray = myRe.exec(haystack)) !== null) {
  42.                                         myResult.push(myArray.index);
  43.                                     }
  44.                                     return myResult;
  45.                                 }
  46.                             </script>