Thursday, March 12, 2009

Twitter Stats using Greasemonkey & jQuery

I wrote a simple Greasemonkey script that uses jQuery to read the Following, Followers, and Updates count from your Twitter Home page and displays them in the title of your Firefox tab (as seen in the image to the left).

Since I enjoy using jQuery, I tend to use this script as the starting point for several other of my custom Greasemoney scripts.

You can install the following Greasemonkey script from the website.

// ==UserScript==
// @name           Twitter Stats
// @namespace
// @description    Display your Twitter Stats in the Tab Title
// @include
// ==/UserScript==

//BEGIN - Load jQuery
var GM_JQ = document.createElement('script');
GM_JQ.src = '';
GM_JQ.type = 'text/javascript';

function GM_wait() {
   if (typeof unsafeWindow.jQuery == 'undefined') { window.setTimeout(GM_wait,100); }
   else { $ = unsafeWindow.jQuery; letsJQuery(); }
//END - Load jQuery
// All your GM code must be inside this function
function letsJQuery() {
   unsafeWindow.console.log('BEGIN letsJQuery'); 

   var followers = $("#follower_count").html();
   var following = $("#following_count").html();
   var updates = $("#update_count").html();
   document.title = 'Twitter / Ing: ' + $.trim(following) + '; Ers: ' + $.trim(followers); 
   unsafeWindow.console.log('END letsJQuery');

unsafeWindow.alert = function alert(message) {
   //do nothing

The above script first loads the jQuery framework and then retrieves the Twitter values.

You might notice the use of unsafeWindow.console.log('Your message here...');

I mainly used this to help debug the script.

You can find these messages displayed in your Firebug console.

Once you have installed the script, it could be easily be coupled with the ReloadEvery firefox extension so that you can sit back and watch your stats update to your heart's content :)


  1. Your current method of injecting jQuery into the page is expensive on bandwidth. It will need to be injected on every page load. Additionally it may reduce the speed at which your script starts up.

    I've found that using @require is more effective as jQuery will only be downloaded on install and you won't need to mess around with the waiting or getting the $ object from the unsafe window.

    Here's an example script that uses that technique to add a Twitter search box to Twitter user pages.

  2. Hello,

    Please add your site at is a place where other people can find you among the best sites on the internet!
    Its just started and we are collecting the best found on the net! We will be delighted to have you in the sweebs listings.


  3. Hi ,
    what is the best way to get the following information with Twitter API without getting Login Credentials for twitterUserA:

    1)suppose I have a user called twitterUserA
    I want to get detailed information for people who are following him and his followers.

    2)so i need Followersname, FollowerID, FollowersFollowerCount,FollowersFollowingCount,followersUpdates#,and other info if possible.

    what is the fastest and best way to get information for a given twitter account without asking for twitter Log in credentials (username and password)