1

I'm wanting to write to the console the size of a given page. Note that if the page is gzipped, I want to know the gzipped size and the ungzipped size. I also want to be able to do this for any ajax requests.

If there is a way of doing this without plugin that would be great.

vdh_ant
  • 12,720
  • 13
  • 66
  • 86
  • Try [`chrome.devtools.network`](https://developer.chrome.com/extensions/devtools.network.html). – Rob W Apr 24 '13 at 14:31
  • I looked at this, but I'm not sure I can access the HAR data unless they have devtools opened at network tab? At least thats how I understand this - "The chrome.devtools.* API modules are available only to the pages loaded within the Developer Tools window." http://stackoverflow.com/questions/12689239/why-is-chrome-devtools-network-undefined. The HAR data is exactly what I want but I need to be able to do it without open devtools. I just want to display page size over the users page. – vdh_ant Apr 24 '13 at 15:59
  • 1
    You need to create a [devtools](https://developer.chrome.com/extensions/devtools.html) extension. In your question, you said that the info has to be logged to the console, so requiring the devtools to be opened should not be a problem. – Rob W Apr 24 '13 at 16:01
  • I was trying to make the question as simple as possible and I didn't know there would be this limitation. Given that I want to display page size over the users page what options do I have? I have see some pretty amazing plugins, there has to be something. – vdh_ant Apr 24 '13 at 20:58
  • 1
    The chrome.webRequest (same limitations as the answer below, but no additional request) or chrome.debugger (not sure) APIs might be useful. – Rob W Apr 24 '13 at 21:02
  • Using chrome.webRequest, how would I determine the page size (pre/post) compression? I know I could get the pre from the headers but what about the post? – vdh_ant Apr 24 '13 at 21:22
  • 1
    Seems to be impossible with the current APIs. – Rob W Apr 25 '13 at 19:51

1 Answers1

-1
var req = new XMLHttpRequest();
req.open('GET', document.location, false);
req.send(null);
var headers = req.getAllResponseHeaders().toLowerCase();
var encoding = (headers.match(/Content-encoding\: (\w*)/i) || []).pop();
var unpacked_size = req.responseText.length;
var packed_size = -1;
if (['gzip','deflate','lzh','sdch'].indexOf(encoding) !== -1) {
    packed_size = parseInt((headers.match(/content-length\: (\d*)/i) || []).pop());
}

var result = {
    encoding: encoding,
    unpacked_size: unpacked_size,
    packed_size: packed_size,
}
Justin Alexander
  • 2,004
  • 3
  • 21
  • 25
  • 2
    This does this via an ajax request, I don't want to make this an extra request. – vdh_ant Apr 24 '13 at 13:42
  • there is no way to access the current pages header data from the scope of the pages own javascript. And your question didn't specify that this was a requirement. – Justin Alexander Apr 24 '13 at 15:05
  • 2
    The question title is "Accessing page size for requests - Chrome Extension". It needs to be a chrome extension afaik. – vdh_ant Apr 24 '13 at 15:54
  • content-length is very frequently skipped, and under certain conditions it should not be added. – fbiagi Apr 30 '13 at 18:47