forked from GoogleChrome/chrome-extensions-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
headers.js
78 lines (66 loc) · 2.4 KB
/
headers.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
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
var tabId = parseInt(window.location.search.substring(1));
window.addEventListener("load", function() {
chrome.debugger.sendCommand({tabId:tabId}, "Network.enable");
chrome.debugger.onEvent.addListener(onEvent);
});
window.addEventListener("unload", function() {
chrome.debugger.detach({tabId:tabId});
});
var requests = {};
function onEvent(debuggeeId, message, params) {
if (tabId != debuggeeId.tabId)
return;
if (message == "Network.requestWillBeSent") {
var requestDiv = requests[params.requestId];
if (!requestDiv) {
var requestDiv = document.createElement("div");
requestDiv.className = "request";
requests[params.requestId] = requestDiv;
var urlLine = document.createElement("div");
urlLine.textContent = params.request.url;
requestDiv.appendChild(urlLine);
}
if (params.redirectResponse)
appendResponse(params.requestId, params.redirectResponse);
var requestLine = document.createElement("div");
requestLine.textContent = "\n" + params.request.method + " " +
parseURL(params.request.url).path + " HTTP/1.1";
requestDiv.appendChild(requestLine);
document.getElementById("container").appendChild(requestDiv);
} else if (message == "Network.responseReceived") {
appendResponse(params.requestId, params.response);
}
}
function appendResponse(requestId, response) {
var requestDiv = requests[requestId];
requestDiv.appendChild(formatHeaders(response.requestHeaders));
var statusLine = document.createElement("div");
statusLine.textContent = "\nHTTP/1.1 " + response.status + " " +
response.statusText;
requestDiv.appendChild(statusLine);
requestDiv.appendChild(formatHeaders(response.headers));
}
function formatHeaders(headers) {
var text = "";
for (name in headers)
text += name + ": " + headers[name] + "\n";
var div = document.createElement("div");
div.textContent = text;
return div;
}
function parseURL(url) {
var result = {};
var match = url.match(
/^([^:]+):\/\/([^\/:]*)(?::([\d]+))?(?:(\/[^#]*)(?:#(.*))?)?$/i);
if (!match)
return result;
result.scheme = match[1].toLowerCase();
result.host = match[2];
result.port = match[3];
result.path = match[4] || "/";
result.fragment = match[5];
return result;
}