main.js (1467B)
1 function init() { 2 var input = document.getElementById("input"); 3 var math = document.getElementById("math"); 4 var permalink = document.getElementById("permalink"); 5 6 if ("oninput" in input) { 7 input.addEventListener("input", reprocess, false); 8 } else { 9 input.attachEvent("onkeyup", reprocess); 10 } 11 12 if ("addEventListener" in permalink) { 13 permalink.addEventListener("click", setSearch); 14 } else { 15 permalink.attachEvent("click", setSearch); 16 } 17 18 var match = (/(?:^\?|&)text=([^&]*)/).exec(window.location.search); 19 if (match) { 20 input.value = decodeURIComponent(match[1]); 21 } 22 23 var macros = {}; 24 var options = {}; 25 var macroRegex = /(?:^\?|&)(?:\\|%5[Cc])([A-Za-z]+)=([^&]*)/g; 26 var macroString = ""; 27 while ((match = macroRegex.exec(window.location.search)) !== null) { 28 options.macros = macros; 29 macros["\\" + match[1]] = decodeURIComponent(match[2]); 30 macroString += "&" + match[0].substr(1); 31 } 32 33 reprocess(); 34 35 function setSearch() { 36 window.location.search = 37 "?text=" + encodeURIComponent(input.value) + macroString; 38 } 39 40 function reprocess() { 41 try { 42 katex.render(input.value, math, options); 43 } catch (e) { 44 if (e.__proto__ == katex.ParseError.prototype) { 45 console.error(e); 46 } else { 47 throw e; 48 } 49 } 50 } 51 } 52 53 init();