Pretty JSON

Make JSON text look better

Du musst eine Erweiterung wie Tampermonkey, Greasemonkey oder Violentmonkey installieren, um dieses Skript zu installieren.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

Sie müssten eine Skript Manager Erweiterung installieren damit sie dieses Skript installieren können

(Ich habe schon ein Skript Manager, Lass mich es installieren!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name        Pretty JSON
// @namespace   qixinglu.com
// @description Make JSON text look better
// @include     http://*/*.json
// @grant       none
// @version 0.0.1.20140517140354
// ==/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, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
    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 '<span class="' + cls + '">' + match + '</span>';
    }
    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 = '<pre>' +
                          highlight(format(document.body.innerHTML)) +
                          '</pre>';