-
Notifications
You must be signed in to change notification settings - Fork 0
/
advanced-json-view.js
233 lines (200 loc) · 7.27 KB
/
advanced-json-view.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
const DEBUG_ENABLED = true;
const ADVANCED_JSON_VIEW = 'AdvancedJSONView: ';
const JSON_REGEX = /^\s*([\[\{].*[\}\]])\s*$/;
const JSONP_REGEX = /^[\s\u200B\uFEFF]*([\w$\[\]\.]+)[\s\u200B\uFEFF]*\([\s\u200B\uFEFF]*([\[{][\s\S]*[\]}])[\s\u200B\uFEFF]*\);?[\s\u200B\uFEFF]*$/;
if (isJson()) {
main();
} else {
log('Terminated. The data is not of type json/jsonp.');
}
/**
* Only call this when the file is of type json
*/
function main() {
var controlsDiv =
'<div class="controls">' +
'<div id="not-found-property-error">Error: This property does not exist. Please try again.</div>' +
'<button id="showPathToProperty">Go to</button><input id="pathToProperty"></input>' +
'<button id="clearPathToProperty">Clear</button>' +
'<text class="text-white"> ---- </text>' +
'<button id="collapseAll">Collapse all</button>' +
'<button id="expandAll">Expand all</button>' +
'<div class="right">' +
'<input id="autoPreserveViewStateCheckbox" type="checkbox" checked></input><label for="autoPreserveViewStateCheckbox">Auto-preserve view state</label>' +
' <label for="autoPreserveViewStateCheckbox">(status: <span id="saveViewStateStatus"></span>)</label>' +
'</div>' +
'</div>';
document.body.innerHTML = controlsDiv + constructPrettifiedOutputOfJson();
}
/**
* Log messages
* @param msg
*/
function log(msg) {
if (DEBUG_ENABLED) {
console.log(ADVANCED_JSON_VIEW + msg);
}
}
/**
* @returns {boolean} - true if the view file of type JSON
*/
function isJson() {
this.data = document.body.innerHTML;
this.uri = document.location.href;
this.data = this.data.replace(/^\s+|\s+$/g,'').replace(/\n/g, '');
// Pre-process data step
if(/^\<pre.*\>(.*)\<\/pre\>$/.test(this.data)){
log("data is wrapped in <pre>...</pre>, stripping HTML...");
this.data = this.data.replace(/<(?:.|\s)*?>/g, ''); //Aggressively strip HTML.
}
// Check data against JSON regex
var is_json = JSON_REGEX.test(this.data);
var is_jsonp = JSONP_REGEX.test(this.data);
log('is_json='+is_json+' is_jsonp='+is_jsonp);
return is_json || is_jsonp;
}
/**
* @returns {string} - prettified output of the global json data
*/
function constructPrettifiedOutputOfJson() {
/**
* This class is extended from Firefox JSONView (https://github.com/bhollis/jsonview)
* @constructor
*/
function JSONFormatter() {}
JSONFormatter.prototype = {
htmlEncode: function (t) {
return t != null ? t.toString().replace(/&/g,'&').replace(/'/g,'"').replace(/</g,'<').replace(/>/g,'>') : '';
},
decorateWithSpan: function (value, className) {
return '<span class="' + className + '">' + this.htmlEncode(value) + '</span>';
},
getPath: function (parentPath, prop) {
return (parentPath === ''? prop: parentPath + "." + prop);
},
// Convert a basic JSON datatype (number, string, boolean, null, object, array) into an HTML fragment.
valueToHTML: function(value, parentPath) {
var valueType = typeof value;
var output = "";
if (value == null) {
output += this.decorateWithSpan('null', 'null');
}
else if (value && value.constructor == Array) {
output += this.arrayToHTML(value, parentPath);
}
else if (valueType == 'object') {
output += this.objectToHTML(value, parentPath);
}
else if (valueType == 'number') {
output += this.decorateWithSpan(value, 'num', parentPath);
}
else if (valueType == 'string') {
if (/^(http|https):\/\/[^\s]+$/.test(value)) {
value = this.htmlEncode(value);
output += '<a href="' + value + '">' + value + '</a>';
} else {
output += this.decorateWithSpan('"' + value + '"', 'string');
}
}
else if (valueType == 'boolean') {
output += this.decorateWithSpan(value, 'bool');
}
return output;
},
// Convert an array into an HTML fragment
arrayToHTML: function(json, parentPath) {
var output = '[<ul class="array collapsible">';
var hasContents = false;
for ( var prop in json ) {
var newParentPath = this.getPath(parentPath, prop);
hasContents = true;
output += '<li id="' + newParentPath + '">';
output += this.valueToHTML(json[prop], newParentPath);
output += '</li>';
}
output += '</ul>]';
if ( ! hasContents ) {
output = "[ ]";
}
return output;
},
// Convert a JSON object to an HTML fragment
objectToHTML: function(json, parentPath) {
var output = '{<ul class="obj collapsible">';
var hasContents = false;
for ( var prop in json ) {
var newParentPath = this.getPath(parentPath, prop);
hasContents = true;
output += '<li id="' + newParentPath + '">';
output += '<span class="prop">' + this.htmlEncode(prop) + '</span>: ';
output += this.valueToHTML(json[prop], newParentPath);
output += '</li>';
}
output += '</ul>}';
if ( ! hasContents ) {
output = "{ }";
}
return output;
},
// Convert a whole JSON object into a formatted HTML document.
jsonToHTML: function(json, callback, uri) {
var output = '';
if( callback ){
output += '<div class="callback">' + callback + ' (</div>';
output += '<div id="json">';
}else{
output += '<div id="json">';
}
output += this.valueToHTML(json, '');
output += '</div>';
if( callback ){
output += '<div class="callback">)</div>';
}
return this.toHTML(output, uri);
},
// Produce an error document for when parsing fails.
errorPage: function(error, data, uri) {
var output = '<div id="parsing-json-error">Error parsing JSON: '+error.message+'</div>';
output += '<h1>'+error.stack+':</h1>';
output += '<div id="json">' + this.htmlEncode(data) + '</div>';
return this.toHTML(output, uri + ' - Error');
},
// Wrap the HTML fragment in a full document. Used by jsonToHTML and errorPage.
toHTML: function(content, title) {
return '<doctype html>' +
'<html><head><title>' + title + '</title>' +
'<link rel="stylesheet" type="text/css" href="'+chrome.extension.getURL("advanced-json-view.css")+'">' +
'<script type="text/javascript" src="'+chrome.extension.getURL("default.js")+'"></script>' +
'</head><body>' +
content +
'</body></html>';
}
};
// To sanitize and build the DOM
this.jsonFormatter = new JSONFormatter();
var outputDoc = '';
var cleanData = '',
callback = '';
var callback_results = JSONP_REGEX.exec(this.data);
if( callback_results && callback_results.length == 3 ){
log("JSONp detected");
callback = callback_results[1];
cleanData = callback_results[2];
} else {
log("Vanilla JSON detected");
cleanData = this.data;
}
log(cleanData);
try {
var jsonObj = JSON.parse(cleanData);
if ( jsonObj ) {
outputDoc = this.jsonFormatter.jsonToHTML(jsonObj, callback, this.uri);
} else {
throw "There was no object!";
}
} catch(e) {
log(e);
outputDoc = this.jsonFormatter.errorPage(e, this.data, this.uri);
}
return outputDoc;
}