wyatt8740

Javascript - pretty-print and uglify JSON strings

Jun 16th, 2016
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Prettifier: */
  2. /* short form bookmarklet */
  3. javascript:(function(){window.open('about:blank'); document.open(); document.write('<html>\n<body>\n<pre>\n' + JSON.stringify(eval('('+prompt("Enter your JSON string for prettification.","")+')'),null,2) + '\n</pre>\n</body>\n</html>'); document.close();})();
  4.  
  5. /* long form */
  6. document.open();
  7. document.write('<html>\n<body>\n<pre>\n' + JSON.stringify(eval('('+prompt("Enter your JSON string for prettification.","")+')'),null,2) + '\n</pre>\n</body>\n</html>');
  8. document.close();
  9.  
  10. /* Uglifier: */
  11. /* this was suprisingly waay harder than prettifying. prompt() can't handle multiple lines so I had to improvise. Also I had to get a working regex that was valid in JavaScript that would strip spaces while leaving them alone inside quotes.*/
  12. /* short form bookmarklet */
  13. javascript:(function(){window.open('about:blank'); document.open(); document.write('<html>\n<body>\n<script type="text/javascript">function stripNewLine(){\nvar str1=document.getElementById("area1").value;\n document.open();\n document.write("<html>\\n<body>\\n<pre>" + str1.replace(/\\r?\\n|\\r/g,""\).replace(/(?:\\s+\)|((?:"(?:.+?)")|(?:\'(?:.+?)\'))/g,"$1") + "</pre>\\n</body>\\n</html>");\n document.close();\n}\n</script>\n<pre><textarea id="area1" name="area1" rows="25" cols="80"></textarea>\n<br/>\n<button onclick="stripNewLine()">Un-prettify</button>\n</body>\n</html>'); document.close();})();
  14.  
  15. /* long form */
  16. document.open();
  17. document.write('<html>\n<body>\n<script type="text/javascript">function stripNewLine(){\nvar str1=document.getElementById("area1").value;\n document.open();\n document.write("<html>\\n<body>\\n<pre>" + str1.replace(/\\r?\\n|\\r/g,""\).replace(/(?:\\s+\)|((?:"(?:.+?)")|(?:\'(?:.+?)\'))/g,"$1") + "</pre>\\n</body>\\n</html>");\n document.close();\n}\n</script>\n<pre><textarea id="area1" name="area1" rows="25" cols="80"></textarea>\n<br/>\n<button onclick="stripNewLine()">Un-prettify</button>\n</body>\n</html>');
  18. document.close();
Advertisement
Add Comment
Please, Sign In to add comment