Skip to content

Instantly share code, notes, and snippets.

@danilopopeye
Created October 2, 2008 16:53
Show Gist options
  • Save danilopopeye/14394 to your computer and use it in GitHub Desktop.
Save danilopopeye/14394 to your computer and use it in GitHub Desktop.
Parse and check your JSON
CmdUtils.CreateCommand({
name: "json-lint",
homepage: "http://ubiquity.danilosousa.net/",
author: { name: "Danilo Sousa", email: "[email protected]" },
takes: { "json code": noun_arb_text },
icon: "chrome://ubiquity/content/icons/page_code.png",
description: "Parse and check your JSON.",
preview: function( previewBlock, directObject ) {
var code = directObject.text;
var url = "http://www.jsonlint.com/ajax/validate";
var params = {
json: code,
reformat: "yes"
};
jQuery.post( url, params, function( data ) {
context.focusedWindow.dataJSONLint = data;
previewBlock.innerHTML = "";
if( data.responseCode ){
var bg = "#FBE3E4";
var font = "#D12F19";
var border = "#FBC2C4";
} else {
var bg = "#E6EFC2";
var font = "#529214";
var border = "#C6D880";
}
var doc = context.focusedWindow.document;
var div = doc.createElement( "div" );
div.style.background = bg + " none repeat scroll 0 0";
div.style.border = "2px solid " + border;
div.style.color = font;
div.style.padding = "5px";
div.style.lineHeight = "1.5";
div.innerHTML = data.result;
previewBlock.appendChild( div );
},"json");
},
execute:function(directObj) {
var data = context.focusedWindow.dataJSONLint;
if( !data.responseCode ){
CmdUtils.setSelection( "<pre>" + data.prettyJSON + "</pre>" )
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment