This repository has been archived by the owner on Nov 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
sniffsnuff.js
373 lines (291 loc) · 7.66 KB
/
sniffsnuff.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/**
* Local print out utility.
* @param {array} args Argument list to print out.
* @param {object} args Argument list to print out.
*/
var out = function(args)
{
chrome
.devtools
.inspectedWindow
.eval('console.log("' + args + '")');
};
/**
* Parses headers
* @param {object} Request A Request object from chrome.devtools.network event
*/
var parseHeaders = function(Request, scope)
{
headers = Request.response.headers;
for (id in headers)
{
fnParseContent(headers[id], Request.request.url, 'Headers received');
}
headers = [];
headers = Request.request.headers;
for (id in headers)
{
fnParseContent(headers[id], Request.request.url, 'Headers sent');
}
};
/**
* Parses cookies
* @param {object} Request A Request object from chrome.devtools.network event
*/
var parseCookies = function(Request, scope)
{
cookies = Request.response.cookies;
for (id in cookies)
{
fnParseContent(cookies[id], Request.request.url, 'Cookies received');
}
cookies = [];
cookies = Request.request.cookies;
for (id in cookies)
{
fnParseContent(cookies[id], Request.request.url, 'Cookies sent');
}
};
// onRequestFinished
chrome.devtools.network.onRequestFinished.addListener(
function(Request)
{
var cookies, headers;
fnUpdateSearch();
if (!Request || !search.active)
return false;
//console.debug(Request);
if (options.monitorResponses)
{
Request.getContent(
function(content)
{
fnParseContent(content, Request.request.url, 'Response body');
}
);
}
if (options.monitorRequests)
{
Request.getContent(
function(content)
{
fnParseContent(content, Request.request.url, 'Request body');
}
);
}
// Parse cookies
options.monitorCookies && parseCookies(Request);
// Parse headers
options.monitorHeaders && parseHeaders(Request);
}
);
chrome.devtools.network.onNavigated.addListener(
function(url)
{
console.debug("Navigating to " + url);
}
);
/**
* Parses the response body.
* @param {string} content - Request or response context.
* @param {string} uri - URL of the resource the response body comes from.
* @param {string} context - Context in which a match has been found (e.g. cookies or headers).
*/
var fnParseContent = function(content, uri, context)
{
console.debug(context);
content && fnDoSearch(search.keyword, content, uri, context);
};
/**
* Looks for keywords in the context provided.
* @param {string} what - Needle.
* @param {string} where - Haystack.
* @param {string} uri - URL of the resource the response body comes from.
* @param {string} context - Context in which a match has been found (e.g. cookies or headers).
*/
var fnDoSearch = function(what, where, uri, context)
{
if (!(what && where))
return false
typeof where === "object" &&
(where = JSON.stringify(where))
console.debug("Looking for %s in %s", what.substr(0, 15), where.substr(0, 15));
var tableResults = document.getElementById('tableResults');
// Remove the status row
var initCell = document.getElementById('initCell');
initCell && initCell.remove();
var results;
var tr, td1, td2, td3, td4;
var re = new RegExp(what, options.ignoreCase? 'gim' : 'gm');
while (results = re.exec(where))
{
//console.debug(results);
if (!results)
return false
var match = results[0];
var index = parseInt(results['index'], 10);
var input = results['input'];
var location =
input.substring(index - search.delta,
index + match.length + search.delta);
tr = document.createElement('tr');
td1 = document.createElement('td');
td2 = document.createElement('td');
td3 = document.createElement('td');
td4 = document.createElement('td');
td3.innerHTML = fnWrapTag(match, 'code');
td4.innerHTML = fnWrapTag(location, 'textarea', 'form-control');
td1.innerText = context;
td2.innerHTML = fnWrapTag(uri, 'a', 'resource-uri');
tr.appendChild(td1);
tr.appendChild(td2);
tr.appendChild(td3);
tr.appendChild(td4);
tableResults.getElementsByTagName('tbody')[0].appendChild(tr);
fnBindResourceURILinks();
//@debug
console.debug("Found " +
match +
" in " +
location +
" at " +
uri);
} // end while
};
/**
* Adds a click event to resource links
* to open them in a DevTools panel.
*/
var fnBindResourceURILinks = function()
{
var i, elems =
document.getElementsByClassName('resource-uri');
for (i=0; i < elems.length; i++)
{
elems[i]
.addEventListener('click', cbOpenResourceURI);
}
};
var cbOpenResourceURI = function(event)
{
var uri = event.toElement.href;
event.preventDefault();
chrome.devtools.panels.openResource(uri);
};
/**
* Returns input wrapped in an XML tag.
* @param {string} input - input to wrap.
* @param {string} tag - tag to wrap with.
* @param {string} className - class name(s) to add to the tag.
* @return {string} Wrapped in tag input.
*/
var fnWrapTag = function(input, tag, className)
{
tag && (tag = tag.toString());
className && (className = className.toString());
// Little monster here
return '<' +
(tag === 'a' ? tag + ' href="' + input + '"' : tag) +
(className ? ' class="' + className + '"' : '') +
'>' +
input +
'</' + tag + '>';
}
/**
* Populates the search options through the
* UI controls
*/
var fnUpdateSearch = function()
{
options =
{
monitorRequests:
document.getElementById('cboxMonitorRequests').checked || false,
monitorResponses:
document.getElementById('cboxMonitorResponses').checked || false,
monitorHeaders:
document.getElementById('cboxMonitorHeaders').checked || false,
monitorCookies:
document.getElementById('cboxMonitorCookies').checked || false,
monitorURLs:
document.getElementById('cboxMonitorURLs').checked || false,
ignoreCase:
document.getElementById('cboxIgnoreCase').checked || false
};
};
var cbDocumentReady = function()
{
console.clear();
console.debug("SniffSnuff is loaded and ready");
fnListenUIEvents();
fnInitTheme();
};
/**
* Initiates some of the Material theme components
*/
var fnInitTheme = function()
{
$.material.init();
};
var fnListenUIEvents = function()
{
var btnSearch = document.getElementById('btnSearch');
var btnClearAll = document.getElementById('btnClearAll');
btnSearch.addEventListener('click', cbBtnSearch);
btnClearAll.addEventListener('click', cbBtnClearAll);
};
var cbBtnClearAll = function(event)
{
var tblResults = document.getElementById('tableResults');
var newtbody = document.createElement('tbody');
var tbody = document.querySelectorAll('#tableResults tbody');
tbody[0].remove();
tblResults.appendChild(newtbody);
};
var cbBtnSearch = function(event)
{
var searchKeyword = document.getElementById('searchKeyword');
var tableResults = document.getElementById('tableResults');
var statusText = document.getElementById('statusText');
/* Set search options */
fnUpdateSearch();
//console.debug(event);
event.preventDefault();
if (search.active)
{
search.active = false;
event.toElement.innerText = 'Search';
event.toElement.className =
event.toElement.className.replace(/ btn-danger/gm, '');
statusText.innerText =
'Idle. Hit "Search" to start listening.';
}
else
{
search.keyword = searchKeyword.value;
statusText.innerText = 'Looking for "' +
search.keyword +
'". Listening in on traffic...';
search.active = true;
event.toElement.innerText = 'Stop';
event.toElement.className += ' btn-danger';
}
};
var options =
{
monitorRequests: true,
monitorResponses: true,
monitorHeaders: true,
monitorCookies: true,
monitorURLs: true,
ignoreCase: true
};
var search =
{
keyword: document.getElementById('keyword'),
active: false,
/** How many bytes to grab when printing the match spot */
delta: 20
};
document.addEventListener("DOMContentLoaded", cbDocumentReady);
//EOF