Skip to content

Commit d59a5ca

Browse files
committed
带界面的初始版
1 parent 95fe240 commit d59a5ca

41 files changed

Lines changed: 9164 additions & 71 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

HttpMonitor.crx

413 KB
Binary file not shown.

src/Fmt.js

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -420,54 +420,3 @@ Fmt.http = (function($$) {
420420
}
421421

422422
})(Fmt);
423-
424-
// Fmt.getDownloadData = function(data) {
425-
// var rows = []
426-
// $.each(window.domainData, function() {
427-
// var d = this
428-
429-
// var row = {
430-
// request: {
431-
// protocol: 'http',
432-
// url: d.url,
433-
// method: d.method,
434-
// contentType: d.contentType,
435-
// headers: [],
436-
// dataMode: 'params',
437-
// data: [],
438-
// },
439-
// response: {
440-
// headers: []
441-
// }
442-
// };
443-
444-
// $.each(d.request, function(k, v) {
445-
// row.request.headers.push({
446-
// name: k,
447-
// value: v
448-
// })
449-
// })
450-
// $.each(d.response, function(k, v) {
451-
// row.response.headers.push({
452-
// name: k,
453-
// value: v
454-
// })
455-
// })
456-
457-
// if ($.isPlainObject(d.post)) {
458-
// row.request.dataMode = 'params'
459-
// $.each(d.post, function(k, v) {
460-
// row.request.data.push({
461-
// name: k,
462-
// value: v
463-
// })
464-
// })
465-
// } else if ($.type(d.post) === "string") {
466-
// row.request.dataMode = 'raw'
467-
// row.request.data = d.post
468-
// }
469-
470-
// rows.push(row)
471-
// })
472-
// return rows
473-
// }

src/background.js

Lines changed: 220 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
var requestSent = {},
22
outputData = initOutputData(),
33
isCapturing = false,
4+
enableCapturing = false,
5+
popupPageId = null,
6+
filterCond = {
7+
banList: [],
8+
allowList: [],
9+
contentTypeList: ['Document', 'XHR']
10+
},
11+
removeIds = {},
412
idleIconPath = './icon.png',
513
recordingIconPath = './icon-rec.png';
614

@@ -16,21 +24,31 @@ function initOutputData() {
1624
}
1725
}
1826

19-
function generateDate()
20-
{
27+
function generateDate() {
2128
var x = new Date();
2229
var y = x.getFullYear();
23-
var m = x.getMonth()+1; m = m < 10 ? '0' + m : m;
24-
var d = x.getDate(); d = d < 10 ? '0' + d : d;
25-
var h = x.getHours(); h = h < 10 ? '0' + h : h;
26-
var i = x.getMinutes(); i = i < 10 ? '0' + i : i;
27-
var s = x.getSeconds(); s = s < 10 ? '0' + s : s;
30+
var m = x.getMonth() + 1;
31+
m = m < 10 ? '0' + m : m;
32+
var d = x.getDate();
33+
d = d < 10 ? '0' + d : d;
34+
var h = x.getHours();
35+
h = h < 10 ? '0' + h : h;
36+
var i = x.getMinutes();
37+
i = i < 10 ? '0' + i : i;
38+
var s = x.getSeconds();
39+
s = s < 10 ? '0' + s : s;
2840
return [y, m, d, 'T', h, i, s].join('')
2941
}
3042

3143
function resetSession() {
3244
requestSent = {};
3345
outputData = initOutputData();
46+
popupPageId = null;
47+
filterCond = {
48+
banList: [],
49+
allowList: [],
50+
contentTypeList: ['Document', 'XHR']
51+
};
3452
}
3553

3654
function toggle(currentTab) {
@@ -42,12 +60,15 @@ function toggle(currentTab) {
4260
startCapturing(target);
4361
} else {
4462
stopCapturing(target);
45-
exportSession();
4663
}
4764
resetSession();
4865

4966
isCapturing = !isCapturing;
5067
}
68+
69+
chrome.browserAction.setTitle({
70+
'title': "HttpMonitor"
71+
});
5172
chrome.browserAction.onClicked.addListener(toggle);
5273

5374
function startCapturing(target) {
@@ -57,17 +78,24 @@ function startCapturing(target) {
5778
chrome.browserAction.setIcon({
5879
path: recordingIconPath
5980
});
81+
openPopupPage();
82+
// chrome.browserAction.setBadgeText({'text': 'ON'});
6083
}
6184

6285
function stopCapturing(target) {
6386
chrome.debugger.detach(target);
6487
chrome.browserAction.setIcon({
6588
path: idleIconPath
6689
});
90+
closePopupPage();
91+
enableCapturing = false;
92+
// chrome.browserAction.setBadgeText({'text': ''});
6793
}
6894

6995
function onDebuggerEvent(debugee, message, params) {
70-
96+
if (!enableCapturing) {
97+
return true;
98+
}
7199
if (message == "Network.requestWillBeSent") {
72100
requestSent[params.requestId] = params.request;
73101
} else if (message == "Network.responseReceived") {
@@ -77,14 +105,27 @@ function onDebuggerEvent(debugee, message, params) {
77105

78106
var request = requestSent[params.requestId];
79107
if (!request) return;
80-
outputData.requests.push(serializeRequest(params.type, request, params.response));
108+
captureRequest(params.requestId, params.type, request, params.response);
81109
});
82110
}
83111
}
84112

85-
function serializeRequest(callType, request, response) {
113+
function captureRequest(id, callType, request, response) {
114+
var row = serializeRequest(id, callType, request, response);
115+
outputData.requests.push(row);
116+
delete requestSent[id];
117+
chrome.runtime.sendMessage({
118+
actionType: 'data',
119+
data: outputData.requests,
120+
removeIds: removeIds
121+
}, function(response) {});
122+
}
123+
124+
function serializeRequest(id, callType, request, response) {
86125

126+
id = (id + '').replace('.', '');
87127
return {
128+
id: id,
88129
url: request.url,
89130
method: request.method,
90131
protocol: response.protocol,
@@ -103,14 +144,177 @@ function serializeRequest(callType, request, response) {
103144
}
104145
}
105146

106-
function exportSession() {
107-
if (outputData.requests.length < 1) return;
108-
outputData.date = generateDate();
147+
function filterData(cond, requests) {
148+
149+
// 默认是全部显示
150+
requests.forEach(function(d) {
151+
d.hidden = false;
152+
});
153+
//校验Url 正则表达式
154+
var urlReg = /[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\.?/;
155+
var allowList = cond.allowList || []
156+
var banList = cond.banList || []
157+
var contentTypeList = cond.contentTypeList || []
158+
159+
if (allowList.length > 0) { // 如果包含白名单,则只显示白名单的内容,其它全部隐藏
160+
161+
requests.forEach(function(d) {
162+
d.hidden = true; // 默认设为 隐藏
163+
// 只有允许列表中的域名才能被显示
164+
var host = urlReg.exec(d.url);
165+
var address = (host[0]);
166+
allowList.forEach(function(item) {
167+
if (address.indexOf(item) >= 0) {
168+
d.hidden = false;
169+
}
170+
})
171+
});
172+
173+
} else if (banList.length > 0) { //如果只包含黑名单,则仅经用掉黑名单的域名
174+
175+
requests.forEach(function(d) {
176+
d.hidden = false; // 默认设为 显示
177+
// 只有允许列表中的域名才能被隐藏
178+
var host = urlReg.exec(d.url);
179+
var address = (host[0]);
180+
banList.forEach(function(item) {
181+
if (address.indexOf(item) >= 0) {
182+
d.hidden = true;
183+
}
184+
})
185+
});
186+
}
187+
188+
// 过滤内容类型 (仅对显示的请求数据进行过滤)
189+
if (contentTypeList.length > 0) {
190+
requests.forEach(function(d) {
191+
if (!d.hidden) {
192+
var findit = false
193+
contentTypeList.forEach(function(item) {
194+
if (item === d.callType) {
195+
findit = true
196+
return false // 跳出循环
197+
}
198+
})
199+
200+
if (!findit) {
201+
d.hidden = true
202+
}
203+
}
204+
});
205+
}
206+
207+
var fd = initOutputData();
208+
requests.forEach(function(d) {
209+
if (!d.hidden && !isRemove(d.id)) {
210+
fd.requests.push(d);
211+
}
212+
});
213+
214+
return fd;
215+
}
216+
217+
function exportSession(filter) {
218+
var fd = filterData(filter, outputData.requests);
219+
if (fd.requests.length < 1) return;
220+
fd.date = generateDate();
109221

110-
var blob = new Blob([JSON.stringify(outputData, null, 2)], {
222+
var blob = new Blob([JSON.stringify(fd, null, 2)], {
111223
type: "text/plain;charset=utf-8"
112224
});
113225

114-
var fileNameSuffix = outputData.date;
226+
var fileNameSuffix = fd.date;
115227
saveAs(blob, "HttpMonitor" + fileNameSuffix + ".txt");
228+
}
229+
230+
function openPopupPage() {
231+
var urlPopup = 'ui/popup.html?version=' + chrome.app.getDetails().version;
232+
//窗口参数
233+
var openWinArgs = {
234+
width: 1040, //parseInt(window.screen.availWidth / 2),
235+
height: 440 //parseInt(window.screen.availHeight)
236+
};
237+
238+
var createWinPopupData = {
239+
url: urlPopup,
240+
type: 'popup',
241+
left: 0,
242+
top: 0,
243+
width: openWinArgs.width,
244+
height: openWinArgs.height //parseInt(openWinArgs.height / 2)
245+
};
246+
247+
chrome.windows.create(createWinPopupData, function(openWin) {
248+
popupPageId = openWin.id;
249+
});
250+
251+
}
252+
253+
function closePopupPage() {
254+
if (popupPageId) {
255+
chrome.windows.remove(popupPageId, function() {
256+
popupPageId = null;
257+
});
258+
}
259+
}
260+
261+
chrome.windows.onRemoved.addListener(function(winId) {
262+
if (winId === popupPageId) {
263+
// enableCapturing = false;
264+
}
265+
});
266+
267+
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
268+
var actionType = message.actionType;
269+
270+
switch (actionType) {
271+
case 'start':
272+
enableCapturing = true;
273+
break;
274+
275+
case 'stop':
276+
enableCapturing = false;
277+
break;
278+
279+
case 'clean':
280+
outputData = initOutputData();
281+
break;
282+
283+
case 'delete':
284+
removeData(message.id);
285+
break;
286+
287+
case 'list':
288+
break;
289+
290+
case 'download':
291+
exportSession(message.filterCond);
292+
break;
293+
294+
case 'log':
295+
var log = message.log;
296+
log(log);
297+
break;
298+
299+
default:
300+
alert('actionType is error');
301+
break;
302+
}
303+
304+
});
305+
306+
function removeData(id)
307+
{
308+
var rid = "x:" + id;
309+
removeIds[rid] = true;
310+
}
311+
312+
function isRemove(id)
313+
{
314+
var rid = "x:" + id;
315+
return removeIds[rid] ? true : false;
316+
}
317+
318+
function log(log) {
319+
116320
}

src/manifest.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
{
22
"manifest_version": 2,
3-
3+
44
"name": "HttpMonitor",
55
"description": "Monitor all HTTP/HTTPs traffic from your browser.",
66
"version": "0.0.1",
77
"author": "httpmonitor@lianzhi.com",
8-
8+
"icons": {
9+
"16": "icon-rec.png",
10+
"48": "icon-rec.png",
11+
"128": "icon-rec.png"
12+
},
913
"background": {
1014
"scripts": ["background.js", "FileSaver.min.js", "Fmt.js"]
1115
},
@@ -14,5 +18,5 @@
1418
"default_title": "HttpMonitor"
1519
},
1620
"offline_enabled": true,
17-
"permissions": [ "webRequest", "webRequestBlocking", "tabs", "http://*/", "https://*/", "<all_urls>", "debugger" ]
18-
}
21+
"permissions": ["webRequest", "webRequestBlocking", "tabs", "http://*/", "https://*/", "<all_urls>", "debugger", "storage"]
22+
}

0 commit comments

Comments
 (0)