Skip to content

Commit

Permalink
Updates:
Browse files Browse the repository at this point in the history
- add request type and reponse status
- add response time taken
  • Loading branch information
phempshall committed Dec 4, 2016
1 parent fc10ab4 commit aa248ad
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
14 changes: 7 additions & 7 deletions chrome/fetchHeaders.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ var filters = {
types: ["main_frame"]
};

/* headers received */
chrome.webRequest.onHeadersReceived.addListener(function(details) {
headers[details.tabId] = headers[details.tabId] || {};
headers[details.tabId].response = details;
}, filters, ["responseHeaders"]);

/* headers sent */
chrome.webRequest.onSendHeaders.addListener(function(details) {
headers[details.tabId] = headers[details.tabId] || {};
headers[details.tabId].request = details;
}, filters, ["requestHeaders"]);

/* headers received */
chrome.webRequest.onHeadersReceived.addListener(function(details) {
headers[details.tabId] = headers[details.tabId] || {};
headers[details.tabId].response = details;
}, filters, ["responseHeaders"]);

/* remove tab data from headers object when tab is onRemoved */
chrome.tabs.onRemoved.addListener(function(tabId, removeInfo) {
delete headers[tabId];
});
});
33 changes: 32 additions & 1 deletion chrome/headersPopup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

/* this needs cleaning up :'( */
chrome.tabs.query({active: true, currentWindow: true}, function(tab) {
var results = document.getElementById('results'),
headers = chrome.extension.getBackgroundPage().headers[tab[0].id];
Expand All @@ -16,21 +17,51 @@ chrome.tabs.query({active: true, currentWindow: true}, function(tab) {

function printResults () {
Object.keys(headers).forEach(function(key) {
console.log(headers[key]);
// console.log(headers[key]);
headers[key][key + 'Headers'].sort(function(a,b) {return (a.name > b.name) ? 1 : ((b.name > a.name) ? -1 : 0);});

printHeading(key);

for (var i = 0; i < headers[key][key + 'Headers'].length; i++) {
if (i === 0) {
printStatus(headers[key]);
}
printHeader(headers[key][key + 'Headers'][i]);
}
});

function printStatus (obj) {
var p = document.createElement('p'),
b = document.createElement('b');

if (obj.statusLine) {
b.appendChild(document.createTextNode(obj.statusLine));
}
else {
b.appendChild(document.createTextNode(obj.method + ' ' + obj.url));
}
p.appendChild(b);
results.appendChild(p);
}

function calcTime () {
return ((headers['response'].timeStamp - headers['request'].timeStamp) / 1000).toFixed(4);
}

function printHeading (key) {
var n = key[0].toUpperCase() + key.substring(1);
var h = document.createElement('h2'),
t = document.createTextNode(n);

h.appendChild(t);

if (key === 'response') {
var timeTaken = calcTime(),
s = document.createElement('small');
s.appendChild(document.createTextNode(' (in ' + timeTaken + 's)'));
h.appendChild(s);
}

results.appendChild(h);
}

Expand Down
4 changes: 2 additions & 2 deletions chrome/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"author": "Paul Hempshall",

"name": "HTTP Headers",
"version": "1.0.1",
"description": "This extension will show you the HTTP request and response headers for the current tab.\n\nhttps://github.com/phempshall/http-headers",
"version": "1.0.2",
"description": "This extension will show you the HTTP request and response headers for the current tab.\n\nOpen Source: https://github.com/phempshall/http-headers",

"background": {
"page": "fetchHeaders.html"
Expand Down

0 comments on commit aa248ad

Please sign in to comment.