-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
71 lines (58 loc) · 2.01 KB
/
background.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
var requestSent = {},
outputString = "",
isCapturing = false,
idleIconPath = './icon.png',
recordingIconPath = './icon-rec.png';
function resetSession() {
requestSent = {};
outputString = "";
}
function toggle(currentTab) {
var target = {
tabId: currentTab.id
};
if (!isCapturing) {
startCapturing(target);
} else {
stopCapturing(target);
exportSession();
}
resetSession();
isCapturing = !isCapturing;
}
chrome.browserAction.onClicked.addListener(toggle);
function startCapturing(target) {
chrome.debugger.attach(target, "1.0");
chrome.debugger.sendCommand(target, "Network.enable");
chrome.debugger.onEvent.addListener(onDebuggerEvent);
chrome.browserAction.setIcon({ path: recordingIconPath });
}
function stopCapturing(target) {
chrome.debugger.detach(target);
chrome.browserAction.setIcon({ path: idleIconPath });
}
function onDebuggerEvent(debugee, message, params) {
if (message == "Network.requestWillBeSent" && params.type == "XHR") {
requestSent[params.requestId] = params.request;
} else if (message == "Network.responseReceived" && params.type == "XHR") {
chrome.debugger.sendCommand(debugee, "Network.getResponseBody", params, function(responseBody) {
params.response.base64Encoded = responseBody.base64Encoded;
params.response.body = responseBody.body;
var request = requestSent[params.requestId];
outputString += serializeRequestToText(request, params.response);
});
}
}
function serializeRequestToText(request, response) {
return response.requestHeadersText
+ (request.postData || "") + "\r\n\r\n"
+ response.headersText
+ (response.body || "") + "\r\n\r\n";
}
function exportSession() {
var blob = new Blob([outputString], {
type: "text/plain;charset=utf-8"
});
var fileNameSuffix = new Date().toISOString().replace(/:/g, "-");
saveAs(blob, "SessionTraces" + fileNameSuffix + ".txt");
}