Created
December 19, 2012 08:52
-
-
Save hongru/4335379 to your computer and use it in GitHub Desktop.
Json Format in document
This file contains hidden or 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
//background js | |
console.log('JSON Page Format started!') | |
;(function () { | |
var txt = document.body.innerText.trim(), | |
easyReg = /^{.+}$|^(\w+)\(({.+})\)$/; | |
var gIndentStyle=' '; | |
var arr = txt.match(easyReg), jsonTxt; | |
if (!arr) return; | |
if (arr) { | |
jsonTxt = arr[2] || arr[0]; | |
} | |
try { | |
console.log(JSON.parse(jsonTxt)); | |
var _html = FormatJSON(JSON.parse(jsonTxt)); | |
if (arr[1]) { | |
_html = arr[1] + '(' + _html + ')'; | |
} | |
document.body.style.cssText = 'font-family:Verdana; font-size:12px; line-height:1.6' | |
document.body.innerHTML = _html; | |
} catch (e) {} | |
// test json format | |
function FormatJSON(oData, sIndent) { | |
if (arguments.length < 2) { | |
var sIndent = ""; | |
} | |
var sIndentStyle = gIndentStyle; | |
var sDataType = RealTypeOf(oData); | |
// open object | |
if (sDataType == "array") { | |
if (oData.length == 0) { | |
return "[]"; | |
} | |
var sHTML = "["; | |
} else { | |
var iCount = 0; | |
Object.keys(oData).forEach(function() { | |
iCount++; | |
return; | |
}); | |
if (iCount == 0) { // object is empty | |
return "{}"; | |
} | |
var sHTML = "{"; | |
} | |
// loop through items | |
var iCount = 0; | |
var regex=/(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;\(\)]*[-A-Z0-9+&@#\/%=~_|])/gim; | |
for (var sKey in oData) { | |
var vValue = oData[sKey]; | |
if (iCount > 0) { | |
sHTML += ","; | |
} | |
if (sDataType == "array") { | |
sHTML += ("<br>" + sIndent + sIndentStyle); | |
} else { | |
sHTML += ("<br>" + sIndent + sIndentStyle + "<b>\"" + sKey + "\"</b>" + ": "); | |
} | |
// display relevant data type | |
switch (RealTypeOf(vValue)) { | |
case "array": | |
case "object": | |
sHTML += FormatJSON(vValue, (sIndent + sIndentStyle)); | |
break; | |
case "boolean": | |
case "number": | |
sHTML += "<font color=green>"+vValue.toString()+"</font>"; | |
break; | |
case "null": | |
sHTML += "<font color=red>null</font>"; | |
break; | |
case "string": | |
var s=vValue; | |
s=s.replace(regex,'<a href="$1" target="_blank">$1</a>'); | |
sHTML += ("\""+s+"\""); | |
break; | |
default: | |
sHTML += ("TYPEOF: " + typeof(vValue)); } | |
// loop | |
iCount++; | |
} | |
// close object | |
if (sDataType == "array") { | |
sHTML += ("<br>" + sIndent + "]"); | |
} else { | |
sHTML += ("<br>" + sIndent + "}"); | |
} | |
// return | |
return sHTML; | |
} | |
function RealTypeOf(v) { | |
if (typeof(v) == "object") { | |
if (v === null) return "null"; | |
if (v.constructor == (new Array).constructor) return "array"; | |
if (v.constructor == (new Date).constructor) return "date"; | |
if (v.constructor == (new RegExp).constructor) return "regex"; | |
return "object"; | |
} | |
return typeof(v); | |
} | |
function SortObject(oData) { | |
var oNewData = {}; | |
var aSortArray = []; | |
// sort keys | |
Object.keys(oData).forEach(function(sKey) { | |
aSortArray.push(sKey); | |
}); | |
aSortArray.sort(SortLowerCase); | |
// create new data object | |
aSortArray.forEach(aSortArray, function(i) { | |
if( RealTypeOf(oData[(aSortArray[i])]) == "object" ) { | |
oData[(aSortArray[i])] = SortObject(oData[(aSortArray[i])]); | |
} | |
oNewData[(aSortArray[i])] = oData[(aSortArray[i])]; | |
}); | |
return oNewData; | |
function SortLowerCase(a,b) { | |
a = a.toLowerCase(); | |
b = b.toLowerCase(); | |
return ((a < b) ? -1 : ((a > b) ? 1 : 0)); | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment