Skip to content

Instantly share code, notes, and snippets.

@elcritch
Last active July 25, 2016 01:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save elcritch/8d9217f0f33c3010819fb04b6f8b7199 to your computer and use it in GitHub Desktop.
Save elcritch/8d9217f0f33c3010819fb04b6f8b7199 to your computer and use it in GitHub Desktop.
JSON viewer hack
function codify() {
$('span:contains("{")').each(function() {
var text = $(this).ignore().text();
var nodes = $(this).find("*").clone();
var res = text.replace(/(\{.+\})/, function (x) {
var s = '</span><code class="lang-json">';
s += "" + x + "";
s += '</code><br><span class="non-code">';
return '<span class="non-code">' + s + '</span>';
});
$(this).html(res);
});
}
function jsonViewify() {
$('code.lang-json').each(function() {
console.log("jsoncode: ", this);
var code_node = $(this);
var jsStr = code_node.text();
try {
var json = JSON.parse(jsStr);
$(this).JSONView(json, { collapsed: true });
} catch (e) {
console.error("parsing bug", e);
};
});
};
function jsonHackRun() {
if (typeof $.fn.ignore === "undefined") {
$.fn.ignore = function(sel){
return this.clone().find(sel||">*").remove().end();
};
}
codify();
jsonViewify();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment