Guest User

Untitled

a guest
Jun 18th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. function prettify() {
  2. function output(inp) {
  3. document.body.appendChild(document.createElement('pre')).innerHTML = inp;
  4. }
  5.  
  6. function syntaxHighlight(json) {
  7. json = json.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
  8. return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {
  9. var cls = 'number';
  10. if (/^"/.test(match)) {
  11. if (/:$/.test(match)) {
  12. cls = 'key';
  13. } else {
  14. cls = 'string';
  15. }
  16. } else if (/true|false/.test(match)) {
  17. cls = 'boolean';
  18. } else if (/null/.test(match)) {
  19. cls = 'null';
  20. }
  21. return '<span class="' + cls + '">' + match + '</span>';
  22. });
  23. }
  24.  
  25. var tag = document.createElement("style");
  26. tag.type = "text/css";
  27. document.getElementsByTagName("head")[0].appendChild(tag);
  28. tag[(typeof document.body.style.WebkitAppearance == "string") ? "innerText" : "innerHTML"] = "pre{outline:#ccc solid 1px;padding:5px;margin:5px}.string{color:green}.number{color:#ff8c00}.boolean{color:#00f}.null{color:#ff00ff}.key{color:red;}"
  29.  
  30. // output(syntaxHighlight(JSON.parse(document.body.innerText.replace("'", "\""))));
  31.  
  32. $.get(window.location.href).done(function(data) {
  33. output(syntaxHighlight(JSON.stringify(data, null, 2)));
  34. });
  35. }
Add Comment
Please, Sign In to add comment