forked from xifangczy/cat-catch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson.js
85 lines (80 loc) · 2.63 KB
/
json.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
// url 参数解析
const params = new URL(location.href).searchParams;
var _url = params.get("url");
// const _referer = params.get("referer");
const _requestHeaders = params.get("requestHeaders");
// 修改当前标签下的所有xhr的requestHeaders
let requestHeaders = JSONparse(_requestHeaders);
setRequestHeaders(requestHeaders, () => { awaitG(init); });
function init() {
$(`<style>${G.css}</style>`).appendTo("head");
var jsonContent = "";
var options = {
collapsed: true,
rootCollapsable: false,
withQuotes: false,
withLinks: true
};
if (isEmpty(_url)) {
$("#jsonCustom").show(); $("#main").hide();
$("#format").click(function () {
_url = $("#jsonUrl").val().trim();
if (isEmpty(_url)) {
let jsonText = $("#jsonText").val();
jsonContent = JSON.parse(jsonText);
renderJson();
$("#jsonCustom").hide(); $("#main").show();
return;
}
getJson(_url);
});
} else {
getJson(_url);
}
function getJson(url) {
$("#jsonCustom").hide(); $("#main").show();
$.ajax({
url: url,
dataType: "text",
}).fail(function (result) {
console.log(result);
$('#json-renderer').html(i18n.fileRetrievalFailed);
$("#collapsed").hide();
}).done(function (result) {
// console.log(result);
result = result.replace(/^try{/, "").replace(/}catch\(e\){.*}$/ig, ""); //去除try{}catch(e){}
try {
jsonContent = JSON.parse(result);
} catch (e) {
console.log(e);
let regexp = [
/^.*=({.*}).*$/,
/^.*\(({.*})\).*$/
]
for (let regex of regexp) {
let res = new RegExp(regex, "ig").exec(result);
if (res) {
// console.log(res);
result = res[1];
break;
}
}
// console.log(result);
jsonContent = JSON.parse(result);
}
renderJson();
});
}
function renderJson() {
$('#json-renderer').jsonViewer(jsonContent, options);
}
$("#collapsed").click(function () {
options.collapsed = !options.collapsed;
if (options.collapsed) {
collapsed.innerHTML = i18n.expandAllNodes;
} else {
collapsed.innerHTML = i18n.collapseAllNodes;
}
renderJson();
});
}