Skip to content

Instantly share code, notes, and snippets.

@falvhb
Created January 11, 2017 10:28
Show Gist options
  • Save falvhb/70ced3a7220730838d91dcfe51d84022 to your computer and use it in GitHub Desktop.
Save falvhb/70ced3a7220730838d91dcfe51d84022 to your computer and use it in GitHub Desktop.
Format pre JSON in page
//Nice formatting of all JSON objects
var divs = document.querySelectorAll('pre.json');
[].forEach.call(divs, function(elm) {
// do whatever
eval('var json = ' + elm.innerHTML);
elm.innerHTML = JSON.stringify(json, undefined, 2);
});
//Allow to easily add CSS rules
var sheet = (function() {
var style = document.createElement("style");
// WebKit hack :(
style.appendChild(document.createTextNode(""));
document.head.appendChild(style);
return style.sheet;
})();
//Add CSS rules
sheet.addRule('pre.json', 'font-size:12px; line-height: 14px; padding:10px; border: 1px solid #EEE;');
sheet.addRule('pre.json:before', 'content: "JSON"; position: relative; display: block; float: right; color: grey;');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment