// ==UserScript== // @name Pretty JSON // @namespace qixinglu.com // @description Make JSON text look better // @include http://*/*.json // @grant none // @version 0.0.1.20140517140354 // @downloadURL https://update.greasyfork.icu/scripts/1105/Pretty%20JSON.user.js // @updateURL https://update.greasyfork.icu/scripts/1105/Pretty%20JSON.meta.js // ==/UserScript== var format = function(text) { var json = JSON.parse(text); return JSON.stringify(json, null, 4); }; var highlight = function(text) { // get from http://stackoverflow.com/questions/4810841/json-pretty-print-using-javascript var json = text.replace(/&/g, '&').replace(//g, '>'); var reg = /("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g; var replace = function (match) { var cls = 'number'; if (/^"/.test(match)) { if (/:$/.test(match)) { cls = 'key'; } else { cls = 'string'; } } else if (/true|false/.test(match)) { cls = 'boolean'; } else if (/null/.test(match)) { cls = 'null'; } return '' + match + ''; } return json.replace(reg, replace); }; var addStyle = function(cssText) { var head = document.querySelector('head'); var style = document.createElement('style'); style.setAttribute('type', 'text/css'); style.textContent = cssText; head.appendChild(style); }; addStyle( 'pre {padding: 10px; margin: 10px;}' + '.string {color: green;}' + '.number {color: darkorange;}' + '.boolean {color: blue;}' + '.null {color: magenta;}' + '.key {color: red;}' ); document.body.innerHTML = '
' +
                          highlight(format(document.body.innerHTML)) +
                          '
';