Created
January 11, 2017 10:28
-
-
Save falvhb/70ced3a7220730838d91dcfe51d84022 to your computer and use it in GitHub Desktop.
Format pre JSON in page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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