Skip to content

Instantly share code, notes, and snippets.

@parthpower
Last active February 18, 2016 12:59
Show Gist options
  • Save parthpower/a1164d906a4aabfad7ac to your computer and use it in GitHub Desktop.
Save parthpower/a1164d906a4aabfad7ac to your computer and use it in GitHub Desktop.
Search Result Size

Search Result Size

Stable Optimized version of URL Content Size for Google Search Results.

What is this?

Get the size of each Google search result before visiting the site. It uses CROS proxy https://cors-anywhere.herokuapp.com/ for making HEAD request to the destination URL.

Quick Start

  • Create a bookmark and set the content of bookmarklet as the URL. (Keep that bookmark in the bookmarkbar)
  • On search result page of Google, click on that bookmark and see the magic.

Known Issues

  • It doesn't work on all URLs due to Refused to get unsafe header "content-length" exception.
  • The size of the web page may not be exact same because of dynamically loading XHRs.

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 getLinks(){for(hh=document.getElementsByClassName("r"),a=[],i=0;i<hh.length;i++)container=hh[i].getElementsByTagName("a")[0],lnk=container.getAttribute("data-href"),null===lnk&&(lnk=container.getAttribute("href")),null!==lnk&&a.push(lnk),UrlSize("https://cors-anywhere.herokuapp.com/"+lnk,container);return a}getLinks();
/*
Search Result 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();
}
function getLinks() {
hh = document.getElementsByClassName('r');
a = [];
for (i = 0; i < hh.length; i++) {
container = hh[i].getElementsByTagName('a')[0];
lnk = container.getAttribute('data-href'); //Some Of results has URL on this tag (IDK the reason, Google is so weird)
if (lnk === null )
lnk = container.getAttribute('href'); //If not then get link from here.
if (lnk !== null )
a.push(lnk); //Collect the links
UrlSize("https://cors-anywhere.herokuapp.com/" + lnk, container); //Using CORS proxy Tons of thanks to this project
}
return a;
}
getLinks();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment