MapMyGlobe

Archive for the ‘Javascript’ Category

Delete your Facebook Friends

Friday, March 21st, 2008

A non-neglictible part of this blog’s readers come for my Facebook scripts, which aim at automating a few things via javascript. One of those readers recently suggested a utility for automatically deleting friends. The reason for that is that he’s so popular he hits the 5,000 Facebook friends limit and can’t add any of the 11,000 peeps that sent him a request :) So, a workaround would be to first delete all of his current friends to make room for the new ones.

Now, my solution is not perfect because Facebook displays friends within a 50-items paging system. Which means you would need to run it 100 times to get rid of all your friends. Still, that’s a 50x improvement ratio if you really do need to delete your friends. The code is the following:

var remove = $$('a.remove');
for (var i = 0; i < remove.length; i++){
  remove[i].onclick();
  $('dialog_button1').click();
}

which you need to run at http://www.facebook.com/friends/?all. I also wrote a Greasemonkey script here or at http://userscripts.org/scripts/show/24199. Hope that helps somebody :)

Gmail Homepage’s Quota estimate

Tuesday, February 26th, 2008

I was curious about how the quota counter on Gmail’s homepage (you know, the one before you log in, that’s updated every second) worked, so I had a quick look at the source, and it proved pretty interesting. First, it’s one of the few scripts I’ve seen from Google that’s not obfuscated. Then, it turns out they use linear interpolation between some past storage values and estimated future storage. The joke is that they have a storage estimate of Number.MAX_VALUE in Year 3456 :)

gmail-logo.gif

A brilliant summary of how storage estimates evolved over time can be found on Zvi Devir’s page. He also points out the Sci-Fi inside joke comment in the interpolation array definition from the geeks over at Google: // Estimates of nanite storage generation over time. :) And finally, a function called updateQuota() is called every second to change the quota value on the webpage via DOM scripting.

Another interesting thing in this script is how they get a value of Round-trip Time (RTT) between your browser and their server and store it in a cookie for future use. They do that by loading an image of size 0, and by looking at the time span between when they start to send it and after they receive it. Well, strictly speaking, depending on your connection’s bandwidth this estimate can be higher because you have to send all the bytes in the request and get all the bytes in the response (when strictly speaking RTT is for one Bit) but I guess the estimate is good enough that way. In my case Gmail’s RTT varies greatly, from values as low as 172ms to as high as 3s. For reference, the code they use is:

var ONE_PX = "https://mail.google.com/mail/images/c.gif?t=" +
  (new Date()).getTime();

function LogRoundtripTime() {
  var img = new Image();
  var start = (new Date()).getTime();
  img.onload = GetRoundtripTimeFunction(start);
  img.src = ONE_PX;
}

function GetRoundtripTimeFunction(start) {
  return function() {
  var end = (new Date()).getTime();
  SetGmailCookie("GMAIL_RTT", (end - start));
  }
}