forked from ccampbell/chromelogger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.js
234 lines (192 loc) · 5.96 KB
/
log.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
234
/* globals chrome, console */
/**
* code for handling all the logging magic
*
* @author Craig Campbell <[email protected]>
*/
(function() {
'use strict';
/**
* @var string
*/
var HEADER_NAMES = ['x-chromelogger-data', 'x-chromephp-data'];
/**
* @var object
*/
var local_storage = null;
var color1 = '#888';
var color2 = '#0563ad';
var ALLOWED_TYPES = {
'group': 1,
'groupEnd': 1,
'groupCollapsed': 1,
'warn': 1,
'error': 1,
'info': 1,
'table': 1,
'log': 1
};
/**
* @var array
*/
var queue = [];
/**
* @var bool
*/
var use_queue = true;
/**
* should we show line numbers?
*
* @return bool
*/
function _showLineNumbers()
{
return local_storage.show_line_numbers === "true";
}
/**
* logs nicely formatted data in new format
*
* @param Object
* @return void
*/
function _logData(data, callback)
{
var column_map = {};
var column_name;
for (var key in data.columns) {
column_name = data.columns[key];
column_map[column_name] = key;
}
var rows = data.rows,
i = 0,
length = rows.length;
for (i = 0; i < length; i++) {
var row = rows[i],
backtrace = row[column_map.backtrace],
label = row[column_map.label],
log = row[column_map.log],
type = row[column_map.type] || 'log';
if (_showLineNumbers() && backtrace !== null) {
console.log('%c' + backtrace, 'color: ' + color1 + '; font-weight: bold;');
}
// new version without label
var new_version = false;
if (data.columns.indexOf('label') === -1) {
new_version = true;
}
// if this is the old version do some converting
if (!new_version) {
var show_label = label && typeof label === "string";
log = [log];
if (show_label) {
log.unshift(label);
}
}
var logs = [];
var current_log;
var last_log;
var new_string;
// loop through logs to add in any class name labels that should be here
for (var j = 0; j < log.length; j++) {
current_log = log[j];
last_log = logs[logs.length - 1];
if (current_log && typeof current_log === 'object' && current_log['___class_name']) {
new_string = '%c' + current_log['___class_name'];
if (typeof last_log === 'string') {
// if the last log was a string we need to append to it
// in order for the coloring to work correctly
logs[logs.length - 1] = last_log + ' ' + new_string;
}
else {
// otherwise just push the new string to the end of the list
logs.push(new_string);
}
logs.push('color: ' + color2 + '; font-weight: bold;');
delete log[j]['___class_name'];
}
logs.push(current_log);
}
if (!(type in ALLOWED_TYPES)) {
type = 'log';
}
console[type].apply(console, logs);
}
if (typeof callback === 'function') {
callback();
}
}
function _processQueue(callback)
{
for (var i = 0; i < queue.length; ++i) {
_process(queue[i]);
}
queue = [];
callback();
}
function _decode(header) {
return JSON.parse(atob(header));
}
function _process(details) {
var headers = details.responseHeaders;
for (var i = 0; i < headers.length; i++) {
if (HEADER_NAMES.indexOf(headers[i].name.toLowerCase()) !== -1) {
_logData(_decode(headers[i].value));
return;
}
}
}
function _handleHeaderUpdate(request, sender, sendResponse) {
// if this is not a header update don't do anything
if (request.name != "header_update") {
return;
}
if (use_queue) {
queue.push(request.details);
return sendResponse("done");
}
_process(request.details);
return sendResponse("done");
}
function _listenForLogMessages() {
chrome.extension.onMessage.addListener(_handleHeaderUpdate);
}
function _stopListening() {
chrome.extension.onMessage.removeListener(_handleHeaderUpdate);
}
function _run() {
_processQueue(function() {
use_queue = false;
});
}
function _initStorage() {
chrome.extension.sendMessage("localStorage", function(response) {
local_storage = response;
color1 = 'color1' in local_storage ? local_storage['color1'] : color1;
color2 = 'color2' in local_storage ? local_storage['color2'] : color2;
_run();
});
}
function _init() {
_listenForLogMessages();
chrome.extension.sendMessage('isActive', function(response) {
if (response === false) {
return _stopListening();
}
return _initStorage();
});
chrome.extension.sendMessage('ready', function(queuedRequests) {
if (queuedRequests) {
for (var i = 0; i < queuedRequests.length; i++) {
if (use_queue) {
queue.push(queuedRequests[i]);
continue;
}
else {
_process(queuedRequests[i]);
}
}
}
});
}
_init();
}) ();