-
Notifications
You must be signed in to change notification settings - Fork 0
/
atk-panel.js
190 lines (159 loc) · 4.67 KB
/
atk-panel.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
/*
* Manage the ATK developper panel inside chrome developper window.
*
* This panel will listen for ajax request and display
* request and response infotmation from the request.
*
*/
/**
* Main entry when dom is ready.
*/
jQuery(function(jQuery){
setupPanel(jQuery);
});
// check and display atkui-js version when opening.
chrome.devtools.inspectedWindow.eval("atk.version()", function(result, error){
document.getElementById('atk-version').innerHTML = ' ' + result + ' ';
});
chrome.devtools.network.onRequestFinished.addListener(function(request){
if (isXhrRequest(request)) {
setPanelRequest(request.request);
request.getContent(function(content){
//setResponseFromContent(content);
setPanelResponse(getResponseFromContent(content));
});
} else {
setPanelResponse({});
setPanelRequest({});
}
});
/**
* Update panel section with callback request information.
*
* @param request
*/
function setPanelRequest(request) {
var qString = '', pData = '';
if (request.queryString && request.queryString.length > 0) {
request.queryString.forEach(function(item){
qString = qString + getTrForItemValue(item);
});
}
if (request.postData && request.postData.params && request.postData.params.length > 0 ) {
request.postData.params.forEach(function(item){
pData = pData + getTrForItemValue(item);
});
}
jQuery('#atk-request-url').html(request.url ? request.url.replace(/\?.*$/,"") : '');
jQuery('#atk-header-query > tbody').html(qString);
jQuery('#atk-header-data > tbody').html(pData);
}
/**
* Return an html string table row.
*
* @param item
* @returns {string}
*/
function getTrForItemValue(item) {
return '<tr><td>'+ item.name +'</td><td>'+item.value+'</td></tr>';
}
/**
* Update panel with callback response information.
*/
function setPanelResponse(response) {
if (typeof response.success != 'undefined') {
jQuery('#atk-response-status').html(jQuery('<span>').addClass('ui empty circular label').addClass(response.success ? 'green' : 'red'));
}
else {
jQuery('#atk-response-status').html('');
}
jQuery('#atk-html').html(response.html ? response.html : '');
jQuery('#atk-rawhtml').text(response.html ? formatFactory(response.html.trim()) : '');
jQuery('#atk-atkjs').text(response.atkjs ? response.atkjs : '');
jQuery('#atk-response-id').text(response.id ? response.id : '');
jQuery('#atk-atkjs').parents('figure').toggle(response.atkjs ? true : false);
jQuery('#atk-rawhtml').parents('figure').toggle(response.html ? true : false);
jQuery('pre code').each(function(i, block) {
hljs.highlightBlock(block);
});
//Prism.highlightAll();
}
/**
* Get response from callback received content.
* Parse received json content and return proper object.
*
* @param content
*/
function getResponseFromContent(content) {
try {
var response;
response = JSON.parse(content);
response.atkjs = response.atkjs ? response.atkjs.replace(/<\/?script>/g, '') : '';
return response;
} catch (e) {
console.log(e);
}
}
/**
* Check if request is from ajax.
*
* @param req
* @returns {boolean}
*/
function isXhrRequest(req) {
return req.request.headers.filter(function(item){
return (item.name === 'X-Requested-With') && (item.value === 'XMLHttpRequest');
}).length > 0;
}
/**
* Panel init.
*
* @param jQuery
*/
function setupPanel(jQuery) {
jQuery('.menu .item').tab();
setPanelResponse({});
}
/**
* Pretty print html.
*
* @param html
* @returns {String}
*/
function formatFactory(html) {
function parse(html, tab = 2) {
var tab;
var html = jQuery.parseHTML(html);
var formatHtml = new String();
function setTabs () {
var tabs = new String();
for (i=0; i < tab; i++){
tabs += ' ';
}
return tabs;
};
function trimIt(toTrim) {
return toTrim ? toTrim.trim() : '';
}
jQuery.each( html, function( i, el ) {
if (el.nodeName == '#text') {
if (trimIt(jQuery(el).text())) {
formatHtml += setTabs() + trimIt(jQuery(el).text()) + '\n';
}
} else {
var innerHTML = trimIt(jQuery(el).html());
jQuery(el).html(innerHTML.replace('\n', '').replace(/ +(?= )/g, ''));
if (jQuery(el).children().length) {
jQuery(el).html('\n' + parse(innerHTML, (tab + 1)) + setTabs());
var outerHTML = trimIt(jQuery(el).prop('outerHTML'));
formatHtml += setTabs() + outerHTML + '\n';
} else {
var outerHTML = trimIt(jQuery(el).prop('outerHTML'));
formatHtml += setTabs() + outerHTML + '\n';
}
}
});
return formatHtml;
};
return parse(html.replace(/(\r\n|\n|\r)/gm," ").replace(/ +(?= )/g,''));
};