Skip to content

Instantly share code, notes, and snippets.

@parthpower
Last active February 18, 2016 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parthpower/a2fcd7b5e288ab3e1d3e to your computer and use it in GitHub Desktop.
Save parthpower/a2fcd7b5e288ab3e1d3e to your computer and use it in GitHub Desktop.
URL Content Size

URL Content Size

Get the size of every URL content within a page. It uses CROS proxy https://cors-anywhere.herokuapp.com/ for making HEAD request to the destination URL. Similar to Search Result Size but this works on all pages.

Quick Start

  • Create a bookmark and set the content of bookmarklet as the URL. (Keep that bookmark in the bookmarkbar)
  • Go to any web page with links, and click that bookmark.

Known Issues

  • It doesn't work on all URLs.
  • The size of the web page may not be exact same because of dynamically loading XHRs.
  • It may affect functionality as well as asthetics of the web page.

Special thanks to @jdcpower1994 coming up with this amazing idea. Thanks to cros-anywhere by Rob--W for CROS proxy.

Tweet me @parthpower for bug reports or feature request

javascript:function UrlSize(e,t){var n=new XMLHttpRequest;n.open("HEAD",e),n.onreadystatechange=function(){this.readyState==this.DONE&&(size=(this.getResponseHeader("content-length")/1024).toFixed(2),size>0&&(t.textContent="["+size+"kb]"+t.textContent))},n.send()}function getAllSize(){for(l=document.links,i=0;i<l.length;i++)if(lnk=null,href=l[i].getAttribute("data-href"),null===href&&(href=l[i].getAttribute("href")),null!==href&&(lnk=href.match(reg)),null!==lnk)for(k=0;k<lnk.length;k++)"/"===lnk[k][0]&&(spl=window.location.href.split("/"),lnk[k]=spl[0]+"//"+spl[2]+lnk[k]),UrlSize("https://cors-anywhere.herokuapp.com/"+lnk[k],l[i])}reg=/(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#\/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[A-Z0-9+&@#\/%=~_|$])/gim,getAllSize();
/*
URL Content Size
Copyright (c) 2016 Parth V. Parikh
Freely distributable under MIT lincese.
*/
function UrlSize(url, container)
{
var http = new XMLHttpRequest();
http.open('HEAD', url);
http.onreadystatechange = function() {
if (this.readyState == this.DONE) {
size = (this.getResponseHeader('content-length') / 1024).toFixed(2);
if (size > 0) {
container.textContent = "[" + size + "kb]" + container.textContent;
}
}
}
http.send();
}
reg = /(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#\/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[A-Z0-9+&@#\/%=~_|$])/igm;
function getAllSize() {
l = document.links;
for (i = 0; i < l.length; i++) {
lnk=null;
href = l[i].getAttribute('data-href');
if (href === null )
href = l[i].getAttribute('href');
if (href !== null )
lnk = href.match(reg);
if (lnk !== null ){
for(k=0;k<lnk.length;k++){
if(lnk[k][0] === '/'){
spl = window.location.href.split("/");
lnk[k]=spl[0] + "//"+spl[2]+lnk[k];
}
UrlSize("https://cors-anywhere.herokuapp.com/" + lnk[k],l[i]);
}
}
}
}
getAllSize();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment